PyTorch torch.var 関数


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

torch.vartorch.var は、PyTorch でテンソルの分散を返す関数です。分散は、各データと平均値の差の二乗の平均であり、データのばらつきの程度を測る指標です。

関数定義

torch.var(input, dim, unbiased, keepdim=False)

使用例

例

import torch

x = torch.tensor([1.0, 2.0, 3.0, 4.0, 5.0])

# すべての要素の分散を返す
print("分散:", torch.var(x))

# dim=0 に沿った分散
y = torch.tensor([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
print("dim=0 分散:", torch.var(y, dim=0))
print("dim=1 分散:", torch.var(y, dim=1))

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

方差: tensor(2.5000)
dim=0 方差: tensor([2.2500, 2.2500, 2.2500])
dim=1 方差: tensor([1., 1.])

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

その他の拡張