I have seen pieces of code using either []
, [None]
, None
or ()
as the shape for a placeholder
, that is
x = tf.placeholder(..., shape=[], ...) y = tf.placeholder(..., shape=[None], ...) z = tf.placeholder(..., shape=None, ...) w = tf.placeholder(..., shape=(), ...)
What's the difference between these?
e.g. TensorShape([None, 256]) Unknown shape: has an unknown number of dimensions, and an unknown size in all dimensions. e.g. TensorShape(None) If a tensor is produced by an operation of type "Foo" , its shape may be inferred if there is a registered shape function for "Foo" .
A None value in the shape of a tensor means that the tensor can be of any size (large than or equal to 1) in that dimension.
A placeholder is simply a variable that we will assign data to at a later date. It allows us to create our operations and build our computation graph, without needing the data. In TensorFlow terminology, we then feed data into the graph through these placeholders.
placeholder is used for input data, and tf. Variable is used to store the state of data.
TensorFlow uses arrays rather than tuples. It converts tuples to arrays. Therefore []
and ()
are equivalent.
Now, consider this code example:
x = tf.placeholder(dtype=tf.int32, shape=[], name="foo1") y = tf.placeholder(dtype=tf.int32, shape=[None], name="foo2") z = tf.placeholder(dtype=tf.int32, shape=None, name="foo3") val1 = np.array((1, 2, 3)) val2 = 45 with tf.Session() as sess: sess.run(tf.global_variables_initializer()) #print(sess.run(x, feed_dict = {x: val1})) # Fails print(sess.run(y, feed_dict = {y: val1})) print(sess.run(z, feed_dict = {z: val1})) print(sess.run(x, feed_dict = {x: val2})) #print(sess.run(y, feed_dict = {y: val2})) # Fails print(sess.run(z, feed_dict = {z: val2}))
As can be seen, placeholder with []
shape takes a single scalar value directly. Placeholder with [None]
shape takes a 1-dimensional array and placeholder with None
shape can take in any value while computation takes place.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With