tensorflow ValueError:两个形状中的尺寸0必须相等

m528fe3b  于 2023-05-29  发布在  其他
关注(0)|答案(1)|浏览(174)

我正在学习TensorFlow。我试图创建一个NN,它可以准确地评估预测模型并为其分配分数。我现在的计划是将现有程序的分数合并在一起,通过mlp运行它们,同时将它们与真实值进行比较。我已经玩了MNIST的数据,我试图将我所学到的应用到我的项目中。不幸的是我有个问题

def multilayer_perceptron(x, w1):
   # Hidden layer with RELU activation
   layer_1 = tf.matmul(x, w1)
   layer_1 = tf.nn.relu(layer_1)
   # Output layer with linear activation
   #out_layer = tf.matmul(layer_1, w2)
   return layer_1

def my_mlp (trainer, trainer_awn, learning_rate, training_epochs, n_hidden, n_input, n_output):
trX, trY= trainer, trainer_awn
#create placeholders
x = tf.placeholder(tf.float32, shape=[9517, 5])
y_ = tf.placeholder(tf.float32, shape=[9517, ])
#create initial weights
w1 = tf.Variable(tf.zeros([5, 1]))
#predicted class and loss function
y = multilayer_perceptron(x, w1)
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(y, y_))
#training
train_step = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
with tf.Session() as sess:
    # you need to initialize all variables
    sess.run(tf.initialize_all_variables())
    print("1")
    for i in range(training_epochs + 1):
        sess.run([train_step], feed_dict={x: [trX['V7'], trX['V8'], trX['V9'], trX['V10'], trX['V12']], y_: trY})
return

代码给了我这个错误

ValueError: Dimension 0 in both shapes must be equal, but are 9517 and 1

运行cross_entropy行时会出现此错误。我不明白为什么会这样,如果你需要更多的信息,我很乐意给予你。

mwngjboj

mwngjboj1#

你的y有形状[9517, 1],而y_有形状[9517]。它们不兼容。请尝试使用tf.reshape(y_, [-1, 1])重塑y_

相关问题