cclloyd
cclloyd

Reputation: 9195

TensorFlow ValueError Dimensions are not compatible

I have a simple program, mostly copied from the MNIST tutorial on Tensorflow. I have a 2D array 118 items long, with each subarray being 13 long. And a 2nd 2D array that is 118 long with a single integer in each sub array, containing either 1, 2, or 3 (the matching class of the first array's item)

Whenever I run it however, I get various dimension errors.

either ValueError: Dimensions X and X are not compatible or ValueError: Incompatible shapes for broadcasting: (?, 13) and (3,) or something along those lines. I've tried most every combination of numbers I can think of here in the various places to get it to align properly but am unable to get it to.

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

Upvotes: 4

Views: 11446

Answers (1)

Sung Kim
Sung Kim

Reputation: 8536

First, it's not clear how many labels you have (3 or 13), and what is the size of input (X) vector (113 or 13)? I assume you have 13 labels, and 118 X vectors based on:

W = tf.Variable(tf.zeros([118, 13]))
y_ = tf.placeholder(tf.float32, [None, 13])

Then, you may change your code something like this:

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

Let me know it addresses your issue.

Upvotes: 1

Related Questions