TensorFlow值错误维度不兼容

2024-04-27 00:06:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个简单的程序,大部分是从关于Tensorflow的MNIST教程中复制的。我有一个二维数组118个项目长,每个子数组是13个长。第二个二维数组,长度118,每个子数组中有一个整数,包含1、2或3(第一个数组项的匹配类)

但是,每当我运行它时,就会出现各种尺寸错误。

要么ValueError: Dimensions X and X are not compatibleValueError: 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}))

Tags: andrunnonereducefortfbatchzeros