tensorflow中的函数获取Tensor维度的两种方法:

获取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()方法来获取维度数值。