tf.shape(tensor)和tensor.shape()的区别

#tf.shape(tensor)和tensor.shape()的区别

a=tf.zeros([4,5,4,5,6])
print(type(a.shape))
print(a.shape.ndims)#多少个维度
print(a.shape.as_list())#返回列表
print(type(tf.shape(a)))
print(type(tf.shape(a)[0]))
b=a.shape.as_list()
c=tf.shape(a)
b[1]=tf.shape(a)[1]
print(b)
sess=tf.Session()
d=sess.run(c)
print(d)
outputs:
<class 'tensorflow.python.framework.tensor_shape.TensorShape'>
5
[4, 5, 4, 5, 6]
<class 'tensorflow.python.framework.ops.Tensor'>
<class 'tensorflow.python.framework.ops.Tensor'>
[4, <tf.Tensor 'strided_slice_1:0' shape=() dtype=int32>, 4, 5, 6]
[4 5 4 5 6]

其中tf.shape(tensor)使用的是动态的,即必须要在session中运行后才能显示出来,但是tensor.shape()是静态的,即通过定义的shape可以惊天的运行出来。
原因:在我们定义的时候,比如进行placeholder的时候我们可能会定义某些维度为None,在静态的时候是看不出来的,只能在运行的时候找到维度。
**使用:**可以在获得某些tensor的维度的时候进行检验,防止维度为None。

你可能感兴趣的:(TensorFlow)