[英]tensorflow ValueError: Dimension 0 in both shapes must be equal
I am currently studying TensorFlow. 我目前正在研究TensorFlow。 I am trying to create a NN which can accurately assess a prediction model and assign it a score.
我正在尝试创建一个可以准确评估预测模型并为其分配分数的NN。 My plan right now is to combine scores from already existing programs run them through a mlp while comparing them to true values.
我现在的计划是将已经存在的程序中的分数通过mlp进行运行,同时将它们与真实值进行比较。 I have played around with the MNIST data and I am trying to apply what I have learnt to my project.
我一直在处理MNIST数据,并试图将学到的知识应用到项目中。 Unfortunately i have a problem
不幸的是我有一个问题
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
The code gives me this error 代码给我这个错误
ValueError: Dimension 0 in both shapes must be equal, but are 9517 and 1
This error occurs when running the line for cross_entropy. 在为cross_entropy行时,会发生此错误。 I don't understand why this is happing, if you need any more information I would be happy to give it to you.
我不明白为什么会这样,如果您需要更多信息,我很乐意为您提供。
in your case, y has shape [9517, 1] while y_ has shape [9517]. 在您的情况下,y的形状为[9517,1],而y_的形状为[9517]。 they are not campatible.
他们不适合居住。 Please try to reshape y_ using tf.reshape(y_, [-1, 1])
请尝试使用tf.reshape(y_,[-1,1])重塑y_
This was caused by the weights.hdf5 file being incompatible with the new data in the repository. 这是由于weights.hdf5文件与存储库中的新数据不兼容所致。 I have updated the repo and it should work now.
我已经更新了仓库,现在应该可以使用了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.