PyTorch torch.quantile 函数
torch.quantile 是 PyTorch 中用于返回张量的分位数的函数。分位数是将数据按比例分割的值。
函数定义
torch.quantile(input, q, dim, keepdim=False)
使用示例
实例
import torch
x = torch.tensor([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
# 返回 0.5 分位数(中位数)
print("0.5 分位数:", torch.quantile(x, 0.5))
# 返回多个分位数
print("0.25, 0.75 分位数:", torch.quantile(x, torch.tensor([0.25, 0.75])))
# 沿 dim=0 分位数
y = torch.tensor([[1, 2, 3], [4, 5, 6]])
print("dim=0 0.5 分位数:", torch.quantile(y, 0.5, dim=0))
x = torch.tensor([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
# 返回 0.5 分位数(中位数)
print("0.5 分位数:", torch.quantile(x, 0.5))
# 返回多个分位数
print("0.25, 0.75 分位数:", torch.quantile(x, torch.tensor([0.25, 0.75])))
# 沿 dim=0 分位数
y = torch.tensor([[1, 2, 3], [4, 5, 6]])
print("dim=0 0.5 分位数:", torch.quantile(y, 0.5, dim=0))
输出结果为:
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])

Pytorch torch 参考手册