今天学习图像数组转置的时候,遇到get_shape()、num_elements()函数。
经查,get_shape()函数的功能和array的shape属性一样,都是可以得到张量(数组)的维度,如下是TesnorFlow的get_shape()源码(..\Lib\site-packages\tensorflow\python\ops\variables.py):
def get_shape(self):
"""Alias of Variable.shape."""
return self.shape
num_elements()函数返回数组的总元素个数,具体是通过数组各维度的乘积实现的,源码如下(..\Lib\site-packages\tensorflow\python\framework\tensor_shape.py):
def num_elements(self):
"""Returns the total number of elements, or none for incomplete shapes."""
if self.is_fully_defined():
size = 1
for dim in self._dims:
size *= dim.value
return size
else:
return None
好了,上代码,如下是做的一个验证:
import tensorflow as tf
a = tf.zeros([3, 4, 5, 6], tf.int32)
print(a)
print('----------')
b = a.get_shape()
e = a.shape
c = b[1:4].num_elements()
d = tf.reshape(a, [-1, c])
init_op = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init_op)
print(a.eval())
print('----------')
print(b)
print('----------')
print(e)
print('----------')
print(c)
print('----------')
print(d.eval())
结果:
'''
a为维度为(3, 4, 5, 6)的张量
'''
Tensor("zeros_11:0", shape=(3, 4, 5, 6), dtype=int32)
----------
'''
a = tf.zeros([3, 4, 5, 6], tf.int32)
维度为(3, 4, 5, 6)的全零张量
'''
[[[[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]]
[[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]]
[[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]]
[[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]]]
[[[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]]
[[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]]
[[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]]
[[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]]]
[[[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]]
[[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]]
[[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]]
[[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]]]]
----------
'''
b = a.get_shape()
返回a的维度
'''
(3, 4, 5, 6)
----------
'''
e = a.shape
类似于a.get_shape()
'''
(3, 4, 5, 6)
----------
'''
b[1:4]
对(3, 4, 5, 6)数组进行切片。可以取到索引1,不可以取到索引4;
.num_elements()
返回切片后的数组总元素数,4 * 5 * 6 = 120
'''
120
----------
'''
d = tf.reshape(a, [-1, c])
按照[-1, 120]的维度,改变a张量的形状。
a张量一共3 * 4 * 5 * 6 = 360个元素,所以这里的“-1”代表3,
重新生成的d的形状应该是(3, 120)
'''
[[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0]]