Tensorflow(python):“ValueError:使用序列设置数组元素"在 train_step.run(...) [英] Tensorflow (python): "ValueError: setting an array element with a sequence" in train_step.run(...)

查看:50
本文介绍了Tensorflow(python):“ValueError:使用序列设置数组元素"在 train_step.run(...)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用我自己的一组图像来实现一个简单的逻辑回归模型,但是当我尝试训练模型时出现此错误:

回溯(最近一次调用最后一次):文件main.py",第 26 行,在 <module> 中.model.entrenar_modelo(sess, training_images, training_labels)文件/home/jr/Desktop/Dropbox/Machine_Learning/TF/Míos/Hip/model_log_reg.py",第 24 行,在 entrenar_modelotrain_step.run({x:batch_xs, y_:batch_ys})运行中的文件/home/jr/.local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py",第 1267 行_run_using_default_session(self, feed_dict, self.graph, session)文件/home/jr/.local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py",第 2763 行,在 _run_using_default_sessionsession.run(操作,feed_dict)文件/home/jr/.local/lib/python2.7/site-packages/tensorflow/python/client/session.py",第 334 行,运行中np_val = np.array(subfeed_val, dtype=subfeed_t.dtype.as_numpy_dtype)ValueError:使用序列设置数组元素.

我提供给 train_step.run({x: batch_xs, y_: batch_ys}) 的数据是这样的:

  • batch_xs:表示 100x100(10,000 个长张量)图像的张量对象列表
  • batch_ys:作为浮点数的标签列表(1.0 或 0.0)

我做错了什么?

编辑

问题似乎是我必须先评估 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)# 现在我可以调用函数了

即使按照以下答案中的建议进行操作,我还是遇到了几个问题.我终于通过放弃张量和使用 numpy 数组来解决所有问题.

解决方案

此特定错误来自 numpy.对具有不一致维度的序列调用 np.array 可能会抛出它.

<预><代码>>>>np.array([1,2,3,[4,5,6]])ValueError:使用序列设置数组元素.

看起来它在 tf 确保 feed_dict 的所有元素都是 numpy.array 的地方失败了.

检查您的feed_dict.

I'm trying to implement a simple logistic regression model trained with my own set of images, but I am getting this error when I try to train the model:

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.

The data I'm feeding to train_step.run({x: batch_xs, y_: batch_ys}) is like this:

  • batch_xs: list of tensor objects representing images of 100x100 (10,000 long tensors)
  • batch_ys: list of labels as floats (1.0 or 0.0)

What am I doing wrong?

Edits

It seems the problem was that I had to evaluate the tensors in batch_xs before passing them to train_step.run(...). I thought the run method would take care of that, but I guess I was wrong? Anyway, so once I did this before calling the function:

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

I had several issues even after doing what is suggested in the answers below. I finally fixed everything by ditching tensors and using numpy arrays.

解决方案

This particular error is coming out of numpy. Calling np.array on a sequence with a inconsistant dimensions can throw it.

>>> np.array([1,2,3,[4,5,6]])

ValueError: setting an array element with a sequence.

It looks like it's failing at the point where tf ensures that all the elements of the feed_dict are numpy.arrays.

Check your feed_dict.

这篇关于Tensorflow(python):“ValueError:使用序列设置数组元素"在 train_step.run(...)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆