ValueError:无法输入形状的值,但是形状相同

时间:2018-11-23 23:07:20

标签: python tensorflow machine-learning error-handling

我用图像尺寸200x200x4创建了一个简单的cnn 和 结果:

layer1:  (?, 100, 100, 32)
layer2:  (?, 50, 50, 64)
#reshape layer2(?x50x50x64) -> layer2_flat(?x160000)

layer2_f:  (?, 160000)
#linear=tf.matmul(w3,l2_flat)+b

linear:  (?, 2)
x:  (?, 160000)   x_data:  (200, 200, 4)
w:  (160000, 2)  x:  (?, 160000)   x_data:  (1, 160000)  b:  (2,)
learning start!!!
(?, 160000)   (1, 160000)    (?, 2)    (2,)

ValueError: Cannot feed value of shape (2,) for 
Tensor 'Placeholder_1:0', which has shape '(?, 2)'

我将所有图像数据存储到numpy数组中,并且在学习阶段发生了错误。 还是我是一名大学生(在编写此问题之前,我看到了有关此错误的其他问题,但不适用于我的代码。) 你们能帮忙为什么会这样吗?

#making layer
tf.reset_default_graph()
x_data=[img1,img2,img3]
y_data=[[0,1],[0,1],[0,1]]
for i in range(0,3):
    y_data[i]=np.array(y_data[i])
x=tf.placeholder(tf.float32,[None,160000])
x_img=tf.reshape(x,[-1,200,200,4])
y=tf.placeholder(tf.float32,[None,2])


#l1
w1=tf.Variable(tf.random_normal([3,3,4,32],stddev=0.01))
l1=tf.nn.conv2d(x_img,w1,strides=[1,1,1,1],padding='SAME')
l1=tf.nn.relu(l1)
l1=tf.nn.max_pool(l1,ksize=[1,2,2,4],strides=    [1,2,2,1],padding='SAME')
print('l1: ',l1.shape)
#in->filter(3x3x1)x32개->padding->relu->max_pool(2x2)
#    strides(1x1)                       strides(2x2)

#l2
w2=tf.Variable(tf.random_normal([3,3,32,64],stddev=0.01))
l2=tf.nn.conv2d(l1,w2,strides=[1,1,1,1],padding='SAME')
tf.nn.relu(l2)
l2=tf.nn.max_pool(l2,ksize=[1,2,2,4],strides=[1,2,2,1],padding='SAME')
print('l2: ',l2.shape)
#50,50,64
l2_flat=tf.reshape(l2,[-1,50*50*64])

#w3
w3=tf.get_variable('w3',shape=[50*50*64,2])
b=tf.Variable(tf.random_normal([2]))
linear=tf.matmul(l2_flat,w3)+b
print('l2_f: ',l2_flat.shape)
print('linear: ',linear.shape)
cost=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(
logits=linear,labels=y))
optimizer=tf.train.AdamOptimizer(learning_rate=0.01).minimize(cost)

print('x: ',x.shape,' ','x_data: ',x_data[1].shape)
sess=tf.Session()
sess.run(tf.global_variables_initializer())

x_data[0]=np.reshape(x_data[0],(1,160000))
x_data[1]=np.reshape(x_data[1],(1,160000))
x_data[2]=np.reshape(x_data[2],(1,160000))

print('w3: ',w3.shape,' x: ',x.shape,' ','x_data: ',x_data[0].shape,
  ' b: ',b.shape)
print('learning start!!!')

for index in range(0,3):
    print(x.shape,' ',x_data[index].shape,'  ',y.shape,'  ',
     y_data[index].shape)

    feed_dict={x:x_data[index],y:y_data[index]}
    c=sess.run([cost],feed_dict=feed_dict)
print('finished!!!')

predict=tf.equal(tf.argmax(linear,1),tf.argmax(y,1))
accuracy=tf.reduce_mean(tf.cast(prediction,tf.float32))
print('accuracy:', sess.run(accuracy,feed_dict=feed_dict))

0 个答案:

没有答案