当前位置:  开发笔记 > 编程语言 > 正文

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

如何解决《使用占位符作为张量流中的形状》经验,为你挑选了1个好方法。

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

import tensorflow as tf

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

错误信息:

Traceback (most recent call last):
  File "C:/Users/v-zhaom/OneDrive/testconv/test_placeholder.py", line 5, in 
    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 
    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'

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

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

也不起作用:

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 
    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'

standy.. 7

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

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

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

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

将返回

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

正如所料



1> standy..:

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

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

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

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

将返回

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

正如所料

推荐阅读
地之南_816
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有