ValueError:无法为张量为'(?,12)'的张量'Placeholder_1:0'输入形状(50,10)的值

时间:2019-07-17 07:08:13

标签: python python-3.x tensorflow

好吧,我正在尝试运行以下具有cnn模型的python代码。 请帮助我找出错误。

# Create the model
# placeholder
x = tf.compat.v1.placeholder(tf.float32, [None,784])
y_= tf.compat.v1.placeholder(tf.float32,[None, CLASS_NUM])

# first convolutinal layer
w_conv1 = weight_variable([1, 25, 1, 32])
b_conv1 = bias_variable([32])

x_image = tf.reshape(x, [-1, 1, 784, 1])

h_conv1 = tf.nn.relu(conv2d(x_image, w_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)

# second convolutional layer
w_conv2 = weight_variable([1, 25, 32, 64])
b_conv2 = bias_variable([64])

h_conv2 = tf.nn.relu(conv2d(h_pool1, w_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)

# densely connected layer
w_fc1 = weight_variable([1*88*64, 1024])
b_fc1 = bias_variable([1024])

h_pool2_flat = tf.reshape(h_pool2, [-1, 1*88*64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, w_fc1) + b_fc1)

# dropout
keep_prob = tf.compat.v1.placeholder("float")
h_fc1_drop = tf.nn.dropout(h_fc1, rate=1 - keep_prob)

# readout layer
w_fc2 = weight_variable([1024, CLASS_NUM])
b_fc2 = bias_variable([CLASS_NUM])

y_conv=tf.compat.v1.nn.softmax(tf.matmul(h_fc1_drop, w_fc2) + b_fc2)

# define var&op of training&testing
actual_label = tf.argmax(y_, 1)
label,idx,count = tf.unique_with_counts(actual_label)
cross_entropy = -tf.reduce_sum(y_*tf.log(y_conv))
train_step = tf.train.GradientDescentOptimizer(1e-4).minimize(cross_entropy)                     
predict_label = tf.argmax(y_conv, 1)
label_p,idx_p,count_p = tf.unique_with_counts(predict_label)
correct_prediction = tf.equal(predict_label, actual_label)
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
correct_label=tf.boolean_mask(actual_label,correct_prediction)
label_c,idx_c,count_c=tf.unique_with_counts(correct_label)

# if model exists: restore it
# else: train a new model and save it
saver = tf.train.Saver()
model_name = "model_" + str(CLASS_NUM) + "class_" + folder
model =  model_name + '/' + model_name + ".ckpt"
if not os.path.exists(model + ".meta"):
   sess.run(tf.global_variables_initializer())
   if not os.path.exists(model_name):
      os.makedirs(model_name)
      for i in range(int(TRAIN_ROUND)+1):
         batch = mnist.train.next_batch(50)
         if i%100 == 0:
            train_accuracy = accuracy.eval(feed_dict={x:batch[0],   y_:batch[1], keep_prob:1.0})
            s = "step %d, train accuracy %g" %(i, train_accuracy)
            print(s)
            train_step.run(feed_dict={x:batch[0], y_:batch[1], keep_prob:0.5})

        save_path = saver.save(sess, model)
        print("Model saved in file:", save_path)
else:     
        saver.restore(sess, model)
        print("Model restored: " + model)

好吧,“ train_accuracy = precision.eval(feed_dict = {x:batch [0],y_:batch [1],keep_prob:1.0})”这一行触发了错误。 这是弹出的错误: ValueError:无法输入形状(50,10)的值 Tensor'Placeholder_1:0',其形状为'(?,12)'

1 个答案:

答案 0 :(得分:0)

该错误表明您正在尝试将形状为(50,10)的张量馈入形状为(?,12)的占位符张量中。 ?表示形状可以是任何形状。 给定您发布的代码,我猜想尝试填充y_时会发生错误,它期望张量为(?,num_classes),并且我想您拥有的类为12。

不幸的是,我认为您为我们提供的信息很少,可以帮助您:您可以在尝试运行的内容中添加一些其他日志或代码部分吗?