快捷方式

torch.quantile

torch.quantile(input, q, dim=None, keepdim=False, *, interpolation='linear', out=None) Tensor

计算 input 张量沿维度 dim 的每一行的 q 分位数。

为了计算分位数,我们将 [0, 1] 范围内的 q 映射到 [0, n] 的索引范围,以找到分位数在排序输入中的位置。如果分位数位于排序顺序中索引为 ij 的两个数据点 a < b 之间,则结果根据给定的 interpolation 方法计算如下:

  • linear: a + (b - a) * fraction,其中 fraction 是计算出的分位数索引的小数部分。

  • lower: a

  • higher: b

  • nearest: ab,以索引更接近计算出的分位数索引者为准(对于 .5 的小数部分向下取整)。

  • midpoint: (a + b) / 2

如果 q 是一个 1D 张量,则输出的第一个维度代表分位数,其大小等于 q 的大小,其余维度是规约后剩余的维度。

注意

默认情况下,dimNone,这将导致 input 张量在计算前被展平。

参数
  • input (Tensor) – 输入张量。

  • q (float or Tensor) – 位于 [0, 1] 范围内的标量或 1D 张量。

  • dim (int) – 要规约的维度。

  • keepdim (bool) – 输出张量是否保留 dim

关键字参数
  • interpolation (str) – 当所需分位数位于两个数据点之间时使用的插值方法。可以是 linearlowerhighermidpointnearest。默认为 linear

  • out (Tensor, optional) – 输出张量。

示例

>>> 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])
>>> a = torch.arange(4.)
>>> a
tensor([0., 1., 2., 3.])
>>> torch.quantile(a, 0.6, interpolation='linear')
tensor(1.8000)
>>> torch.quantile(a, 0.6, interpolation='lower')
tensor(1.)
>>> torch.quantile(a, 0.6, interpolation='higher')
tensor(2.)
>>> torch.quantile(a, 0.6, interpolation='midpoint')
tensor(1.5000)
>>> torch.quantile(a, 0.6, interpolation='nearest')
tensor(2.)
>>> torch.quantile(a, 0.4, interpolation='nearest')
tensor(1.)

文档

查阅 PyTorch 的全面开发者文档

查看文档

教程

获取适合初学者和高级开发者的深度教程

查看教程

资源

查找开发资源并获得问题解答

查看资源