rec_ce_smooth_loss.py 1.2 KB
Newer Older
T
Topdu 已提交
1 2 3 4 5
import paddle
from paddle import nn
import paddle.nn.functional as F


T
Topdu 已提交
6 7 8
class CESmoothingLoss(nn.Layer):
    def __init__(self, smoothing=True, with_all=False, **kwargs):
        super(CESmoothingLoss, self).__init__()
T
Topdu 已提交
9
        self.loss_func = nn.CrossEntropyLoss(reduction='mean', ignore_index=0)
T
Topdu 已提交
10
        self.smoothing = smoothing
T
Topdu 已提交
11
        self.with_all = with_all
T
Topdu 已提交
12 13 14

    def forward(self, pred, batch):
        pred = pred.reshape([-1, pred.shape[2]])
T
Topdu 已提交
15 16 17 18 19
        if self.with_all:
            tgt = batch[1]
        else:
            max_len = batch[2].max()
            tgt = batch[1][:, 1:2 + max_len]
T
Topdu 已提交
20
        tgt = tgt.reshape([-1])
T
Topdu 已提交
21 22 23 24 25 26
        if self.smoothing:
            eps = 0.1
            n_class = pred.shape[1]
            one_hot = F.one_hot(tgt, pred.shape[1])
            one_hot = one_hot * (1 - eps) + (1 - one_hot) * eps / (n_class - 1)
            log_prb = F.log_softmax(pred, axis=1)
T
Topdu 已提交
27 28
            non_pad_mask = paddle.not_equal(
                tgt, paddle.zeros(
T
topduke 已提交
29
                    tgt.shape, dtype=tgt.dtype))
T
Topdu 已提交
30 31 32 33 34
            loss = -(one_hot * log_prb).sum(axis=1)
            loss = loss.masked_select(non_pad_mask).mean()
        else:
            loss = self.loss_func(pred, tgt)
        return {'loss': loss}