我正在尝试将这个( https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/3_NeuralNetworks/multilayer_perceptron.py )教程与我自己的数据一起使用,但无法使其工作。我的数据是 [1X10] 大小的向量。教程是关于 MNIST 数据的,我正在尝试为系统提供不同类型的向量。
我收到错误:
% (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape())))
ValueError: Cannot feed value of shape (0, 1) for Tensor u'Placeholder_1:0',
which has shape '(?, 2)'
错误来自batch_x和batch_y,但我不知道如何决定它们。我将感谢解决这个问题的每一个想法。谢谢
# Training cycle
for epoch in range(training_epochs):
avg_cost = 0.
total_batch = int(train_data.shape[0]/batch_size)
# Loop over all batches
for i in range(total_batch):
batch_x = train_data[:i*batch_size]
batch_y = train_labels[:i*batch_size]
np.reshape(batch_x, (-1, 10))
np.reshape(batch_y, (-1, 1))
# Run optimization op (backprop) and cost op (to get loss value)
_, c = sess.run([optimizer, cost], feed_dict={x: batch_x,
y: batch_y})
# Compute average loss
avg_cost += c / total_batch
# Display logs per epoch step
if epoch % display_step == 0:
print("Epoch:", '%04d' % (epoch+1), "cost=", \
"{:.9f}".format(avg_cost))
print("Optimization Finished!")
最佳答案
错误可能与您的 batch_y
的外观有关。看来您的输入占位符 y
期望大小为 [?, 2]
的张量(这里 ?
指的是可变大小),但您正在输入大小为 [0, 1]
的张量。虽然你的 y_batch 的第一个维度是 0 已经很奇怪了(我会检查为什么会发生这种情况),但还有一个问题是 y_batch 的第二个维度似乎是 1当它预计为 2 时 - 这可能就是您看到该错误的原因。为什么要在将 batch_y
输入到模型之前对其进行整形 (np.reshape(batch_y, (-1, 1))
)?该模型以某种形状定义输入占位符(例如 [?, 2]
),并且您在训练和测试期间必须始终遵守该形状。
关于python - 当使用 tensorflow 和我自己的数据时如何决定批量大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43219491/