PyTorch torch.var 函数
torch.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))
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 参考手册