我正在玩这个问题的固定代码.我收到了上述错误.谷歌搜索表明它可能是某种尺寸不匹配,虽然我的诊断没有显示任何:
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_})
Run Code Online (Sandbox Code Playgroud)
看一下尺寸,一切都很好:
y data raw (20,)
y data [20, 1]
y place holder [20, 1]
x data (20, 10)
x place holder [20, 10]
Run Code Online (Sandbox Code Playgroud)
错误:
---------------------------------------------------------------------------
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.
Run Code Online (Sandbox Code Playgroud)
任何调试技巧?
当feed_dict参数中tf.Session.run()的一个值是一个tf.Tensor对象(在本例中是结果tf.reshape())时,会引发这个非常有用的错误.
值feed_dict必须是numpy数组,或者是一些x可以使用隐式转换为numpy数组的值numpy.array(x).tf.Tensor对象不能被隐式转换,因为这样做可能需要大量工作:相反,你必须调用sess.run(t)将张量转换t为numpy数组.
正如你在答案中注意到的那样,使用了np.reshape(_y_, [-1, 1])作品,因为它产生了一个numpy数组(因为它_y_是一个刚开始的numpy数组).通常,您应该始终使用numpy和其他纯Python操作准备要提供的数据.
| 归档时间: |
|
| 查看次数: |
6113 次 |
| 最近记录: |