Skip to content

Instantly share code, notes, and snippets.

@applenob
Created May 26, 2020 06:41
Show Gist options
  • Save applenob/fe63585ae8798660fbe312271ee19f1c to your computer and use it in GitHub Desktop.
Save applenob/fe63585ae8798660fbe312271ee19f1c to your computer and use it in GitHub Desktop.
"""
from https://www.kaggle.com/shonenkov/tpu-training-super-fast-xlmroberta
"""
import torch.nn as nn
class LabelSmoothing(nn.Module):
def __init__(self, smoothing = 0.1):
super(LabelSmoothing, self).__init__()
self.confidence = 1.0 - smoothing
self.smoothing = smoothing
def forward(self, x, target):
if self.training:
x = x.float()
target = target.float()
logprobs = torch.nn.functional.log_softmax(x, dim = -1)
nll_loss = -logprobs * target
nll_loss = nll_loss.sum(-1)
smooth_loss = -logprobs.mean(dim=-1)
loss = self.confidence * nll_loss + self.smoothing * smooth_loss
return loss.mean()
else:
return torch.nn.functional.cross_entropy(x, target)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment