Cannot convert a partially converted tensor in TensorFlow

Question:

There are many methods in TensorFlow that requires specifying a shape, for example truncated_normal:

tf.truncated_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)

I have a placeholder for the input of shape [None, 784], where the first dimension is None because the batch size can vary. I could use a fixed batch size but it still would be different from the test/validation set size.

I cannot feed this placeholder to tf.truncated_normal because it requires a fully specified tensor shape. What is a simple way to having tf.truncated_normal accept different tensor shapes?

Asked By: Blackecho

||

Answers:

You just need to feed it in as a single example but in the batched shape. So that means adding an extra dimension to the shape e.g.

batch_size = 32 # set this to the actual size of your batch
tf.truncated_normal((batch_size, 784), mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)

This way it will “fit” into the placeholder.

If you expect batch_size to change you can also use:

tf.truncated_normal(tf.shape(input_tensor), mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)

Where input_tensor could be a placeholder or just whatever tensor is going to have this noise added to it.

Answered By: Daniel Slater
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.