PyTorch基礎——均值、(無偏估計)方差

gy笨瓜發表於2020-10-10
import torch 

dat = torch.Tensor([1,2,3])

# 1.求均值
dat_mean = torch.mean(dat) # => dat_mean = tensor(2.)

# 2.求方差
dat_var1 = torch.var(dat)                  # => dat_var1 = tensor(1.),對應於無偏估計
dat_var2 = torch.var(dat, unbiased=False)  # => dat_var2 = tensor(0.6667)

torch.var(....)函式中unbiased預設為True,表示方差的無偏估計,除數為 —— 元素個數減一

dat_var1(無偏估計)計算:
( (1-2)*(1-2) +(2-2)*(2-2)+(3-2)*(3-2) ) / (3-1) = 2/2 = 1

dat_var2計算:
( (1-2)*(1-2)+(2-2)*(2-2)+(3-2)*(3-2) )  / 3 = 2/3 = 0.6666667

相關文章