獲取Tensor維度的兩種方法:
Tensor.get_shape()
返回TensorShape對象,
如果需要確定的數值而把TensorShape當作list使用,肯定是不行的。
需要調用TensorShape的as_list()方法,
需要調用TensorShape.as_list()方法來獲取維度數值。
來實踐一下:
import tensorflow as tf a = tf.zeros(shape=[10,20]) b = a.get_shape() c = b.as_list() print(b) print(c)
輸出結果:
(10, 20) [10, 20]
Tensorflow.shape()
返回Tensor對象,需要調用Session.run()方法來獲取維度數值。