1

我有 19 个输入整数特征。输出和标签是 1 或 0。我检查了来自tensorflow 网站的 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)'

我该如何处理这个错误?

4

2 回答 2

1

尝试

train_step.run(feed_dict={x: x1, y_: y1})
于 2016-05-01T21:25:25.000 回答
0

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

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

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

于 2019-03-19T01:35:09.137 回答