torch.quantile

torch.quantile(input, q) → Tensor

Returns the q-th quantiles of all elements in the input tensor, doing a linear interpolation when the q-th quantile lies between two data points.

Parameters
  • input (Tensor) – the input tensor.
  • q (float or Tensor) – a scalar or 1D tensor of quantile values in the range [0, 1]

Example:

>>> a = torch.randn(1, 3)
>>> a
tensor([[ 0.0700, -0.5446,  0.9214]])
>>> q = torch.tensor([0, 0.5, 1])
>>> torch.quantile(a, q)
tensor([-0.5446,  0.0700,  0.9214])
torch.quantile(input, q, dim=None, keepdim=False, *, out=None) → Tensor

Returns the q-th quantiles of each row of the input tensor along the dimension dim, doing a linear interpolation when the q-th quantile lies between two data points. By default, dim is None resulting in the input tensor being flattened before computation.

If keepdim is True, the output dimensions are of the same size as input except in the dimensions being reduced (dim or all if dim is None) where they have size 1. Otherwise, the dimensions being reduced are squeezed (see torch.squeeze()). If q is a 1D tensor, an extra dimension is prepended to the output tensor with the same size as q which represents the quantiles.

Parameters
  • input (Tensor) – the input tensor.
  • q (float or Tensor) – a scalar or 1D tensor of quantile values in the range [0, 1]
  • dim (int) – the dimension to reduce.
  • keepdim (bool) – whether the output tensor has dim retained or not.
Keyword Arguments

out (Tensor, optional) – the output tensor.

Example:

>>> a = torch.randn(2, 3)
>>> a
tensor([[ 0.0795, -1.2117,  0.9765],
        [ 1.1707,  0.6706,  0.4884]])
>>> q = torch.tensor([0.25, 0.5, 0.75])
>>> torch.quantile(a, q, dim=1, keepdim=True)
tensor([[[-0.5661],
        [ 0.5795]],

        [[ 0.0795],
        [ 0.6706]],

        [[ 0.5280],
        [ 0.9206]]])
>>> torch.quantile(a, q, dim=1, keepdim=True).shape
torch.Size([3, 2, 1])

© 2019 Torch Contributors
Licensed under the 3-clause BSD License.
https://pytorch.org/docs/1.8.0/generated/torch.quantile.html