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

时间:2018-06-18 04:22:13

标签: python tensorflow regression

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

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 个答案:

答案 0 :(得分:0)

您必须为占位符元素提供值。请参阅文档here。正如您所看到的,他们已经使用feed_dict来提供相同的内容。