Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between [], [None], None and () for the shape of a placeholder?

Tags:

tensorflow

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?

like image 466
armundle Avatar asked Oct 25 '17 19:10

armundle


People also ask

What is none in TensorFlow shape?

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" .

What does None mean in shape?

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.

What is the use of placeholder in TensorFlow?

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.

What's the difference between a placeholder and a variable in TensorFlow?

placeholder is used for input data, and tf. Variable is used to store the state of data.


1 Answers

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.

like image 134
Prasad Avatar answered Oct 12 '22 23:10

Prasad