pytorch 重用loss

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

发布了217 篇原创文章 · 获赞 55 · 访问量 20万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章