Python 如何获得张量的类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42758315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:07:33  来源:igfitidea点击:

How to get the type of a Tensor?

pythontensorflow

提问by CGTheLegend

I'm looking for something similar to the effects of:

我正在寻找类似于以下效果的东西:

x.get_shape()

that will give the type of x. Is there is any function for this?

这将给出x. 有什么功能吗?

回答by umutto

You can use get_shape()to get the shape of a tensorflow variable.

您可以使用get_shape()获取 tensorflow 变量的形状。

>>> x = tf.Variable(tf.random_normal([256, 100]))
>>> x.get_shape()
(256, 100)

You can use dtypeproperty to get the type of a tensorflow variable.

您可以使用dtype属性来获取 tensorflow 变量的类型。

>>> x = tf.Variable(tf.random_normal([256, 100]))
>>> x.dtype
<dtype: 'float32_ref'>

You can use as_numpy_dtypeproperty of dtype to convert from tf.dtypeto numpy dtype.

您可以使用dtype 的 as_numpy_dtype属性将tf.dtype转换为numpy dtype

>>> x = tf.Variable(tf.random_normal([256, 100]))
>>> x.dtype.as_numpy_dtype
<class 'numpy.float32'>

回答by Miriam Farber

To get the type you can do

要获得您可以执行的类型

x.dtype