Tensorflow (python): "ValueError: setting an array element with a sequence" in train_step.run(...)
Tensorflow (python): "ValueError: setting an array element with a sequence" in train_step.run(...)
我正在尝试实现一个使用我自己的图像集训练的简单逻辑回归模型,但是当我尝试训练模型时出现此错误:
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.
我提供给 train_step.run({x: batch_xs, y_: batch_ys})
的数据是这样的:
- batch_xs:表示 100x100(10,000 个长张量)图像的张量对象列表
- batch_ys:标签列表为浮点数(1.0 或 0.0)
我做错了什么?
编辑
看来问题是我必须在将张量传递给 train_step.run(...)
之前评估 batch_xs
中的张量。我以为 运行 方法可以解决这个问题,但我想我错了?
不管怎样,一旦我在调用函数之前做了这个:
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
即使按照以下答案中的建议进行操作后,我仍然遇到了几个问题。我终于通过放弃张量和使用 numpy 数组来修复所有问题。
此特定错误来自 numpy
。在维度不一致的序列上调用 np.array
可能会抛出它。
>>> np.array([1,2,3,[4,5,6]])
ValueError: setting an array element with a sequence.
看起来它在 tf
确保 feed_dict
的所有元素都是 numpy.array
的地方失败了。
检查你的feed_dict
。
Operation.run()
(also Session.run()
and Tensor.eval()
) accepts a dictionary mapping Tensor
objects (usually tf.placeholder()
张量的 feed_dict
参数)到 numpy 数组(或可以简单地转换为 numpy 数组的对象)。
在您的例子中,您传递的是 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.
我们可以使用以下方法将 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)
请注意,如果 batch_ys
是一个浮点数列表,TensorFlow 会将其透明地转换为一维 numpy 数组,因此您不需要转换此参数。
编辑: mdaoust 在评论中提出了一个有效的观点:如果将数组列表传递给 np.array
(因此作为 [=13 中的值=]),它会自动被 vstack
ed,所以应该不需要像我建议的那样转换你的输入。相反,听起来您的列表元素的形状不匹配。尝试添加以下内容:
assert all(x.shape == (100, 100) for x in batch_xs)
...在调用 train_step.run()
之前,这应该会显示您是否存在不匹配。
我正在尝试实现一个使用我自己的图像集训练的简单逻辑回归模型,但是当我尝试训练模型时出现此错误:
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.
我提供给 train_step.run({x: batch_xs, y_: batch_ys})
的数据是这样的:
- batch_xs:表示 100x100(10,000 个长张量)图像的张量对象列表
- batch_ys:标签列表为浮点数(1.0 或 0.0)
我做错了什么?
编辑
看来问题是我必须在将张量传递给 train_step.run(...)
之前评估 batch_xs
中的张量。我以为 运行 方法可以解决这个问题,但我想我错了?
不管怎样,一旦我在调用函数之前做了这个:
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
即使按照以下答案中的建议进行操作后,我仍然遇到了几个问题。我终于通过放弃张量和使用 numpy 数组来修复所有问题。
此特定错误来自 numpy
。在维度不一致的序列上调用 np.array
可能会抛出它。
>>> np.array([1,2,3,[4,5,6]])
ValueError: setting an array element with a sequence.
看起来它在 tf
确保 feed_dict
的所有元素都是 numpy.array
的地方失败了。
检查你的feed_dict
。
Operation.run()
(also Session.run()
and Tensor.eval()
) accepts a dictionary mapping Tensor
objects (usually tf.placeholder()
张量的 feed_dict
参数)到 numpy 数组(或可以简单地转换为 numpy 数组的对象)。
在您的例子中,您传递的是 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.
我们可以使用以下方法将 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)
请注意,如果 batch_ys
是一个浮点数列表,TensorFlow 会将其透明地转换为一维 numpy 数组,因此您不需要转换此参数。
编辑: mdaoust 在评论中提出了一个有效的观点:如果将数组列表传递给 np.array
(因此作为 [=13 中的值=]),它会自动被 vstack
ed,所以应该不需要像我建议的那样转换你的输入。相反,听起来您的列表元素的形状不匹配。尝试添加以下内容:
assert all(x.shape == (100, 100) for x in batch_xs)
...在调用 train_step.run()
之前,这应该会显示您是否存在不匹配。