mirror of
https://github.com/labmlai/annotated_deep_learning_paper_implementations.git
synced 2025-11-02 13:00:17 +08:00
implemented resnets and transfer learning
This commit is contained in:
committed by
Varuna Jayasiri
parent
4f31570f92
commit
94796efa44
16
labml_nn/resnets/utils/labelsmoothing.py
Normal file
16
labml_nn/resnets/utils/labelsmoothing.py
Normal file
@ -0,0 +1,16 @@
|
||||
import torch.nn.functional as F
|
||||
from torch import nn
|
||||
|
||||
class LabelSmoothingLoss(nn.Module):
|
||||
def __init__(self, epsilon= 0.5, reduction='mean'):
|
||||
super().__init__()
|
||||
self.epsilon = epsilon
|
||||
self.reduction = reduction
|
||||
|
||||
def forward(self, pred, target):
|
||||
n = pred.size()[-1]
|
||||
log_pred = F.log_softmax(pred, dim=-1)
|
||||
loss = -log_pred.sum(dim=-1).mean()
|
||||
nll = F.nll_loss(log_pred, target, reduction=self.reduction)
|
||||
out = (1-self.epsilon)*nll + self.epsilon*(loss / n)
|
||||
return out
|
||||
Reference in New Issue
Block a user