tensorflow: ValueError: setting an array element with a sequence

tensorflow: ValueError: setting an array element with a sequence

我正在玩 this question 的固定代码。我收到上述错误。谷歌搜索表明它可能是某种维度不匹配,尽管我的诊断没有显示任何:

with tf.Session() as sess:
    sess.run(init)

    # Fit all training data
    for epoch in range(training_epochs):
        for (_x_, _y_) in getb(train_X, train_Y):
            print("y data raw", _y_.shape )
            _y_ = tf.reshape(_y_, [-1, 1])
            print( "y data ", _y_.get_shape().as_list())
            print("y place holder", yy.get_shape().as_list())

            print("x data", _x_.shape )            
            print("x place holder", xx.get_shape().as_list() )

            sess.run(optimizer, feed_dict={xx: _x_, yy: _y_})

看尺寸,一切正常:

y data raw (20,)
y data  [20, 1]
y place holder [20, 1]

x data (20, 10)
x place holder [20, 10]

错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-131-00e0bdc140b2> in <module>()
     16             print("x place holder", xx.get_shape().as_list() )
     17 
---> 18             sess.run(optimizer, feed_dict={xx: _x_, yy: _y_})
     19 
     20 #         # Display logs per epoch step

/usr/local/lib/python3.4/dist-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict)
    355             e.args = (e.message,)
    356             raise e
--> 357           np_val = np.array(subfeed_val, dtype=subfeed_t.dtype.as_numpy_dtype)
    358           if subfeed_t.op.type == 'Placeholder':
    359             if not subfeed_t.get_shape().is_compatible_with(np_val.shape):

ValueError: setting an array element with a sequence.

有什么调试技巧吗?

用普通 numpy 替换 tf reshape 一个帮助:

        _y_ = np.reshape(_y_, [-1, 1])

实际原因尚不清楚,但它有效。

tf.Session.run() is a tf.Tensor object (in this case, the result of tf.reshape()feed_dict 参数中的一个值出现时,这不是很有帮助。

feed_dict 中的值必须是 numpy 数组,或者某些值 x 可以使用 numpy.array(x) 隐式转换为 numpy 数组。 tf.Tensor 对象不能 隐式 转换,因为这样做可能需要大量工作:相反,您必须调用 sess.run(t) 来转换张量 t到一个 numpy 数组。

正如您在回答中注意到的那样,使用 np.reshape(_y_, [-1, 1]) 是有效的,因为它会生成一个 numpy 数组(并且因为 _y_ 是一个 numpy 数组)。通常,您应该始终使用 numpy 和其他纯 Python 操作准备要馈送的数据。