Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using placeholder as shape in tensorflow

I'm try to define a two dimensional placeholder in tensorflow, However, I don't know the size of that in advance. Hence I define another placeholder, but it seems it doesn't work at all. Here is the minimum example:

import tensorflow as tf

batchSize = tf.placeholder(tf.int32)
input = tf.placeholder(tf.int32, [batchSize, 5])

Error message:

Traceback (most recent call last):
  File "C:/Users/v-zhaom/OneDrive/testconv/test_placeholder.py", line 5, in <module>
    input = tf.placeholder(tf.int32, [batchSize, 5])
  File "C:\Python35\lib\site-packages\tensorflow\python\ops\array_ops.py", line 1579, in placeholder
    shape = tensor_shape.as_shape(shape)
  File "C:\Python35\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 821, in as_shape
    return TensorShape(shape)
  File "C:\Python35\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 457, in __init__
    self._dims = [as_dimension(d) for d in dims_iter]
  File "C:\Python35\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 457, in <listcomp>
    self._dims = [as_dimension(d) for d in dims_iter]
  File "C:\Python35\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 378, in as_dimension
    return Dimension(value)
  File "C:\Python35\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 33, in __init__
    self._value = int(value)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'Tensor'

Then I tried to pack the shape, so I have this:

    input = tf.placeholder(tf.int32, tf.pack([batchSize, 5]))

doesn't work either:

Traceback (most recent call last):
  File "C:\Python35\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 451, in __init__
    dims_iter = iter(dims)
  File "C:\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 510, in __iter__
    raise TypeError("'Tensor' object is not iterable.")
TypeError: 'Tensor' object is not iterable.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/v-zhaom/OneDrive/testconv/test_placeholder.py", line 5, in <module>
    input = tf.placeholder(tf.int32, tf.pack([batchSize, 5]))
  File "C:\Python35\lib\site-packages\tensorflow\python\ops\array_ops.py", line 1579, in placeholder
    shape = tensor_shape.as_shape(shape)
  File "C:\Python35\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 821, in as_shape
    return TensorShape(shape)
  File "C:\Python35\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 454, in __init__
    self._dims = [as_dimension(dims)]
  File "C:\Python35\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 378, in as_dimension
    return Dimension(value)
  File "C:\Python35\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 33, in __init__
    self._value = int(value)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'Tensor'
like image 363
Zhao Avatar asked Jan 13 '17 08:01

Zhao


People also ask

What is the difference between placeholder and variable in TensorFlow?

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

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 is placeholder in Python TensorFlow?

A placeholder is a variable in Tensorflow to which data will be assigned sometime later on. It enables us to create processes or operations without the requirement for data. Data is fed into the placeholder as the session starts, and the session is run. We can feed in data into tensorflow graphs using placeholders.


1 Answers

Use None if you don't know the length in some dimension in advance, e.g.

input = tf.placeholder(tf.int32, [None, 5])

When you feed this placeholder a proper array of shape (batch_size, 5), it's dynamic shape will be set correctly, i.e.

sess.run(tf.shape(input), feed_dict={input: np.zeros(dtype=np.int32, shape=(10, 5))})

will return

array([10,  5], dtype=int32)

as expected

like image 178
standy Avatar answered Oct 24 '22 22:10

standy