PyTorch torch.quantile 関数


Pytorch torch 参考手册Pytorch torch リファレンスマニュアル

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))

出力結果は次のとおりです:

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 参考手册Pytorch torch リファレンスマニュアル

その他の拡張