tf.shape(a)中a 数据的类型可以是tensor, list, array,返回结果是一个tensor
a.get_shape()中a的数据类型只能是tensor,返回结果是一个元组(tuple)
import tensorflow as tf
import numpy as np
x=tf.constant([[1,2,3],[4,5,6]])
y=[[1,2,3],[4,5,6]]
z=np.arange(24).reshape([2,3,4])
sess=tf.Session()
x_shape=tf.shape(x) # x_shape 是一个tensor
y_shape=tf.shape(y) # <tf.Tensor 'Shape_2:0' shape=(2,) dtype=int32>
z_shape=tf.shape(z) # <tf.Tensor 'Shape_5:0' shape=(3,) dtype=int32>
print(x_shape) #结果:Tensor("Shape:0", shape=(2,), dtype=int32)
print(y_shape) #结果:Tensor("Shape:0", shape=(2,), dtype=int32)
print(z_shape) #结果:Tensor("Shape_2:0", shape=(3,), dtype=int32)
print(sess.run(x_shape)) # 结果:[2 3]
print(sess.run(y_shape)) # 结果:[2 3]
print(sess.run(z_shape)) # 结果:[2 3 4]
x_shape=x.shape
y_shape=y.shape
z_shape=z.shape #这句正确,只有array具有shape这个属性
print(x_shape)
print(y_shape) #这句正确,结果是(2,3,4)
print(z)
print(z_shape)
print(sess.run(x_shape))
print(sess.run(y_shape))
print(sess.run(z_shape))
x_shape=x.get_shape() #返回的是一个元组 , 不能使用print(sess.run(x_shape))
print(x_shape) #结果:(2,3)
print(x_shape[0]) #结果: 2
print(x_shape.as_list()) #结果:[2,3]
print(x_shape.as_list()[0]) #结果: 2
y_shape=y.get_shape() # AttributeError: 'list' object has no attribute 'get_shape'
z_shape=z.get_shape() # AttributeError: 'numpy.ndarray' object has no attribute 'get_shape'