[TensorFlow笔记] 获取Tensor的维度(tf.shape(x)、x.shape和x.get_shape()的区别)
import tensorflow as tfinput = tf.constant([[0,1,2],[3,4,5]])print(type(input.shape))print(type(input.get_shape()))print(type(tf.shape(input)))Out:<class 'tensorflow.python.framework.ten...
·
import tensorflow as tf
input = tf.constant([[0,1,2],[3,4,5]])
print(type(input.shape))
print(type(input.get_shape()))
print(type(tf.shape(input)))
Out:
<class 'tensorflow.python.framework.tensor_shape.TensorShape'>
<class 'tensorflow.python.framework.tensor_shape.TensorShape'>
<class 'tensorflow.python.framework.ops.Tensor'>
可以看到s.shape和x.get_shape()都是返回TensorShape类型对象,而tf.shape(x)返回的是Tensor类型对象。
因此要想获得维度信息,则需要调用TensorShape的ts.as_list()方法,返回的是Python的list:
input.shape.as_list() # Out: [2,3]
input.get_shape().as_list() # Out: [2,3]
此外,还可以获得维度的个数:
input.shape.ndims # Out: 2
input.get_shape().ndims # Out: 2
tf.rank(input) # Out: type=Tensor, value=2
总结
获得Python原生类型的维度信息:
input.shape.as_list() # [2,3]
input.shape.ndims # 2
获得TensorFlow中Tensor类型的维度信息:
tf.shape(input)
tf.rank(input)
「智能机器人开发者大赛」官方平台,致力于为开发者和参赛选手提供赛事技术指导、行业标准解读及团队实战案例解析;聚焦智能机器人开发全栈技术闭环,助力开发者攻克技术瓶颈,促进软硬件集成、场景应用及商业化落地的深度研讨。 加入智能机器人开发者社区iRobot Developer,与全球极客并肩突破技术边界,定义机器人开发的未来范式!
更多推荐



所有评论(0)