mygrad.prod#

mygrad.prod(a: ArrayLike, axis: None | int | Tuple[int, ...] = None, keepdims: bool = False, *, constant: bool | None = None) Tensor[source]#

Return the product of array elements over given axes.

Parameters:
aArrayLike

Input data.

axisOptional[int, Tuple[int, …]]

Axis or axes along which to operate. By default, flattened input is used.

keepdimsbool, optional (default=False)

If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

constantOptional[bool]

If True, this tensor is treated as a constant, and thus does not facilitate back propagation (i.e. constant.grad will always return None).

Defaults to False for float-type data. Defaults to True for integer-type data.

Integer-type tensors must be constant.

Returns:
product_along_axismygrad.Tensor

A tensor shaped as a but with the specified axis removed.

Notes

The product of an empty tensor is the neutral element 1:

>>> import mygrad
>>> mygrad.prod([])
Tensor(1.0)

Examples

By default, calculate the product of all elements:

>>> import mygrad as mg
>>> mg.prod([1.,2.])
Tensor(2.0)

Even when the input array is two-dimensional:

>>> mg.prod([[1.,2.],
...          [3.,4.]])
Tensor(24.0)

But we can also specify the axis over which to multiply:

>>> mg.prod([[1.,2.],
...          [3.,4.]], axis=1)
Tensor([  2.,  12.])