2016-05-02 97 views
2

我有一个简单的程序,主要是从关于Tensorflow的MNIST教程中复制的。我有一个二维数组,长118个,每个子阵列长13个。第二个二维数组长度为118,每个子数组中有一个整数,包含1,2或3(第一个数组项的匹配类)TensorFlow ValueError尺寸不兼容

每当我运行它时,错误。

要么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

尝试转置第一个数组,即应该有13个子数组,每个118个项目以使乘法有效。 –

回答

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]) 

让我知道它解决您的问题。

相关问题