【发布时间】:2018-01-24 17:12:47
【问题描述】:
import tensorflow as tf
# H(x) = Wx + b
W = tf.Variable(tf.random_normal([1],name='weight'))
b = tf.Variable(tf.random_normal([1],name='bias'))
X = tf.placeholder(tf.float32)
Y = tf.placeholder(tf.float32)
hypothesis = X * W + b
cost = tf.reduce_mean(tf.square(hypothesis - Y))
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)
train = optimizer.minimize(cost)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
Weights = []
for step in range(100):
sess.run([cost,hypothesis,train], feed_dict={X:x_col[0],Y:y_col[0]})
if step % 99 ==0:
print(step, sess.run(cost), sess.run(W), sess.run(b))
这是我的代码。当我在 Python Shell 中输入x_col[0] 时,我得到array([ 3., 5., 73., 33.], dtype=float32),而对于y_col[0],我得到array([ 3., 5., 73., 33.])。
所以我相信代码应该可以为 b 提供 0 和 W 的 1 和 0 成本。但是出现了这个错误。不知道怎么解决这个问题
供您参考,供sess.run([cost,hypothesis,train], feed_dict={X:x_col[0],Y:y_col[0]})
我得到[960446.13, array([ 76.92639923, 127.70278168, 1854.09997559, 838.57220459], dtype=float32), None]。
【问题讨论】:
-
请阅读Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers? - 总结是这不是解决志愿者的理想方式,并且可能会适得其反。请不要将此添加到您的问题中。
-
感谢您编辑我的问题。抱歉,我不打算遇到这种情况。
标签: python numpy tensorflow