mat*_*tes 17 python-2.7 tensorflow
我正在尝试使用我自己的一组图像来训练一个简单的逻辑回归模型,但是当我尝试训练模型时,我收到了这个错误:
Traceback (most recent call last):
File "main.py", line 26, in <module>
model.entrenar_modelo(sess, training_images, training_labels)
File "/home/jr/Desktop/Dropbox/Machine_Learning/TF/Míos/Hip/model_log_reg.py", line 24, in entrenar_modelo
train_step.run({x: batch_xs, y_: batch_ys})
File "/home/jr/.local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1267, in run
_run_using_default_session(self, feed_dict, self.graph, session)
File "/home/jr/.local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2763, in _run_using_default_session
session.run(operation, feed_dict)
File "/home/jr/.local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 334, in run
np_val = np.array(subfeed_val, dtype=subfeed_t.dtype.as_numpy_dtype)
ValueError: setting an array element with a sequence.
Run Code Online (Sandbox Code Playgroud)
我要喂的数据train_step.run({x: batch_xs, y_: batch_ys})
是这样的:
我究竟做错了什么?Thx提前!
编辑1:它接缝问题是我必须batch_xs
在传递它们之前评估张量train_step.run(...)
.我认为run方法可以解决这个问题,但我想我错了?无论如何,所以一旦我在调用函数之前这样做了:
for i, x in enumerate(batch_xs):
batch_xs[i] = x.eval()
#print batch_xs[i].shape
#assert all(x.shape == (100, 100, 3) for x in batch_xs)
# Now I can call the function
Run Code Online (Sandbox Code Playgroud)
编辑2:即使按照以下答案中的建议,我也有几个问题.我终于通过抛弃张量并使用numpy数组来修复所有内容.
希望这有助于其他人
mda*_*ust 33
这个特殊的错误正在出现numpy
.调用np.array
具有不一致尺寸的序列可以抛出它.
>>> np.array([1,2,3,[4,5,6]])
ValueError: setting an array element with a sequence.
Run Code Online (Sandbox Code Playgroud)
看起来它确实失败了,tf
确保了所有元素feed_dict
都是numpy.array
s.
检查你的feed_dict
.
(也和)的feed_dict
参数接受一个字典映射对象(通常是张量)到一个numpy数组(或者可以简单地转换为numpy数组的对象).Operation.run()
Session.run()
Tensor.eval()
Tensor
tf.placeholder()
在你的情况下,你传递batch_xs
,这是一个numpy数组的列表,TensorFlow不知道如何将其转换为numpy数组.假设这个batch_xs
定义如下:
batch_xs = [np.random.rand(100, 100),
np.random.rand(100, 100),
..., # 29 rows omitted.
np.random.rand(100, 100)] # len(batch_xs) == 32.
Run Code Online (Sandbox Code Playgroud)
我们可以使用以下代码转换batch_xs
为32 x 100 x 100
数组:
# Convert each 100 x 100 element to 1 x 100 x 100, then vstack to concatenate.
batch_xs = np.vstack([np.expand_dims(x, 0) for x in batch_xs])
print batch_xs.shape
# ==> (32, 100, 100)
Run Code Online (Sandbox Code Playgroud)
请注意,如果batch_ys
是浮点数列表,它将由TensorFlow透明地转换为1-D numpy数组,因此您不需要转换此参数.
编辑: mdaoust在注释中提出了一个有效点:如果你将数组列表传递给np.array
(因此作为a中的值feed_dict
),它将自动vstack
编辑,因此不需要按照我的建议转换输入.相反,听起来你的列表元素的形状不匹配.尝试添加以下内容:
assert all(x.shape == (100, 100) for x in batch_xs)
Run Code Online (Sandbox Code Playgroud)
...在致电之前train_step.run()
,这应该揭示你是否有不匹配.
归档时间: |
|
查看次数: |
33101 次 |
最近记录: |