user2362956
user2362956

Reputation:

Tensorflow - ValueError: Cannot feed value of shape

I have 19 input integer features. Output and labels is 1 or 0. I examine MNIST example from tensorflow website.

My code is here :

validation_images, validation_labels, train_images, train_labels = ld.read_data_set()
print "\n"
print len(train_images[0])
print len(train_labels)

import tensorflow as tf
sess = tf.InteractiveSession()

x = tf.placeholder(tf.float32, shape=[None, 19])
y_ = tf.placeholder(tf.float32, shape=[None, 2])

W = tf.Variable(tf.zeros([19,2]))
b = tf.Variable(tf.zeros([2]))

sess.run(tf.initialize_all_variables())
y = tf.nn.softmax(tf.matmul(x,W) + b)
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)

start = 0
batch_1 = 50
end = 100

for i in range(1000):
  #batch = mnist.train.next_batch(50)
  x1 = train_images[start:end]
  y1 = train_labels[start:end]
  start = start + batch_1
  end = end + batch_1   
  x1 = np.reshape(x1, (-1, 19))
  y1 = np.reshape(y1, (-1, 2))
  train_step.run(feed_dict={x: x1[0], y_: y1[0]})

I run upper code, I get an error. The compiler says that

% (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape())))
ValueError: Cannot feed value of shape (19,) for Tensor u'Placeholder:0', which has shape '(?, 19)'

How can I handle this error?

Upvotes: 1

Views: 5686

Answers (2)

Inna
Inna

Reputation: 703

You can reshape your feed's value by the following code:

x1 = np.column_stack((x1))
x1 = np.transpose(x1)   # if necessary

Thus, the shape of the input value will be (1, 19) instead of (19,)

Upvotes: 0

Yaroslav Bulatov
Yaroslav Bulatov

Reputation: 57883

Try

train_step.run(feed_dict={x: x1, y_: y1})

Upvotes: 1

Related Questions