1. 程式人生 > >Tensorflow獲取張量Tensor的具體維數

Tensorflow獲取張量Tensor的具體維數

獲取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