python - 无法为具有形状“(?,4)”的张量“Placeholder_1:0”提供形状(0,)的值
问题描述
我似乎无法解决问题,因为我是 tensorflow 的新手,我认为问题是图形不匹配,但无法解决,请帮忙。我想解决这个问题,因为我将把它用于 android 应用程序。
定义占位符
x = tf.placeholder(tf.float32,shape=[None,80,80,3])
y_true = tf.placeholder(tf.float32,shape=[None,4])
CNN 层
#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})]))
在上面的代码中,模型没有形成,因为错误发生,因为我得到的错误是 无法为张量'Placeholder_1:0'提供形状(0,)的值,它的形状为'(?,4)'
解决方案
我会说你很可能输入错误的数据。
我会查看 Y 中的实际内容,并确保它具有 形状(?,4)
。在每一步我都会打印出来Y[j:j+step_size]
。
为了适合您的图表,它必须是一个包含大小为 4 的子数组的数组,
例如[[0,0,1,0], [1,0,0,0], [0,0,0,1]]
推荐阅读
- php - 为什么在 laravel 的服务容器上使用单例方法?
- python - Selenium 找不到我的 chromedriver.exe,即使它在我的路径中
- php - 如何在 Laravel Eloquent 中正确创建引用同一张表的两个外键的关系?
- java - 带有 Groovy 的 Nifi ExecuteScript:OOM 异常
- java - 从openFrameworks端(ofApp.cpp)向android JAVA发送字符串?
- html - 尝试在不使用 CSS HTML 中的表格的情况下制作 3 列,每列都有不同的内容
- php - 表单文本区域值不可见
- javascript - 使用 axios 发布请求调用获取解析错误,我无法弄清楚
- reactjs - 为什么状态变化没有进入减速器?
- reactjs - 试图在 React 中传递一个道具,但找不到方法