使用占位符作为张量流中的形状

Zha*_*hao 8 python tensorflow

我试着在tensorflow中定义一个二维占位符,但是,我事先并不知道它的大小.因此我定义了另一个占位符,但它似乎根本不起作用.这是最小的例子:

import tensorflow as tf

batchSize = tf.placeholder(tf.int32)
input = tf.placeholder(tf.int32, [batchSize, 5])
Run Code Online (Sandbox Code Playgroud)

错误信息:

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'
Run Code Online (Sandbox Code Playgroud)

然后我试着打包形状,所以我有这个:

    input = tf.placeholder(tf.int32, tf.pack([batchSize, 5]))
Run Code Online (Sandbox Code Playgroud)

也不起作用:

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'
Run Code Online (Sandbox Code Playgroud)

sta*_*ndy 7

None如果您事先不知道某个尺寸的长度,请使用,例如

input = tf.placeholder(tf.int32, [None, 5])
Run Code Online (Sandbox Code Playgroud)

当您为此占位符提供适当的形状数组(batch_size,5)时,它的动态形状将被正确设置,即

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

将返回

array([10,  5], dtype=int32)
Run Code Online (Sandbox Code Playgroud)

正如所料