简体   繁体   English

如何在TensorFlow中获得None的真实形状(动态输入形状)?

[英]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.

相关问题 tensorflow:Model 是用形状 (None, None, 6) 构造的,但它是在形状不兼容的输入上调用的 - tensorflow:Model was constructed with shape (None, None, 6), but it was called on an input with incompatible shape Tensorflow C ++ API用于动态输入形状 - Tensorflow C++ API for dynamic input shape 警告:警告:tensorflow:Model 是用形状(无,150)构造的,但它是在形状不兼容的输入上调用的(无,1) - WARNING: WARNING:tensorflow:Model was constructed with shape (None, 150) , but it was called on an input with incompatible shape (None, 1) Tensorflow:Model 是用形状 (None, 28, 28) 构造的,但它是在形状不兼容的输入上调用的 (None, 28) - Tensorflow:Model was constructed with shape (None, 28, 28) , but it was called on an input with incompatible shape (None, 28) Tensorflow: ValueError: Input 0 is incompatible with layer model: expected shape=(None, 99), found shape=(None, 3) - Tensorflow: ValueError: Input 0 is incompatible with layer model: expected shape=(None, 99), found shape=(None, 3) 如何在张量流图中得到每个节点的输入形状? - how to get the shape of the input in each node in tensorflow graph? 具有动态形状TensorFlow的变量 - Variables with dynamic shape TensorFlow python 中的神经网络:警告:tensorflow:Model 是用形状(无,7)构建的 - Neural Network in python: WARNING:tensorflow:Model was constructed with shape (None, 7) for input tensorflow占位符 - 理解`shape = [无,` - tensorflow placeholder - understanding `shape=[None,` 如何摆脱“sequential_2”层的错误输入 0 与该层不兼容:预期形状 =(None, 32, 1200),找到形状 =(None, 1148) - How to get rid Error Input 0 of layer "sequential_2" is incompatible with the layer: expected shape=(None, 32, 1200), found shape=(None, 1148)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM