Tensorflow - ValueError:无法提供形状值

问题描述 投票:1回答:2

我有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)'

我该如何处理这个错误?

tensorflow deep-learning softmax
2个回答
1
投票

尝试

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

0
投票

您可以通过以下代码重塑Feed的值:

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

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

© www.soinside.com 2019 - 2024. All rights reserved.