median
- paddle.compat. median ( input: Tensor, dim: int | None = None, keepdim: bool = False, *, out: tuple[Tensor, Tensor] | Tensor | None = None ) Tensor | MedianRetType [source]
-
Returns the median of the values in input.
- Parameters
-
input (Tensor) – The input tensor.
dim (int|None, optional) – The dimension to reduce. If None, computes the median over all elements. Default is None.
keepdim (bool, optional) – Whether the output tensor has dim retained or not. Default is False.
out (Tensor|tuple[Tensor, Tensor], optional) – If provided, the result will be written into this tensor. For global median (dim=None), out must be a single tensor. For median along a dimension (dim specified, including dim=-1), out must be a tuple of two tensors (values, indices).
- Returns
-
If dim is None, returns a single tensor. If dim is specified (including dim=-1), returns a named tuple MedianRetType(values: Tensor, indices: Tensor).
- Return type
-
Tensor|MedianRetType
Examples
>>> import paddle >>> x = paddle.to_tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> result = paddle.compat.median(x) >>> print(result) Tensor(shape=[], dtype=int64, place=Place(cpu), stop_gradient=True, 5) >>> ret = paddle.compat.median(x, dim=1) >>> print(ret.values) Tensor(shape=[3], dtype=int64, place=Place(cpu), stop_gradient=True, [2, 5, 8]) >>> print(ret.indices) Tensor(shape=[3], dtype=int64, place=Place(cpu), stop_gradient=True, [1, 1, 1]) >>> # Using out parameter >>> out_values = paddle.zeros([3], dtype='int64') >>> out_indices = paddle.zeros([3], dtype='int64') >>> paddle.compat.median(x, dim=1, out=(out_values, out_indices)) >>> print(out_values) Tensor(shape=[3], dtype=int64, place=Place(cpu), stop_gradient=True, [2, 5, 8])