我有些错误。请帮我... 我研究线性回归,但我不知道原因,不能再解决这个问题了。
import tensorflow as tf
X = tf.placeholder(tf.float32, shape=[None])
Y = tf.placeholder(tf.float32, shape=[None])
x_train = [1,2,3]
y_train = [1,2,3]
w = tf.Variable(tf.random_normal([1]), name="weight")
b = tf.Variable(tf.random_normal([1]), name="bias")
hypothesis = x_train * w + b
cost = tf.reduce_mean(tf.square(hypothesis - y_train))
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)
train = optimizer.minimize(cost)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
sess.run(train)
if(step % 20 == 0):
print(step,'\t',sess.run(cost),'\t',sess.run(w),'\t',sess.run(b))
for step in range(501):
_cost, _w, _b = \
sess.run([cost,w,b,train],
feed_dict={X:[1,2,3,4,5], Y:[2.1,3.1,4.1,5.1,6.1]})
if step % 20 == 0:
print(step, _cost, _w, _b)
print(sess.run(hypothesis, feed_dict={x:[5]}))
下面提到的print(sess.run(hypothesis, feed_dict={x:[5]}))
出错:
有太多值要解包(预期3)
什么是too many values to unpack (expected 3)
错误?(T0T)
答案 0 :(得分:2)
在你的会话中你运行成本,w,b和火车。因此,即使train只返回None,也会返回4个值。
_cost, _w, _b, _ = \
sess.run([cost,w,b,train],
feed_dict={X:[1,2,3,4,5], Y:[2.1,3.1,4.1,5.1,6.1]})