tensorflow:ValueError:使用序列设置数组元素 [英] tensorflow: ValueError: setting an array element with a sequence
问题描述
我正在使用 这个问题.我收到上述错误.谷歌搜索表明这可能是某种维度不匹配,尽管我的诊断没有显示任何:
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_})
看尺寸,一切正常:
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.
任何调试技巧?
推荐答案
这个—不是很有帮助—当 feed_dict
参数中的值之一给 tf.Session.run()
是一个tf.Tensor
对象(在这种情况下,tf.reshape()
).
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()
).
feed_dict
中的值必须是 numpy 数组,或者某些值 x
可以使用 numpy.array(x)
.tf.Tensor
对象不能隐式转换,因为这样做可能需要大量工作:相反,您必须调用 sess.run(t)
将张量 t
转换为 numpy 数组.
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.
正如您在回答中所注意到的,使用 np.reshape(_y_, [-1, 1])
有效,因为它生成一个 numpy 数组(并且因为 _y_
是一个 numpy 数组开始).通常,您应该始终使用 numpy 和其他纯 Python 操作准备要馈送的数据.
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.
这篇关于tensorflow:ValueError:使用序列设置数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!