为什么在 BERTTrainer 中的 NLLLoss 函数中指定了 `ignore_index=0`?

作者: Jasmine969创建于 2022年7月7日更新于 2023年1月10日

trainer/pretrain.py

Python class BERTTrainer: def init(self, ...): ... # 使用负对数似然损失函数来预测掩码标记 self.criterion = nn.NLLLoss(ignore_index=0) ... 我不明白为什么在计算 NLLLoss 时指定了 ignore index=0。如果 is_next 的真实值为 False(标签为 0),但 BERT 预测为 True,那么 NLLLoss 将为 0(或 nan)...那么 ignore_index = 0 的目的是什么呢?

内容来源: codertimo/BERT-pytorch