Tensorflow獲取張量Tensor的具體維數例項
阿新 • • 發佈:2020-01-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,4]) >>> x.shape TensorShape([Dimension(1),Dimension(2),Dimension(3),Dimension(4)]) >>> x.get_shape() TensorShape([Dimension(1),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,4] >>> x.shape.as_list() [1,4] # 可以與int型數值比較 >>> x.shape[2] == 3 True >>> x.get_shape()[2] == 3 True
以上這篇Tensorflow獲取張量Tensor的具體維數例項就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。