mindspore.ops.amax

mindspore.ops.amax(input, axis=None, keepdims=False, *, initial=None, where=None)[source]

Reduces all dimensions of a tensor by returning the maximum value in input, by default. And also can reduce a dimension of input along specified axis. keepdims determines whether the dimensions of output and input are the same.

Note

The axis with tensor type is only used for compatibility with older versions and is not recommended.

Parameters
  • input (Tensor[Number]) – The input tensor. The dtype of the tensor to be reduced is number. \((N, *)\) where \(*\) means, any number of additional dimensions.

  • axis (Union[int, tuple(int), list(int), Tensor]) – The dimensions to reduce. Default: None , reduce all dimensions. Only constant value is allowed. Assume the rank of x is r, and the value range is [-r,r).

  • keepdims (bool) – If True , keep these reduced dimensions and the length is 1. If False , don’t keep these dimensions. Default: False .

Keyword Arguments
  • initial (scalar, optional) – The minimum value of an output element. Must be present to allow computation on empty slice. Default: None .

  • where (Tensor[bool], optional) – A Tensor indicating whether to replace the primitive value in input with the value in initial. If True , do not replace, otherwise replace. For the index of True in where, the corresponding value in initial must be assigned. Default: None , which indicates True by default.

Returns

Tensor, has the same data type as input tensor.

  • If axis is None , and keepdims is False , the output is a 0-D tensor representing the product of all elements in the input tensor.

  • If axis is int, set as 1, and keepdims is False , the shape of output is \((x_0, x_2, ..., x_R)\).

  • If axis is tuple(int), set as (1, 2), and keepdims is False , the shape of output is \((x_0, x_3, ..., x_R)\).

  • If axis is 1-D Tensor, set as [1, 2], and keepdims is False , the shape of output is \((x_0, x_3, ..., x_R)\).

Raises
  • TypeError – If input is not a Tensor.

  • TypeError – If axis is not one of the following: int, tuple, list or Tensor.

  • TypeError – If keepdims is not a bool.

  • ValueError – If axis is out of range.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore
>>> import numpy as np
>>> from mindspore import Tensor, ops
>>> x = Tensor(np.random.randn(3, 4, 5, 6).astype(np.float32))
>>> output = ops.amax(x, 1, keepdims=True)
>>> result = output.shape
>>> print(result)
(3, 1, 5, 6)
>>> # case 1: Reduces a dimension by the maximum value of 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.amax(x)
>>> print(output)
9.0
>>> print(output.shape)
()
>>> # case 2: Reduces a dimension along axis 0.
>>> output = ops.amax(x, 0, True)
>>> print(output)
[[[7. 7. 7. 7. 7. 7.]
  [8. 8. 8. 8. 8. 8.]
  [9. 9. 9. 9. 9. 9.]]]
>>> # case 3: Reduces a dimension along axis 1.
>>> output = ops.amax(x, 1, True)
>>> print(output)
[[[3. 3. 3. 3. 3. 3.]]
 [[6. 6. 6. 6. 6. 6.]]
 [[9. 9. 9. 9. 9. 9.]]]
>>> # case 4: Reduces a dimension along axis 2.
>>> output = ops.amax(x, 2, True)
>>> print(output)
[[[1.]
  [2.]
  [3.]]
 [[4.]
  [5.]
  [6.]]
 [[7.]
  [8.]
  [9.]]]