18

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.

Jan Schultke
  • 17,446
  • 6
  • 47
  • 96
mathetes
  • 11,766
  • 7
  • 25
  • 32

2 Answers2

36

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.

mdaoust
  • 6,242
  • 3
  • 28
  • 29
10

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

In your case, you are passing batch_xs, which is a list of numpy arrays, and TensorFlow does not know how to convert this to a numpy array. Let's 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 into 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, this will be transparently converted into a 1-D numpy array by TensorFlow, so you should not need to convert this argument.

EDIT: mdaoust makes a valid point in the comments: If you pass a list of arrays into np.array (and therefore as the value in a feed_dict), it will automatically be vstacked, so there should be no need to convert your input as I suggested. Instead, it sounds like you have a mismatch between the shapes of your list elements. Try adding the following:

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

...before the call to train_step.run(), and this should reveal whether you have a mismatch.

mrry
  • 125,488
  • 26
  • 399
  • 400
  • np auto-stacks array-lists like this, so I'm still betting on size a inconsistency. `p=tf.placeholder(tf.float32,[2,10,10]);` `q = tf.identity(p);` `q.eval(feed_dict={p:[np.random.randn(10,10),np.random.randn(10,10)]})‌​.shape #==> (2, 10, 10) ` – mdaoust Dec 08 '15 at 18:04
  • 1
    @mrry Is there any way to feed TF tensors to `feed_dict`? As you know I read_and_decode images purely in TF and now having the same problem when feeding – Hamed MP Dec 08 '15 at 18:21
  • @HamedMP: There's currently no way to feed a (symbolic) `Tensor` as the value in a `feed_dict`. The three main choices here are: (i) evaluate the tensor and pass its value, (ii) construct the graph so that you use the value of the `Tensor` in the original expression, or (iii) use a "queue" as an indirection, evaluate the `Tensor` and enqueue it; then define the original expression as a function of `dequeue` rather than a placeholder. – mrry Dec 09 '15 at 07:56
  • Thanks @mrry, I'm using 1st method. The data is images and tensor images are shown correctly in the tensroboard, but when I evaluate them the become corrupted and neither train nor validation improves from 10% (worse than my pervious implementation with tf.Variable where at least train was improving.) I even tried casting to np.uin32, transposing channels,.. but none worked – Hamed MP Dec 09 '15 at 09:58
  • @HamedMP: It would probably be best to move this to a new question. Can you post details of what you've tried? – mrry Dec 09 '15 at 10:01
  • @mrry As it's project and has a lot of data to run it, I sent the previous version by email. I will send the new version to your email – Hamed MP Dec 09 '15 at 14:18
  • @mrry i have the same problem, i tried everything in stack. – keramat Sep 29 '19 at 20:43