rec_nrtr_loss.py 1.2 KB

123456789101112131415161718192021222324252627282930313233
  1. import paddle
  2. from paddle import nn
  3. import paddle.nn.functional as F
  4. class NRTRLoss(nn.Layer):
  5. def __init__(self, smoothing=True, ignore_index=0, **kwargs):
  6. super(NRTRLoss, self).__init__()
  7. if ignore_index >= 0 and not smoothing:
  8. self.loss_func = nn.CrossEntropyLoss(
  9. reduction="mean", ignore_index=ignore_index
  10. )
  11. self.smoothing = smoothing
  12. def forward(self, pred, batch):
  13. max_len = batch[2].max()
  14. tgt = batch[1][:, 1 : 2 + max_len]
  15. pred = pred.reshape([-1, pred.shape[2]])
  16. tgt = tgt.reshape([-1])
  17. if self.smoothing:
  18. eps = 0.1
  19. n_class = pred.shape[1]
  20. one_hot = F.one_hot(tgt, pred.shape[1])
  21. one_hot = one_hot * (1 - eps) + (1 - one_hot) * eps / (n_class - 1)
  22. log_prb = F.log_softmax(pred, axis=1)
  23. non_pad_mask = paddle.not_equal(
  24. tgt, paddle.zeros(tgt.shape, dtype=tgt.dtype)
  25. )
  26. loss = -(one_hot * log_prb).sum(axis=1)
  27. loss = loss.masked_select(non_pad_mask).mean()
  28. else:
  29. loss = self.loss_func(pred, tgt)
  30. return {"loss": loss}