这是我的第一个效果很好的代码。
输入是2维,输出是2维
第一个代码:
w = tf.Variable(tf.zeros([2,1]))
b = tf.Variable(tf.zeros([1]))
x = tf.placeholder(tf.float32,shape=[None,2])
t = tf.placeholder(tf.float32,shape=[None,1])
y = tf.nn.sigmoid(tf.matmul(x,w) + b)
cross_entropy = - tf.reduce_sum(t * tf.log(y) + (1-t) *
tf.log(1 -y))
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(cross_entropy)
correct_prediction = tf.equal(tf.to_float(tf.greater(y,0.5)),t)
X = np.array([[0,0],[0,1],[1,0],[1,1]])
Y = np.array([[0],[1],[1],[1]])
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
for epoch in range(200):
sess.run(train_step,feed_dict={
x:X,
t:Y
})
现在我想将其扩展到10维输入和2维输出。
然后我改变如下,但它显示错误 。 我知道这个错误与占位符的大小有关 我应该在哪里改变?为什么???
Traceback (most recent call last):
File "wisdom2.py", line 57, in <module>
t: Y
File "/Users/whitebear/tensorflow/lib/python3.4/site-packages/tensorflow/python/client/session.py", line 789, in run
run_metadata_ptr)
File "/Users/whitebear/tensorflow/lib/python3.4/site-packages/tensorflow/python/client/session.py", line 975, in _run
% (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape())))
ValueError: Cannot feed value of shape (5, 1) for Tensor 'Placeholder:0', which has shape '(?, 10)'
第二代码:
w = tf.Variable(tf.zeros([10,1])) ## change dimensions to 2 -> 10
b = tf.Variable(tf.zeros([1]))
x = tf.placeholder(tf.float32,shape=[None,10]) # change dimensions to 2 -> 10
t = tf.placeholder(tf.float32,shape=[None,1])
y = tf.nn.sigmoid(tf.matmul(x,w) + b)
cross_entropy = - tf.reduce_sum(t * tf.log(y) + (1 -t) * tf.log(1 -y))
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(cross_entropy)
correct_prediction = tf.equal(tf.to_float(tf.greater(y,0.5)),t)
##I changed here....
X = np.array([[0],[1],[0],[1],[1]]) #answer
Y = np.array([
[2,-2,3,-4,2,2,3,5,3,6],
[1,3,-3,2,2,5,1,3,2,3],
[-2,3,2,-2,2,-2,1,3,4,5],
[-2,2,-1,-2,2,-2,7,3,9,2],
[-2,-3,2,-2,2,-4,1,-4,4,5]
])
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
for epoch in range(200):
sess.run(train_step,feed_dict={
x: X,
t: Y
})
答案 0 :(得分:1)
您正在输入错误的形状作为placeholders
的输入。您已在占位符中更改了x
的维度,但却输入错误的输入X
(您尚未更改),而不是y
(您已更改)。因此,要么交换X,要么更改相应的placeholders