PyTorch torch.quantile function
PyTorch torch reference manual
torch.quantile is a function in PyTorch used to return the quantiles of a tensor. Quantiles are values that divide the data into proportional parts.
Function definition
torch.quantile(input, q, dim, keepdim=False)
Usage examples
Example
import
torch
x
=
torch.
tensor
(
[
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
]
)
Return the 0.5 quantile (median)
(
"0.5 quantile:"
,
torch.
quantile
(
x
,
0.5
)
)
Return multiple quantiles
(
"0.25, 0.75 quantiles:"
,
torch.
quantile
(
x
,
torch.
tensor
(
[
0.25
,
0.75
]
)
)
)
Quantiles along dim=0
y
=
torch.
tensor
(
[
[
1
,
2
,
3
]
,
[
4
,
5
,
6
]
]
)
(
"dim=0 0.5 quantile:"
,
torch.
quantile
(
y
,
0.5
,
dim
=
0
)
)
The output is:
0.5 分位数: tensor(5.5000)
0.25, 0.75 分位数: tensor([2.7500, 7.7500])
dim=0 0.5 分位数: tensor([2.5000, 3.5000, 4.5000])