prod

paddle. prod ( x, axis=None, keepdim=False, dtype=None, name=None ) [source]

Compute the product of tensor elements over the given axis.

Parameters
  • x (Tensor) – The input tensor, its data type should be float32, float64, int32, int64.

  • axis (int|list|tuple, optional) – The axis along which the product is computed. If None, multiply all elements of x and return a Tensor with a single element, otherwise must be in the range \([-x.ndim, x.ndim)\). If \(axis[i]<0\), the axis to reduce is \(x.ndim + axis[i]\). Default is None.

  • keepdim (bool, optional) – Whether to reserve the reduced dimension in the output Tensor. The result tensor will have one fewer dimension than the input unless keepdim is true. Default is False.

  • dtype (str|np.dtype, optional) – The desired date type of returned tensor, can be float32, float64, int32, int64. If specified, the input tensor is casted to dtype before operator performed. This is very useful for avoiding data type overflows. The default value is None, the dtype of output is the same as input Tensor x.

  • name (str, optional) – Name for the operation (optional, default is None). For more information, please refer to Name.

Returns

Tensor, result of product on the specified dim of input tensor.

Examples

>>> import paddle

>>> # the axis is a int element
>>> x = paddle.to_tensor([[0.2, 0.3, 0.5, 0.9],
...                       [0.1, 0.2, 0.6, 0.7]])
>>> out1 = paddle.prod(x)
>>> out1
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
0.00022680)

>>> out2 = paddle.prod(x, -1)
>>> out2
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
[0.02700000, 0.00840000])

>>> out3 = paddle.prod(x, 0)
>>> out3
Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
[0.02000000, 0.06000000, 0.30000001, 0.63000000])

>>> out4 = paddle.prod(x, 0, keepdim=True)
>>> out4
Tensor(shape=[1, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
[[0.02000000, 0.06000000, 0.30000001, 0.63000000]])

>>> out5 = paddle.prod(x, 0, dtype='int64')
>>> out5
Tensor(shape=[4], dtype=int64, place=Place(cpu), stop_gradient=True,
[0, 0, 0, 0])

>>> # the axis is list
>>> y = paddle.to_tensor([[[1.0, 2.0], [3.0, 4.0]],
...                         [[5.0, 6.0], [7.0, 8.0]]])
>>> out6 = paddle.prod(y, [0, 1])
>>> out6
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
[105., 384.])

>>> out7 = paddle.prod(y, (1, 2))
>>> out7
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
[24.  , 1680.])