feed_dict中的TensorFlow形状错误

时间:2016-03-31 20:51:42

标签: tensorflow

我正在尝试将现有的logreg示例调整为我的数据并收到以下错误:

Epoch: 0001 cost=
Traceback (most recent call last):
  File "tflin.py", line 64, in <module>
    print "Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(sess.run(cost, feed_dict={X: train_X, Y:train_Y})), \
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 315, in run
    return self._run(None, fetches, feed_dict)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 506, in _run
    % (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape())))
ValueError: Cannot feed value of shape (60000, 6) for Tensor u'Placeholder:0', which has shape '(6,)'

可在此处找到源代码:https://github.com/ilautar/tensorflow-test/blob/master/tflin.py

我确定这很明显,有什么指针吗?

谢谢你, 伊戈尔

1 个答案:

答案 0 :(得分:2)

发生错误是因为您尝试将60000 x 6矩阵提供给定义为长度为6的向量的tf.placeholder()。当您try to feed the whole train_X matrix时(相对于feeding a single row,成功了。)

完成这项工作的最佳方法是执行以下操作:

  1. 根据批量输入定义占位符(和模型),批量输入可以有不同的形状:

    # tf Graph Input
    X = tf.placeholder(tf.float32, [None, n_input])
    Y = tf.placeholder(tf.float32, [None])
    
  2. 在提供单个示例时,使用numpy.newaxis将其扩展为1 x 6矩阵:

    # Fit all training data
    for epoch in range(training_epochs):
        for (x, y) in zip(train_X, train_Y):
            sess.run(optimizer, feed_dict={X: x[numpy.newaxis, ...],
                                           Y: y[numpy.newaxis, ...]})