Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tensorflow (python): "ValueError: setting an array element with a sequence" in train_step.run(...)

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.

like image 754
mathetes Avatar asked Dec 08 '15 13:12

mathetes


1 Answers

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.

like image 159
mdaoust Avatar answered Sep 25 '22 12:09

mdaoust