[英]TensorFlow ValueError Dimensions are not compatible
我有一个简单的程序,主要是从Tensorflow上的MNIST教程中复制而来。 我有一个长度为118个项目的2D数组,每个子数组长13个。 第二个2D数组,长118个,每个子数组中有一个整数,包含1,2或3(第一个数组项的匹配类)
每当我运行它时,我会遇到各种尺寸错误。
ValueError: Dimensions X and X are not compatible
或ValueError: Incompatible shapes for broadcasting: (?, 13) and (3,)
或沿着这些线的东西。 我已经尝试了大多数我能在各个地方想到的数字组合,以使它正确对齐,但我无法得到它。
x = tf.placeholder(tf.float32, [None, 13])
W = tf.Variable(tf.zeros([118, 13]))
b = tf.Variable(tf.zeros([3]))
y = tf.nn.softmax(tf.matmul(x, W) + b)
y_ = tf.placeholder(tf.float32, [None, 13])
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
for i in range(1000):
batch_xs = npWineList
batch_ys = npWineClass
sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
首先,不清楚你有多少标签(3或13),输入(X)矢量(113或13)的大小是多少? 我假设你有13个标签,118个X向量基于:
W = tf.Variable(tf.zeros([118, 13]))
y_ = tf.placeholder(tf.float32, [None, 13])
然后,您可以更改您的代码:
x = tf.placeholder(tf.float32, [None, 118])
W = tf.Variable(tf.zeros([118, 13]))
b = tf.Variable(tf.zeros([13]))
y = tf.nn.softmax(tf.matmul(x, W) + b)
y_ = tf.placeholder(tf.float32, [None, 13])
让我知道它解决了您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.