- len():返回对象的长度,如list、dict等,注意不是length()函数
len([1,2,3,4,5])# 返回值是5
- count():计算包含对象个数
[1,2,3,1].count(1)# 返回值为2
size、shape、get_shape是其他模块中的函数,如:
numpy中:
- size():计算数组和矩阵所有数据的个数
- shape ():得到矩阵每维的大小
a = np.array([[1,2,3],[4,5,6]])
np.size(a)# 返回值为 6
np.size(a,1)# 返回值为 3
np.shape(a)# 返回值为(2,3)
tfrecord中:
- tf.shape()和get_shape()都可以得到tensor的尺寸
- tf.shape(a)中a的数据类型可以是tensor, list, array
- a.get_shape()中a的数据类型只能是tensor,且返回的是一个元组(tuple)
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 sess.run(x_shape) # 结果:[2 3]
print sess.run(y_shape) # 结果:[2 3]
print sess.run(z_shape) # 结果:[2 3 4]
x_shape=x.get_shape() # 返回的是TensorShape([Dimension(2), Dimension(3)]),不能使用sess.run() 因为返回的不是tensor或string,而是元组
x_shape=x.get_shape().as_list() # 可以使用 as_list()得到具体的尺寸,x_shape=[2 3]
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'