mindspore.ops.prod

mindspore.ops.prod(input, axis=None, keep_dims=False)[源代码]

默认情况下,使用指定维度的所有元素的乘积代替该维度的其他元素,以移除该维度。也可仅缩小该维度大小至1。 keep_dims 控制输出和输入的维度是否相同。

参数:
  • input (Tensor[Number]) - 输入Tensor,其数据类型为数值型。shape: \((N, *)\) ,其中 \(*\) 表示任意数量的附加维度。

  • axis (Union[int, tuple(int), list(int)]) - 要减少的维度。默认值: None,缩小所有维度。只允许常量值。假设 input 的秩为r,取值范围[-r,r)。

  • keep_dims (bool) - 如果为True,则保留缩小的维度,大小为1。否则移除维度。默认值:False。

返回:

Tensor。

  • 如果 axis 为None,且 keep_dims 为False,则输出一个零维Tensor,表示输入Tensor中所有元素的乘积。

  • 如果 axis 为int,取值为1,并且 keep_dims 为False,则输出的shape为 \((input_0, input_2, ..., input_R)\)

  • 如果 axis 为tuple(int)或list(int),取值为(1, 2),并且 keep_dims 为False,则输出Tensor的shape为 \((input_0, input_3, ..., input_R)\)

异常:
  • TypeError - input 不是Tensor。

  • TypeError - axis 不是以下数据类型之一:int、Tuple或List。

  • TypeError - keep_dims 不是bool类型。

  • ValueError - axis 超出范围。

支持平台:

Ascend GPU CPU

样例:

>>> x = Tensor(np.random.randn(3, 4, 5, 6).astype(np.float32))
>>> output = ops.prod(x, 1, keep_dims=True)
>>> result = output.shape
>>> print(result)
(3, 1, 5, 6)
>>> # case 1: Reduces a dimension by multiplying all elements in the dimension.
>>> x = Tensor(np.array([[[1, 1, 1, 1, 1, 1], [2, 2, 2, 2, 2, 2], [3, 3, 3, 3, 3, 3]],
...                      [[4, 4, 4, 4, 4, 4], [5, 5, 5, 5, 5, 5], [6, 6, 6, 6, 6, 6]],
...                      [[7, 7, 7, 7, 7, 7], [8, 8, 8, 8, 8, 8], [9, 9, 9, 9, 9, 9]]]), mindspore.float32)
>>> output = ops.prod(x)
>>> print(output)
2.2833798e+33
>>> print(output.shape)
()
>>> # case 2: Reduces a dimension along axis 0.
>>> output = ops.prod(x, 0, True)
>>> print(output)
[[[ 28.  28.  28.  28.  28.  28.]
  [ 80.  80.  80.  80.  80.  80.]
  [162. 162. 162. 162. 162. 162.]]]
>>> # case 3: Reduces a dimension along axis 1.
>>> output = ops.prod(x, 1, True)
>>> print(output)
[[[  6.   6.   6.   6.   6.   6.]]
 [[120. 120. 120. 120. 120. 120.]]
 [[504. 504. 504. 504. 504. 504.]]]
>>> # case 4: Reduces a dimension along axis 2.
>>> output = ops.prod(x, 2, True)
>>> print(output)
[[[1.00000e+00]
  [6.40000e+01]
  [7.29000e+02]]
 [[4.09600e+03]
  [1.56250e+04]
  [4.66560e+04]]
 [[1.17649e+05]
  [2.62144e+05]
  [5.31441e+05]]]