在将batch_xs输入x时,我 reshape 了batch_xs,因为BATCH_SIZE为1。 这是我的来源。 我不确定是什么导致了 ValueError。
with tf.name_scope("input") as scope:
x = tf.placeholder(tf.float32, shape=[1, 784])
BATCH_SIZE = 1
DROP_OUT_RATE = 0.4
EPOCH = 1
MEMORIZE = 10
accuracy_array = []
loss = tf.nn.l2_loss(y - x) / BATCH_SIZE
train_step = tf.train.AdamOptimizer(1e-4).minimize(loss)
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
mnist_list = make_mnist_train_list(55000, 10)
test_list = make_mnist_test_list(5000, 10)
sess = tf.Session()
sess.run(tf.initialize_all_variables())
for i in range(EPOCH):
for j in range(5500/BATCH_SIZE):
batch_xs = tf.reshape(mnist_list[0][j*BATCH_SIZE:j*BATCH_SIZE+1], [1, 784])
sess.run(train_step, feed_dict={x: batch_xs, keep_prob: (1.0 - DROP_OUT_RATE), r_keep_prob: (1.0 - DROP_OUT_RATE)})
if (i +1)% MEMORIZE == 0:
accuracy_array.append(loss.eval(session=sess, feed_dict={x: batch_xs, keep_prob: 1.0, r_keep_prob: 1.0}))
print(accuracy_array[ int(math.floor((i+1)/MEMORIZE -1))])
这给了我值错误,这对我来说没有意义。
ValueError:参数必须是稠密张量
最佳答案
来自文档here :
Each key in feed_dict can be one of the following types:
If the key is a Tensor, the value may be a Python scalar, string, list, or numpy ndarray that can be converted to the same dtype as that tensor. Additionally, if the key is a placeholder, the shape of the value will be checked for compatibility with the placeholder.
If the key is a SparseTensor, the value should be a SparseTensorValue.
可以用作 feed_dict
中键的“值”的类型应该是 Python 原始类型或 numpy 数组。您正在使用 tf.reshape 的结果,它是 TensorFlow 张量类型。如果您想提供重新整形的数组,您可以简单地使用np.reshape
。
关于tensorflow - 无法使用 tensorflow 创建数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35176812/