代码之家  ›  专栏  ›  技术社区  ›  송준석

如何从tensorflow中的简单回归预测代码得到结果?

  •  0
  • 송준석  · 技术社区  · 7 年前

    我试图通过编写以下代码来找到回归模型:

    X1 = tf.placeholder(tf.float32)
    X2 = tf.placeholder(tf.float32)
    X3 = tf.placeholder(tf.float32)
    X4 = tf.placeholder(tf.float32)
    X5 = tf.placeholder(tf.float32)
    
    
    Y = tf.placeholder(tf.float32)
    
    W1 = tf.Variable(tf.random_normal([1, 1]), dtype = tf.float32,name='weight1')
    W2 = tf.Variable(tf.random_normal([1, 1]), dtype = tf.float32,name='weight2')
    W3 = tf.Variable(tf.random_normal([1, 1]), dtype = tf.float32, name='weight3')
    W4 = tf.Variable(tf.random_normal([1, 1]), dtype = tf.float32,name='weight4')
    W5 = tf.Variable(tf.random_normal([1, 1]), dtype = tf.float32,name='weight5')
    b1=  b = tf.Variable(tf.random_normal([1]), dtype =  tf.float32 ,name='bias1')
    
    hypothesis = tf.sigmoid(tf.matmul(X1, W1)+tf.matmul(X2, W2)+tf.matmul(X3, W3)+tf.matmul(X4, W4) + tf.matmul(X5, W5) + b1)
    
    
    cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1 - Y) * tf.log(1 - hypothesis))
    train = tf.train.GradientDescentOptimizer(learning_rate=0.000000000000000001).minimize(cost)
    
    predicted = tf.cast(hypothesis > 0.5, dtype=tf.float32)
    accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, Y), dtype=tf.float32))
    
    with tf.Session() as sess:
       # Initialize TensorFlow variables
       sess.run(tf.global_variables_initializer())
       for step in range(5000):
           sess.run(train, feed_dict={X1:ct, X2: temperature, X3:humidity, X4: windspeed, X5:tideheight, Y:sst})
    

    但当我把代码转过来时,所有的权重都会变成nan值。

    1 回复  |  直到 7 年前
        1
  •  0
  •   unholy_me    7 年前

    必须将值馈送到占位符元素中。查看文档 here . 如你所见,他们使用 feed_dict 给予同样的东西。

    推荐文章