fn

numel

int
numel(x: Tensor)
source

Return the total number of elements in a tensor.

Equivalent to the product of the shape dimensions, returned as a plain Python int so it can be used in pure-Python control flow (loops, conditionals) without triggering autograd tracing.

Parameters

xTensor
Input tensor.

Returns

int

Total number of scalar elements: dshape[d]\prod_{d} \text{shape}[d].

Notes

For a tensor of shape (s0,s1,,sn1)(s_0, s_1, \dots, s_{n-1}):

numel(x)=i=0n1si.\text{numel}(x) = \prod_{i = 0}^{n - 1} s_i.

A 0-D (scalar) tensor has numel == 1; a tensor with any dimension of size 0 has numel == 0.

Examples

>>> import lucid
>>> x = lucid.zeros((2, 3, 4))
>>> lucid.numel(x)
24