8000 fix ncrf model · flypythoncom/ner-bert@1a9a1f3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1a9a1f3

Browse files
author
Ubuntu
committed
fix ncrf model
1 parent ae638ae commit 1a9a1f3

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

modules/layers/decoders.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -589,16 +589,25 @@ def forward(self, inputs, labels_mask):
589589
self.eval()
590590
logits, intent_output = self.forward_model(inputs)
591591
_, preds = self.crf._viterbi_decode_nbest(logits, labels_mask, self.nbest)
592+
# print(preds.shape)
593+
preds = preds[:, :, 0]
594+
"""for idx in range(len(preds)):
595+
for idx_ in range(len(preds[0])):
596+
if preds[idx][idx_] > 0:
597+
preds[idx][idx_] -= 1
598+
else:
599+
raise"""
600+
# print(preds)
592601
self.train()
593602
return preds, intent_output.argmax(-1)
594603

595604
def score(self, inputs, labels_mask, labels, cls_ids):
596605
logits, intent_output = self.forward_model(inputs)
597-
crf_score = self.crf.neg_log_likelihood_loss(logits, labels_mask, labels) / logits.shape[0]
606+
crf_score = self.crf.neg_log_likelihood_loss(logits, labels_mask, labels) / logits.size(0)
598607
return crf_score + self.intent_loss(intent_output, cls_ids)
599608

600609
@classmethod
601610
def create(cls, label_size, input_dim, intent_size, input_dropout=0.5, key_dim=64,
602611
val_dim=64, num_heads=3, use_cuda=True, nbest=8):
603-
return cls(NCRF(label_size + 2, use_cuda), label_size, input_dim, intent_size, input_dropout,
612+
return cls(NCRF(label_size, use_cuda), label_size + 2, input_dim, intent_size, input_dropout,
604613
key_dim, val_dim, num_heads, nbest)

modules/layers/ncrf.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
# @Date: 2017-12-04 23:19:38
44
# @Last Modified by: Jie Yang, Contact: jieynlp@gmail.com
55
# @Last Modified time: 2018-05-16 16:57:39
6+
# https://github.com/Das-Boot/NCRFpp
7+
# @inproceedings{yang2018ncrf,
8+
# title={NCRF++: An Open-source Neural Sequence Labeling Toolkit},
9+
# author={Yang, Jie and Zhang, Yue},
10+
# booktitle={Proceedings of the 56th Annual Meeting of the Association for Computational Linguistics},
11+
# Url = {https://arxiv.org/pdf/1806.05626.pdf},
12+
# year={2018}
13+
# }
614
from __future__ import print_function
715
import torch
816
import torch.autograd as autograd
@@ -90,6 +98,7 @@ def _calculate_PZ(self, feats, mask):
9098
mask_idx = mask[idx, :].view(batch_size, 1).expand(batch_size, tag_size)
9199

92100
## effective updated partition part, only keep the partition value of mask value = 1
101+
mask_idx = mask_idx.byte()
93102
masked_cur_partition = cur_partition.masked_select(mask_idx)
94103
## let mask_idx broadcastable, to disable warning
95104
mask_idx = mask_idx.contiguous().view(batch_size, tag_size, 1)
@@ -240,6 +249,7 @@ def _score_sentence(self, scores, mask, tags):
240249
### need convert tags id to search from 400 positions of scores
241250
tg_energy = torch.gather(scores.view(seq_len, batch_size, -1), 2, new_tags).view(seq_len, batch_size) # seq_len * bat_size
242251
## mask transpose to (seq_len, batch_size)
252+
mask = mask.byte()
243253
tg_energy = tg_energy.masked_select(mask.transpose(1,0))
244254

245255
# ## calculate the score from START_TAG to first label

modules/models/bert_models.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,13 +309,15 @@ def create(cls,
309309
# Global params
310310
use_cuda=True,
311311
# Meta
312-
meta_dim=None):
312+
meta_dim=None,
313+
# NCRFpp
314+
nbest=8):
313315
embedder = BertEmbedder.create(
314316
bert_config_file, init_checkpoint_pt, embedding_dim, use_cuda, bert_mode, freeze)
315317
if meta_dim is None:
316318
encoder = BertBiLSTMEncoder.create(embedder, enc_hidden_dim, rnn_layers, use_cuda)
317319
else:
318320
encoder = BertMetaBiLSTMEncoder.create(embedder, meta_dim, enc_hidden_dim, rnn_layers, use_cuda)
319321
decoder = AttnNCRFJointDecoder.create(
320-
label_size, encoder.output_dim, intent_size, input_dropout, key_dim, val_dim, num_heads, use_cuda)
322+
label_size, encoder.output_dim, intent_size, input_dropout, key_dim, val_dim, num_heads, use_cuda, nbest=nbest)
321323
return cls(encoder, decoder, use_cuda)

0 commit comments

Comments
 (0)
0