How does a tensor shape determine its element count?

prod multiplies its factors. The product of a rectangular array's axis lengths gives its logical element count.

numpy.prod in practice

Shape (4,3,2) encodes four groups of three pairs, or 24 entries. Multiply by item size for a byte estimate and account for temporary buffers separately.

import numpy as np
np.prod([4,3,2])

Result 24

中文

张量形状怎样换算成元素总数?

prod 将因子连乘。矩形数组的轴长相乘等于逻辑元素个数。

numpy.prod 算例

形状 (4,3,2) 表示四组、每组三对数,共 24 项。估算字节数还要乘以每个元素的大小,并单独考虑额外缓冲区。

import numpy as np
np.prod([4,3,2])

结果 24

References / 参考来源

Keep reading / 继续阅读