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

Function argmin

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

r"""Returns the indices of the minimum 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

481
482
483def argmin(
484 inp: Tensor,
485 axis: Optional[Union[int, Sequence[int]]] = None,
486 keepdims: bool = False,
487) -> Tensor:
488 r"""Returns the indices of the minimum values along
489 given axis. If axis is a list of dimensions,
490 reduce over all of them.
491
492 Args:
493 inp: input tensor.
494 axis: dimension to reduce. If None, all dimensions will be reduced. Default: None
495 keepdims: whether the output tensor has axis retained or not. Default: False
496
497 Returns:
498 output tensor.
499
500 Examples:
501 >>> import numpy as np
502 >>> x = Tensor(np.arange(1, 7, dtype=np.int32).reshape(2,3))
503 >>> F.argmin(x)
504 Tensor(0, dtype=int32, device=xpux:0)
505 """
506 if axis is None:
507 assert not keepdims, "can not set axis=None and keepdims=True"
508 inp = inp.flatten()
509 axis = 0
510
511 axis = _normalize_axis(inp.ndim, axis, reverse=True)
512 if isinstance(axis, collections.abc.Iterable):
513
514 for ai in axis:
515 op = builtin.Argmin(axis=ai)
516 (inp,) = apply(op, inp)
517
518 if not keepdims:
519 inp = squeeze(inp, ai)
520
521 return inp
522
523 op = builtin.Argmin(axis=axis)
524 (result,) = apply(op, inp)
525 if not keepdims:
526 result = squeeze(result, axis)
527 return result
528
529
530def argmax(

Callers

nothing calls this directly

Calls 4

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

Tested by

no test coverage detected