LabelSmoother计算损失

针对LLM模型,计算每一个batch的损失

@dataclass
class LabelSmoother:
    """
    Adds label-smoothing on a pre-computed output from a Transformers model.

    Args:
        epsilon (`float`, *optional*, defaults to 0.1):
            The label smoothing factor.
        ignore_index (`int`, *optional*, defaults to -100):
            The index in the labels to ignore when computing the loss.
    """

LabelSmoother 数据类注释已表明:在transformer 模型的预计算输出中添加标签滑动。epsilon表示标签滑动因子,默认0.1;ignore_index 为计算损失时忽略不计的index值,默认-100。

源码如下:

    epsilon: float = 0.1
    ignore_index: int = -100

    def __call__(self, model_output, labels, shift_labels=False):
        logits = model_output["logits"] if isinstance(model_output, dict) else model_output[0]
        if shift_labels:
            logits = logits[..., :-1, :].contiguous()
            labels = labels[..., 1:].contiguous()

        log_probs = -nn.functional.log_softmax(logits, dim=-1)
        if labels.dim() == log_probs.dim() - 1:
            labels = labels.unsqueeze(-1)

        padding_mask = labels.eq(self.ignore_index)
        # In case the ignore_index is -100, the gather will fail, so we replace labels by 0. The padding_mask
        # will ignore them in any case.
        labels = torch.clamp(labels, min=0)
        nll_loss = log_probs.gather(dim=-1, index=labels)
        # works for fp16 input tensor too, by internally upcasting it to fp32
        smoothed_loss = log_probs.sum(dim=-1, keepdim=True, dtype=torch.float32)

        nll_loss.masked_fill_(padding_mask, 0.0)
        smoothed_loss.masked_fill_(padding_mask, 0.0)

        # Take the mean over the label dimensions, then divide by the number of active elements (i.e. not-padded):
        num_active_elements = padding_mask.numel() - padding_mask.long().sum()
        nll_loss = nll_loss.sum() / num_active_elements
        smoothed_loss = smoothed_loss.sum() / (num_active_elements * log_probs.shape[-1])
        return (1 - self.epsilon) * nll_loss + self.epsilon * smoothed_loss

自回归模型中,如GPT系列模型,预测是基于当前位置的上下文信息来预测下一个位置的token。这意味着在训练过程中,模型学习的是 根据当前序列生成下一个词 的任务。

shift_labels 为标签移位标识,false不移位,true需要移位(logits 倒数第二个维度删除最后一行;labels最后一个维度删除一列),为什么要如此处理尼?效果如下:

原始预训练语料为:我爱中国   (分词结果为[100, 101, 102, 103])

则,input_ids=[我,爱,中,国] = [100, 101, 102, 103]

       logits=[爱,中,国] = [101, 102, 103]

       labels=[爱,中,国] = [101, 102, 103]

其他说明:

1.padding_mask 记录labels 对应位置是否为填充值(索引为-100)

2.nll_loss 为-log(softmaxt(C)),C为对应的labels的值

3.num_active_elements 所有有效值的个数(即lables中索引不为-100)

4.return (1 - self.epsilon) * nll_loss + self.epsilon * smoothed_loss 返回计算的损失值。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值