tensorflow: ValueError: setting an array element with a sequence

匿名 (未验证) 提交于 2019-12-03 02:35:01

问题:

I am playing with the fixed code from this question. I am getting the above error. Googling suggests it might be some kind of dimension mismatch, though my diagnostics does not show any:

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_}) 

Looking at the dimensions, everything is alright:

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

Error:

--------------------------------------------------------------------------- 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. 

Any debugging tips?

回答1:

This―not very helpful―error is raised when one of the values in the feed_dict argument to tf.Session.run() is a tf.Tensor object (in this case, the result of tf.reshape()).

The values in feed_dict must be numpy arrays, or some value x that can be implicitly converted to a numpy array using numpy.array(x). tf.Tensor objects cannot be implicitly converted, because doing so might require a lot of work: instead you have to call sess.run(t) to convert a tensor t to a numpy array.

As you noticed in your answer, using np.reshape(_y_, [-1, 1]) works, because it produces a numpy array (and because _y_ is a numpy array to begin with). In general, you should always prepare data to be fed using numpy and other pure-Python operations.



回答2:

replacing tf reshape with plain numpy one helped:

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

the actual reason why is still unclear, but it works.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!