简体   繁体   English

如何使用“ tf.shape()”获得“静态”形状?

[英]How to get 'static' shape using 'tf.shape()'?

I am just trying to get the static shape of a tensor using command 'tf.shape()'. 我只是想使用命令'tf.shape()'获得张量的静态形状。 I noticed that we could use 'x.get_shape' to obtain the static shape, however, in my code, for training, the shape is static, for evaluation, the shape of tensor is dynamic 'None'-so that I am not sure if I also could get static shape by using 'tf.shape()' as well. 我注意到我们可以使用'x.get_shape'获得静态形状,但是,在我的代码中,用于训练的形状是静态的,用于评估,张量的形状是动态的'None'-因此我不确定如果我也可以通过使用'tf.shape()'获得静态形状。

The shape of 'deconv4.outputs' is [16,256,256,3], however, when I use 'tf.shape(deconv4.outputs)[0]', the result is: 'Tensor("FRUC_DVF_g/strided_slice_2:0", shape=(), dtype=int32, device=/device:GPU:0)', not '16'. 'deconv4.outputs'的形状为[16,256,256,3],但是,当我使用'tf.shape(deconv4.outputs)[0]'时,结果为:'Tensor(“ FRUC_DVF_g / strided_slice_2:0”,shape =(),dtype = int32,device = / device:GPU:0)',而不是'16'。

Could you please help me with this? 你能帮我这个忙吗?

Thanks in advance! 提前致谢!

I have got the output of the model called 'deconv4.outputs' with the shape of [16,256,256,3]. 我得到了名为[deconv4.outputs]的模型,其输出形状为[16,256,256,3]。 I want to obtain the first dimension of 'deconv4.outputs' using 'tf.shape()' command. 我想使用“ tf.shape()”命令获取“ deconv4.outputs”的第一维。

flow = tf.slice(deconv4.outputs, [0,0,0,0], [tf.shape(deconv4.outputs)[0], 256, 256, 2])

The expected result is that the 'tf.shape(deconv4.outputs)[0]' returns '16'. 预期结果是“ tf.shape(deconv4.outputs)[0]”返回“ 16”。

tf.shape(), should return 16, when running under a session with appropriate inputs. 在具有适当输入的会话下运行时,tf.shape()应该返回16。

Anything which is dynamic, if you specify None at the input, without running it under a session you would get (). 任何动态的东西,如果您在输入中指定None而不在会话中运行它,则会得到()。

Try your code with eager_execution_enabled if possible. 如果可能,请尝试使用eager_execution_enabled启用代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 tf.shape() 在张量流中得到错误的形状 - tf.shape() get wrong shape in tensorflow 类型错误:tf.shape 不是 function - TypeError: tf.shape is not a function 在 tf.function 中,如何将 tf.shape() 获得的动态张量形状转换为 python 值而不是张量本身? - In tf.function, how to convert dynamic tensor shape getted by tf.shape() to python values but not tensors itself? 我如何在Tensorflow中调整未知尺寸的图像的大小(tf.shape(input)方法不起作用) - How do I resize image with unknown size in Tensorflow(tf.shape(input) method doesn't work) tensorflow 2.0 中的 x.shape 和 tf.shape() 有什么区别? - What is the difference between x.shape and tf.shape() in tensorflow 2.0? 如何将依赖于占位符的形状传递给 tf.get_variable()? - How to pass a shape that is dependent on a placeholder to tf.get_variable()? TF Keras 加载 model 时如何获得预期的输入形状? - TF Keras how to get expected input shape when loading a model? tf.data.dataset:如何将形状分配给保证 output 特定形状的数据集(形状未定义)? - tf.data.dataset: How do I assign shape to a dataset (with shape undefined) that is guaranteed to output certain shape? 如何动态地用形状做 tf.reshape? - How to do tf.reshape with shape dynamiclly? 如何获取TOCO tf_convert的冻结Tensorflow模型的input_shape - How do i get the input_shape of a frozen Tensorflow-Model for TOCO tf_convert
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM