损失函数
1. CrossEntropyloss
a.交叉熵损失函数,常用于分类
b.用这个loss前面不需要加 softmax层
c.该函数限制了target的类型为torch.LongTensor
import torch as t
from torch import nn
from torch.autograd import Variable as V
# batch_size=4, 计算每个类别分数(二分类)
output = V(t.randn(4,2)) # batch_size * C=(batch_size, C)
# target必须是LongTensor!
target =V(t.Tensor([1,0,1,1])).long()
criterion = nn.CrossEntropyLoss()
loss = criterion(output, target)
print('loss', loss)
output: loss tensor(1.0643)
2. toch.nn.MSELoss
均方损失函数,类似于nn.L1Loss函数:
import torch
loss_fn = torch.nn.MSELoss(reduce=False, size_average=False)
input = torch.autograd.Variable(torch.randn(3,4))
target = torch.autograd.Variable(torch.randn(3,4))
loss = loss_fn(input, target)
print(input); print(target); print(loss)
print(input.size(), target.size(), loss.size())
output:
评论区