torch.quantile#
- torch.quantile(input, q, dim=None, keepdim=False, *, interpolation='linear', out=None) Tensor#
計算
input張量沿dim維度的每行的 q 分位數。為了計算分位數,我們將 q 值在 [0, 1] 的範圍內對映到索引範圍 [0, n],以找到已排序輸入中分位數的位置。如果分位數位於已排序順序中的索引
i和j之間的兩個資料點a < b之間,則根據給定的interpolation方法計算結果,如下所示:linear:a + (b - a) * fraction,其中fraction是計算出的分位數索引的小數部分。lower:a。higher:b。nearest:a或b,取決於哪個索引更接近計算出的分位數索引(.5 分數向下取整)。midpoint:(a + b) / 2。
如果
q是一個 1D 張量,輸出的第一維表示分位數,其大小等於q的大小,其餘維度是歸約後剩餘的維度。注意
預設情況下,
dim為None,這會導致在計算之前將input張量展平。- 引數
- 關鍵字引數
示例
>>> 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.)