我有一个简单的程序,主要是从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}))
答案 0 :(得分:1)
首先,不清楚你有多少标签(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])
让我知道它解决了您的问题。