MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / argmax

Function argmax

imperative/python/megengine/functional/math.py:530–574  ·  view source on GitHub ↗

r"""Returns the indices of the maximum values along given axis. If axis is a list of dimensions, reduce over all of them. Args: inp: input tensor. axis: dimension to reduce. If None, all dimensions will be reduced. Default: None keepdims: whether the output tenso

(
    inp: Tensor,
    axis: Optional[Union[int, Sequence[int]]] = None,
    keepdims: bool = False,
)

Source from the content-addressed store, hash-verified

528
529
530def argmax(
531 inp: Tensor,
532 axis: Optional[Union[int, Sequence[int]]] = None,
533 keepdims: bool = False,
534) -> Tensor:
535 r"""Returns the indices of the maximum values along
536 given axis. If axis is a list of dimensions,
537 reduce over all of them.
538
539 Args:
540 inp: input tensor.
541 axis: dimension to reduce. If None, all dimensions will be reduced. Default: None
542 keepdims: whether the output tensor has axis retained or not. Default: False
543
544 Returns:
545 output tensor.
546
547 Examples:
548 >>> import numpy as np
549 >>> x = Tensor(np.arange(1, 7, dtype=np.int32).reshape(2,3))
550 >>> F.argmax(x)
551 Tensor(5, dtype=int32, device=xpux:0)
552 """
553 if axis is None:
554 assert not keepdims, "can not set axis=None and keepdims=True"
555 inp = inp.flatten()
556 axis = 0
557 axis = _normalize_axis(inp.ndim, axis, reverse=True)
558
559 if isinstance(axis, collections.abc.Iterable):
560
561 for ai in axis:
562 op = builtin.Argmax(axis=ai)
563 (inp,) = apply(op, inp)
564
565 if not keepdims:
566 inp = squeeze(inp, ai)
567
568 return inp
569
570 op = builtin.Argmax(axis=axis)
571 (result,) = apply(op, inp)
572 if not keepdims:
573 result = squeeze(result, axis)
574 return result
575
576
577# sorting functions

Callers 1

topkFunction · 0.85

Calls 4

_normalize_axisFunction · 0.85
squeezeFunction · 0.85
applyFunction · 0.50
flattenMethod · 0.45

Tested by

no test coverage detected