Tensorflow - ValueError:无法提供形状值

时间:2016-05-01 21:20:04

标签: tensorflow deep-learning softmax

我有19个输入整数功能。输出和标签是1或0.我从tensorflow website检查MNIST示例。

我的代码在这里:

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

我运行高级代码,我收到错误。编译器说

% (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)'

如何处理此错误?

2 个答案:

答案 0 :(得分:1)

尝试

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

答案 1 :(得分:0)

您可以通过以下代码来调整Feed的值:

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

因此,输入值的形状将是(1,19)而不是(19,)