jlhw
jlhw

Reputation: 245

ValueError: setting an array element with a sequence when using feed_dict in TensorFlow

I am trying to feed a Tensor containing the correct labels when I perform training.

The correct labels for the entire training dataset are contained in one tensor which has been converted from a numpy array:

numpy_label = np.zeros((614,5),dtype=np.float32)

for i  in range(614):
    numpy_label[i,label_numbers[i]-1] = 1

# Convert to tensor
y_label_all = tf.convert_to_tensor(numpy_label,dtype=tf.float32)

I have a placeholder for the correct labels for each batch:

images_per_batch = 5
y_label = tf.placeholder(tf.float32,shape=[images_per_batch,5])

During each training step, I slice the corresponding portion of y_label_all as y_ and want to feed it as y_label:

for step in range(100):

    # Slice correct labels for current batch
    y_ = tf.slice(y_label_all,[step,0],[images_per_batch,5])

    # Train
    _, loss_value = sess.run([train_step,loss],feed_dict={y_label:y_})

This generates the error:

_, loss_value = sess.run([train_step,loss],feed_dict={y_label:y_})
  File "/usr/local/lib/python2.7/dist-    packages/tensorflow/python/client/session.py", line 357, in run
np_val = np.array(subfeed_val, dtype=subfeed_t.dtype.as_numpy_dtype)
ValueError: setting an array element with a sequence.

Shape of variables y_ and y_label:

#y_: 
Tensor("Slice:0", shape=TensorShape([Dimension(5), Dimension(5)]), dtype=float32)

#y_label: 
Tensor("Placeholder:0", shape=TensorShape([Dimension(5), Dimension(5)]), dtype=float32)

I don't understand what is going wrong? Apparently it is something to do with the numpy - but now that I have converted the numpy array to a tensor, does that affect anything?

Help and insight are much appreciated. Thank you!

Upvotes: 1

Views: 1981

Answers (1)

Yaroslav Bulatov
Yaroslav Bulatov

Reputation: 57903

The problem is that that feed_dict must be compatible with numpy arrays.

Your code results in something like this

np.array(<tf.Tensor 'Slice_5:0' shape=(5, 5) dtype=float32>, dtype=np.float32)

Which fails in numpy with cryptic error above. To fix it, you need to convert your Tensor to numpy, something like below

for step in range(100):

    # Slice correct labels for current batch
    y_ = tf.slice(y_label_all,[step,0],[images_per_batch,5])
    y0 = sess.run([y_])

    # Train
    _, loss_value = sess.run([train_step,loss],feed_dict={y_label:y0})

Upvotes: 2

Related Questions