无法为具有形状“(”的张量“占位符\u 1:0”馈送形状(0,)的值?, 4)'

2024-10-05 10:21:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我似乎无法解决的问题,因为我是新的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})]))

在上面的代码中,当模型没有形成时,错误就发生了,当我得到错误时 无法为具有shape'(?,4)


Tags: runtestlayertruetffulldropoutsess
1条回答
网友
1楼 · 发布于 2024-10-05 10:21:54

我认为很可能你输入的数据是错误的。
我会看看Y中的实际值,并确保它具有(?,4)的形状。在每一步我都会打印出Y[j:j+step_size]。你知道吗

要适合您的图形,它必须是一个包含大小为4的子数组的数组
e、 g[[0,0,1,0], [1,0,0,0], [0,0,0,1]]

相关问题 更多 >

    热门问题