1

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!

1

1 Answer 1

2

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})
Sign up to request clarification or add additional context in comments.

3 Comments

One observation: if you put a call to tf.slice() in the training loop, the performance can be surprisingly poor, because it adds nodes to the graph in each iteration. Instead, you should define a single tf.placeholder() for the current step, a single tf.slice() in terms of that placeholder, and feed in the value for the step (instead of the label data, which is already in the graph) in each iteration.
@mrry about that....wondering if we should just make that use-case fast rather than making the users think harder about refactoring things with placeholders
@mrry Thank you! Yes, it does seem to be running a bit slow. However, I don't understand what you mean by "... a single tf.slice() in terms of that placeholder, and feed in the value for the step (instead of the label data, which is already in the graph) in each iteration." Could you please elaborate on that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.