lonePatient / BERT-NER-Pytorch

Chinese NER(Named Entity Recognition) using BERT(Softmax, CRF, Span)
MIT License
2.05k stars 425 forks source link

作NER任务求分类损失的时候为啥【clc】标签忽略了,而【seq】没有忽略? #72

Open cqray1990 opened 2 years ago

cqray1990 commented 2 years ago

if labels is not None: assert self.loss_type in ['lsr', 'focal', 'ce'] if self.loss_type == 'lsr': loss_fct = LabelSmoothingCrossEntropy(ignore_index=0) elif self.loss_type == 'focal': loss_fct = FocalLoss(ignore_index=0) else: loss_fct = CrossEntropyLoss(ignore_index=0)

Only keep active parts of the loss

        if attention_mask is not None:
            active_loss = attention_mask.view(-1) == 1
            active_logits = logits.view(-1, self.num_labels)[active_loss]
            active_labels = labels.view(-1)[active_loss]
            loss = loss_fct(active_logits, active_labels)