-
Notifications
You must be signed in to change notification settings - Fork 0
/
criterion.py
78 lines (64 loc) · 2.75 KB
/
criterion.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch import Tensor
class LmCrossEntropyLoss(nn.Module):
def __init__(self, ignore_index=None, reduction='batchmean') -> None:
super(LmCrossEntropyLoss, self).__init__()
assert reduction in ['none', 'batchmean', 'sum', 'mean']
self.reduction = reduction
self.criterion = nn.CrossEntropyLoss(ignore_index=ignore_index, reduction='none')
def forward(self, input: Tensor, target: Tensor) -> Tensor:
loss = self.compute_loss(input, target)
return self._reduce(loss)
def compute_loss(self, input: Tensor, target: Tensor) -> Tensor:
batch_size, _, num_embeddings = input.shape
loss = self.criterion(
input.view(-1, num_embeddings),
target.view(-1)
).view(batch_size, -1)
return loss
def _reduce(self, loss: Tensor) -> Tensor:
if self.reduction == 'batchmean':
return loss.sum(dim=1).mean(dim=0)
if self.reduction == 'sum':
return loss.sum()
if self.reduction == 'mean':
return loss.mean()
return loss
class LabelSmoothedLmCrossEntropyLoss(nn.Module):
def __init__(self, ignore_index=None, reduction='batchmean', label_smoothing=0.1) -> None:
super(LabelSmoothedLmCrossEntropyLoss, self).__init__()
assert reduction in ['none', 'batchmean', 'sum', 'mean']
assert label_smoothing > 0
self.ignore_index = ignore_index
self.reduction = reduction
self.label_smoothing = label_smoothing
def forward(self, input: Tensor, target: Tensor) -> Tensor:
loss = self.compute_loss(input, target)
return self._reduce(loss)
def compute_loss(self, input: Tensor, target: Tensor) -> Tensor:
if target.dim() == input.dim() - 1:
target = target.unsqueeze(-1)
lprobs = F.log_softmax(input, dim=-1)
nll_loss = -lprobs.gather(dim=-1, index=target)
smooth_loss = -lprobs.sum(dim=-1, keepdim=True)
if self.ignore_index is not None:
pad_mask = target.eq(self.ignore_index)
nll_loss.masked_fill_(pad_mask, 0.0)
smooth_loss.masked_fill_(pad_mask, 0.0)
else:
nll_loss = nll_loss.squeeze(-1)
smooth_loss = smooth_loss.squeeze(-1)
epsilon = self.label_smoothing
eps_i = epsilon / lprobs.size(-1)
loss = (1.0 - epsilon) * nll_loss + eps_i * smooth_loss
return loss
def _reduce(self, loss: Tensor) -> Tensor:
if self.reduction == 'batchmean':
return loss.sum(dim=1).mean(dim=0)
if self.reduction == 'sum':
return loss.sum()
if self.reduction == 'mean':
return loss.mean()
return loss