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

I am trying to implement a simple logistic regression model prepared using my own set of images, but I get 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 submit to train_step.run({x: batch_xs, y_: batch_ys}) looks like this:

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

What am I doing wrong? thanks in advance!

EDIT 1 : the problem is 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 this, but I think I was wrong? Anyway, as soon as 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 

EDIT 2 . I had several problems even after it was suggested in the answers below. I finally fixed everything by breaking tensors and using numpy arrays.

Hope this helps someone else.

+8
tensorflow
source share
2 answers

This particular error comes out of numpy . Calling np.array in a sequence with inconsistent dimensions may cause it.

 >>> np.array([1,2,3,[4,5,6]]) ValueError: setting an array element with a sequence. 

It doesn't seem to work at the point where tf ensures that all feed_dict elements are numpy.array s.

Check feed_dict .

+17
source share

The feed_dict argument for Operation.run() (also Session.run() and Tensor.eval() ) accepts a dictionary mapping of Tensor objects (usually tf.placeholder() ) to a numpy array (or objects that can be trivially converted to a numpy array )

In your case, you pass batch_xs , which is a list of numpy arrays, and TensorFlow does not know how to convert this to a numpy array. Say that batch_xs is defined as follows:

 batch_xs = [np.random.rand(100, 100), np.random.rand(100, 100), ..., # 29 rows omitted. np.random.rand(100, 100)] # len(batch_xs) == 32. 

We can convert batch_xs to a 32 x 100 x 100 array using the following:

 # 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) 

Note that if batch_ys is a list of floats, it will be transparently converted to an array with 1-D numpy from TensorFlow, so you will not need to convert this argument.

EDIT: mdaoust makes the right point in the comments: if you pass a list of arrays to np.array (and therefore as a value in feed_dict ), it will be automatically vstack ed, so there should be no need to convert your input, as I suggested. Instead, it looks like you have a mismatch between the forms of the list items. Try adding the following:

 assert all(x.shape == (100, 100) for x in batch_xs) 

... before calling train_step.run() , and this should show if you have a mismatch.

+5
source share

All Articles