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)
# 0.0002268

out2 = paddle.prod(x, -1)
# [0.027  0.0084]

out3 = paddle.prod(x, 0)
# [0.02 0.06 0.3  0.63]

out4 = paddle.prod(x, 0, keepdim=True)
# [[0.02 0.06 0.3  0.63]]

out5 = paddle.prod(x, 0, dtype='int64')
# [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])
# [105. 384.]

out7 = paddle.prod(y, (1, 2))
# [  24. 1680.]