理解 github上代码:Bert-BiLSTM-CRF-pytorch
Github 相关链接: link.
neg_log_likelihood_loss = forward_score - gold_score
这部分应该是为了计算真实径的分数(gold_score)
def _score_sentence(self, scores, mask, tags):
"""
Args:
scores: size=(seq_len, batch_size, tag_size, tag_size)
注:这个score是 _forward_alg中返回的第二项值
(即LSTM输出的feats 和transition matrix相加之后的结果)
mask: size=(batch_size, seq_len)
tags: size=(batch_size, seq_len)
Returns:
score:
"""
batch_size = scores.size(1)
seq_len = scores.size(0)
tag_size = scores.size(-1)
new_tags = Variable(torch.LongTensor(batch_size, seq_len))
if self.use_cuda:
new_tags = new_tags.cuda()
for idx in range(seq_len):
if idx == 0:
new_tags[:, 0] = (tag_size - 2) * tag_size + tags[:, 0]
else:
new_tags[:, idx] = tags[:, idx-1] * tag_size + tags[:, idx]
end_transition = self.transitions[:, self.END_TAG_IDX].contiguous().view(
1, tag_size).expand(batch_size, tag_size)
length_mask = torch.sum(mask, dim=1).view(batch_size, 1).long()
end_ids = torch