import torch
torch.nn.MSELoss()
>>> a = torch.rand(3)
>>> a
tensor([0.2161, 0.2227, 0.9175])
>>> b = torch.rand(3)
>>> b
tensor([0.6976, 0.9149, 0.4918])
>>> mse = torch.nn.MSELOSS()
>>> mse(a, b)
tensor(0.2974)
>>> ((0.2161-0.6976)**2 + (0.2227-0.9149)**2 + (0.9175-0.4918)**2)/3
0.2974011933333333
控制檯程序不好複製,一個複製技巧就是先選擇,然後標記,最後在標題上右擊選擇編輯複製即可,直接CTRL+C不好使。
MSELoss是求高斯距一個函數。
1. 均方誤差函數(Mean Squared Eqation)
torch.nn.CrossEntropyLoss()
1.描述兩個概率分佈間的距離
2.交叉熵函數
import torch.nn as nn
loss = nn.CrossEntropyLoss()
input = torch.randn(3, 5, requires_grad=True)
target = torch.empty(3, dtype=torch.long).random_(5)
output = loss(input, target)
output.backward()
分類問題用Cross Entropy
迴歸問題用MSE