当前位置: 移动技术网 > IT编程>脚本编程>Python > Tensorflow获取张量Tensor的具体维数实例

Tensorflow获取张量Tensor的具体维数实例

2020年03月09日  | 移动技术网IT编程  | 我要评论

穿越之采遍古代美草,l15a1,惊变20年

获取tensor的维数

>>> import tensorflow as tf

>>> tf.__version__
'1.2.0-rc1'

>>> x=tf.placeholder(dtype=float32,shape=[1,2,3,4])
>>> x=tf.placeholder(dtype=tf.float32,shape=[1,2,3,4])

>>> x.shape
tensorshape([dimension(1), dimension(2), dimension(3), dimension(4)])
>>> x.get_shape()
tensorshape([dimension(1), dimension(2), dimension(3), dimension(4)])
# 返回tuple

>>> x.shape[2]
dimension(3)
>>> x.get_shape()[2]
dimension(3)

# 获取具体维度数值
>>> x.shape[2].value
3
>>> x.get_shape()[2].value
3
# 也可以将tensorshape变量转化为list类型,然后直接按照索引取值
>>> x.shape.as_list()
[1, 2, 3, 4]
>>> x.shape.as_list()
[1, 2, 3, 4]

# 可以与int型数值比较
>>> x.shape[2] == 3
true
>>> x.get_shape()[2] == 3
true

以上这篇tensorflow获取张量tensor的具体维数实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网