我似乎无法解决该问题,因为我是Tensorflow的新手,我认为问题是图形不匹配,但无法解决,请提供帮助。 我想解决这个问题,因为我将在Android应用程序中使用它。
x = tf.placeholder(tf.float32,shape=[None,80,80,3])
y_true = tf.placeholder(tf.float32,shape=[None,4])
#Defining the graph
with tf.name_scope('Model'):
convo_1 = convolutional_layer(x,shape=[4,4,3,32])
convo_1_pooling = max_pool_2by2(convo_1)
convo_2 = convolutional_layer(convo_1_pooling,shape=[4,4,32,16])
convo_2_pooling = max_pool_2by2(convo_2)
convo_3 = convolutional_layer(convo_2_pooling,shape=[4,4,16,8])
convo_3_pooling = max_pool_2by2(convo_3)
convo_4 = convolutional_layer(convo_3_pooling,shape=[4,4,8,4])
convo_4_pooling = max_pool_2by2(convo_4)
convo_2_flat = tf.reshape(convo_4_pooling,[-1,5*5*4])
full_layer_one = tf.nn.relu(normal_full_layer(convo_2_flat,80))
hold_prob1 = tf.placeholder(tf.float32)
full_one_dropout = tf.nn.dropout(full_layer_one,keep_prob=hold_prob1)
full_layer_two = tf.nn.relu(normal_full_layer(full_one_dropout,40))
hold_prob2 = tf.placeholder(tf.float32)
full_two_dropout = tf.nn.dropout(full_layer_two,keep_prob=hold_prob2)
full_layer_three = tf.nn.relu(normal_full_layer(full_two_dropout,20))
hold_prob3 = tf.placeholder(tf.float32)
full_three_dropout = tf.nn.dropout(full_layer_three,keep_prob=hold_prob3)
full_layer_four = tf.nn.relu(normal_full_layer(full_three_dropout,10))
hold_prob4 = tf.placeholder(tf.float32)
full_four_dropout = tf.nn.dropout(full_layer_four,keep_prob=hold_prob4)
y_pred = normal_full_layer(full_four_dropout,4)
%%time
#Changing settings for GPU running.
config = tf.ConfigProto(allow_soft_placement=True)
config.gpu_options.allow_growth = True
config.gpu_options.allocator_type = 'BFC'
#Training and saving the result
with tf.Session(config=config) as sess:
sess.run(init)
summary_writer = tf.summary.FileWriter(TRAIN_DIR, graph=tf.get_default_graph())
for i in range(epochs):
for j in range(0,steps,step_size):
_ , c , summary,d = sess.run([train,cross_entropy,merged_summary_op,acc],feed_dict={x:X[j:j+step_size] , y_true:Y[j:j+step_size] ,hold_prob1:0.5,hold_prob2:0.5,hold_prob3:0.5,hold_prob4:0.5})
summary_writer.add_summary(summary, i * total_batch + j)
acc_train.append(d)
mean_of_cross_entropy = sess.run(cross_entropy,feed_dict={x:cv_x,y_true:cv_y ,hold_prob1:1.0,hold_prob2:1.0,hold_prob3:1.0,hold_prob4:1.0})
mean_of_acc = sess.run(acc,feed_dict={x:cv_x ,y_true:cv_y ,hold_prob1:1.0,hold_prob2:1.0,hold_prob3:1.0,hold_prob4:1.0})
cross_entropy_list.append(mean_of_cross_entropy)
acc_list.append(mean_of_acc)
print(i,mean_of_cross_entropy,mean_of_acc)
saver.save(sess, "models/CNN_MC.ckpt")
print("test accuracy = ",np.mean([sess.run(acc,feed_dict={x:test_x[:230],y_true:test_y[:230] ,hold_prob1:1.0,hold_prob2:1.0,hold_prob3:1.0,hold_prob4:1.0}),sess.run(acc,feed_dict={x:test_x[230:460],y_true:test_y[230:460] ,hold_prob1:1.0,hold_prob2:1.0,hold_prob3:1.0,hold_prob4:1.0}),sess.run(acc,feed_dict={x:test_x[460:],y_true:test_y[460:] ,hold_prob1:1.0,hold_prob2:1.0,hold_prob3:1.0,hold_prob4:1.0})]))
print("cross_entropy loss = ",np.mean([sess.run(cross_entropy,feed_dict={x:test_x[:230],y_true:test_y[:230] ,hold_prob1:1.0,hold_prob2:1.0,hold_prob3:1.0,hold_prob4:1.0}),sess.run(cross_entropy,feed_dict={x:test_x[230:460],y_true:test_y[230:460] ,hold_prob1:1.0,hold_prob2:1.0,hold_prob3:1.0,hold_prob4:1.0}),sess.run(cross_entropy,feed_dict={x:test_x[460:],y_true:test_y[460:] ,hold_prob1:1.0,hold_prob2:1.0,hold_prob3:1.0,hold_prob4:1.0})]))
在上面的代码中,模型没有形成,因为出现错误,因为我得到错误 无法为形状为((?,4)''的张量'Placeholder_1:0'输入形状(0,)的值
答案 0 :(得分:0)
我想很可能是您输入的数据有误。
我将查看Y中实际包含的内容,并确保其形状为(?,4)
。在每一步中,我都会打印出Y[j:j+step_size]
。
要适合您的图形,它必须是一个包含大小为4的子数组的数组
例如[[0,0,1,0], [1,0,0,0], [0,0,0,1]]