Reputation: 2193
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'
Upvotes: 8
Views: 2987
Reputation: 1065
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
Upvotes: 7