TensorShape 是tensorflow中关于张量shape的类(class).
使用示例如下:
import tensorflow.compat.v1 as tf
from tensorflow.python.framework import tensor_shape
from tensorflow.python.framework import constant_op
tensor_test1=[10,10,10]
tensor_test2 = [None,10,10]
p1 = tensor_shape.as_shape(tensor_test1) # 得到的是一个类实例,该类实例包含一个属性,是 tensor_test1 的value
const = constant_op.constant(p1.as_list())
print("type(p1) = ",type(p1))
print("p1 = ",p1) # 使用p1时会自动调用p1中的value属性
print("p1.is_fully_defined() = ",p1.is_fully_defined())# is_fully_defined 是 TensorShape 类的一个内部函数
print("p1.ndims = ",p1.ndims) # ndims 也是TensorShape的一个属性值
print("p1.as_list() = ",p1.as_list()) # 把TensorShape的value属性转换成python中的list类型
print("const = ",const)
结果如下:
type(p1) = <class 'tensorflow.python.framework.tensor_shape.TensorShape'>
p1 = (10, 10, 10)
p1.is_fully_defined() = True
p1.ndims = 3
p1.as_list() = [10, 10, 10]
const = Tensor("Const:0", shape=(3,), dtype=int32)