使用BCELoss时,忽视某个类别,不计入loss(例如padding位置的loss不计入)
loss_fct = BCEWithLogitsLoss(reduction="none")
batch_size = prediction_scores.size()[0]
seq_len = prediction_scores.size()[1]
tmp_loss = loss_fct(prediction_scores.view(batch_size * seq_len, 1),
labels.view(batch_size * seq_len, 1).float())
tmp_mask = (labels != -100)
# 忽视类别标签为-100
real_loss = tmp_loss.view(batch_size, seq_len) * tmp_mask
masked_lm_loss = torch.sum(real_loss) / torch.sum(tmp_mask)