[英]How to get real shape of None (dynamic input shape) in TensorFlow?
I have a placeholder which shape is [None, dimension], "None" means batch size. 我有一个占位符,其形状为[无,尺寸],“无”表示批处理大小。 I want to get the real shape of "None".
我想得到“无”的真实形状。
I try two methods when I build model: 构建模型时,我尝试了两种方法:
First, x.get_shape() and get shape as [Dimension(None), Dimension(128)] 首先,x.get_shape()并以[Dimension(None),Dimension(128)]的形式获取形状
Second, x.shape and get shape as [Dimension(None), Dimension(128)] 其次,x.shape并获得形状为[Dimension(None),Dimension(128)]
And what I want is the real shape, for example, when the batch size is 100 in this round, I would like to get [Dimension(100), Dimension(128)]. 我想要的是真实形状,例如,当本轮的批次大小为100时,我想获得[Dimension(100),Dimension(128)]。
How do I get the dynamic input shape? 如何获得动态输入形状?
I believe tf.shape
is what you are looking for. 我相信
tf.shape
是您想要的。
tf.shape(x)
can get the shape while session is running. 在会话运行时,
tf.shape(x)
可以获取形状。
The full example is below: 完整的示例如下:
import tensorflow as tf
a = tf.ones([3,4])
b = tf.shape(a)
sess=tf.Session()
print(b.eval(session=sess))
You can also use b
to init new variables. 您也可以使用
b
来初始化新变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.