torch.quantile¶
- torch.quantile(input, q, dim=None, keepdim=False, *, interpolation='linear', out=None) 张量 ¶
计算沿维度
dim
的input
张量每行的第 q 个分位数。要计算分位数,我们将 q 在 [0, 1] 范围内映射到索引范围 [0, n] 以找到分位数在排序输入中的位置。如果分位数位于两个数据点
a < b
之间,其索引分别为i
和j
,则根据给定的interpolation
方法计算结果,如下所示:linear
:a + (b - a) * fraction
, 其中fraction
是计算的分位数索引的小数部分。lower
:a
.higher
:b
.nearest
:a
或b
,取其索引更接近计算出的分位数索引的那个(对于0.5的分数向下取整)。midpoint
:(a + b) / 2
。
如果
q
是一个一维张量,输出的第一个维度表示分位数,其大小等于q
的大小,其余维度是缩减后剩余的部分。注意
默认情况下,
dim
是None
,这会导致input
张量在计算之前被展平。- Parameters
- Keyword Arguments
示例:
>>> 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.)