对不起,Tensorflow和Python中的Newbee,我实现这段代码是为了学习9个随机数的和。我得到一个错误,我不能understand.Unfortunately我不能在我们的教程中找到类似的问题在这里...
import tensorflow as tf
import numpy as np
n_samples = 100
x = tf.placeholder(tf.float32, shape=[n_samples, 9])
y = tf.placeholder(tf.float32, shape=[n_samples])
x_value = tf.placeholder(tf.float32, shape=[n_samples, 9])
y_value = tf.placeholder(tf.float32, shape=[n_samples])
W = tf.Variable(tf.zeros([9, 1]))
b = tf.Variable(0.0)
y = tf.matmul(x, W) + b
y_pred = tf.placeholder(tf.float32, shape=[n_samples])
cost = tf.reduce_sum((y - y_pred)**2 / n_samples)
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cost)
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
x_value = np.random.uniform(0, 1, size = (n_samples, 9))
y_value = np.random.uniform(0, 1, size = (n_samples))
for i in range(n_samples):
mysum = 0.0
print (i)
for j in range(9):
print (x_value[i][j])
mysum += x_value[i][j]
y_value[i] = mysum
print (y_value[i])
cost = sess.run( train_step, feed_dict={x: x_value, y: y_value} )
print (cost)我得到了这个错误:
ValueError: Cannot feed value of shape (100,) for Tensor u'add:0', which has shape '(100, 1)'任何帮助都是非常感谢的。
发布于 2017-03-02 01:10:24
代码定义了y两次:
y = tf.placeholder(tf.float32, shape=[n_samples])
# ...
y = tf.matmul(x, W) + b因为y只是一个普通的Python变量,所以第二个赋值会用偏置加法的输出覆盖占位符。当您为y提供一个值时,TensorFlow将其解释为试图为tf.matmul(x, W) + b的结果提供一个替换值,而不是原始的tf.placeholder()。
要解决此问题,请为占位符y和tf.matmul(x, W) + b的结果使用不同的Python变量名。
https://stackoverflow.com/questions/42537244
复制相似问题