Cannot convert a partially converted tensor in TensorFlow

Each Answer to this Q is separated by one/two green lines.

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?

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.


The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .