mindspore.Tensor.argmax

Tensor.argmax(axis=None, keepdims=False) Tensor

Return the indices of the maximum values along the given axis of the tensor.

Parameters
  • axis (Union[int, None], optional) – Specify the axis for computation. If None , compute all elements in the tensor. Default None .

  • keepdims (bool, optional) – Whether the output tensor has dim retained. Default False .

Returns

Tensor

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore
>>> x = mindspore.tensor([[9, 3, 4, 5],
...                       [5, 2, 7, 4],
...                       [8, 1, 3, 6]])
>>> # case 1: By default, compute the maximum of all elements.
>>> x.argmax()
Tensor(shape=[], dtype=Int64, value= 0)
>>>
>>> # case 2: Compute the maximum along axis 1.
>>> x.argmax(axis=1)
Tensor(shape=[3], dtype=Int64, value= [0, 2, 0])
>>>
>>> # case 3: If keepdims=True, the output shape will be same of that of the input.
>>> x.argmax(axis=1, keepdims=True)
Tensor(shape=[3, 1], dtype=Int64, value=
[[0],
 [2],
 [0]])
Tensor.argmax(dim=None, keepdim=False) Tensor

Return the maximum values along the given dimension of the tensor.

Parameters
  • dim (Union[int, None], optional) – Specify the dim for computation. If None , compute all elements in the tensor.

  • keepdim (bool, optional) – Whether the output tensor has dim retained.

Returns

Tensor

Supported Platforms:

Ascend

Examples

>>> import mindspore
>>> x = mindspore.tensor([[9, 3, 4, 5],
...                       [5, 2, 7, 4],
...                       [8, 1, 3, 6]])
>>> # case 1: By default, compute the maximum of all elements.
>>> x.argmax()
Tensor(shape=[], dtype=Int64, value= 0)
>>>
>>> # case 2: Compute the maximum along dim 1.
>>> x.argmax(dim=1)
Tensor(shape=[3], dtype=Int64, value= [0, 2, 0])
>>>
>>> # case 3: If keepdim=True, the output shape will be same of that of the input.
>>> x.argmax(dim=1, keepdim=True)
Tensor(shape=[3, 1], dtype=Int64, value=
[[0],
 [2],
 [0]])