hertz-pj / BERT-BiLSTM-CRF-NER-pytorch

Pytorch BERT-BiLSTM-CRF For NER
406 stars 67 forks source link

你好, 可否告訴, 如何做predict? #4

Closed marcusau closed 3 years ago

marcusau commented 4 years ago

I have trained the model.... and was planning to use the output model to predict the NER:

here are my codes. I defined max seq length =128 when training the model.

My initial of loading configs, tokenizer and model are all ok, but

still encounter with the following errors:

need_birnn : True rnn_dim: 128 max_seq_length: 128

input_text='創科局常任秘書長蔡淑嫻今早終於港台節目訪問,公開晶苑(2232)處理當中較大規模口罩生產,廠房包括荃灣南豐紗廠、土瓜灣聯業製衣,及晶苑於越南的廠房。'

textlist=list(input_text)
tokens = [tokenizer.tokenize(word) for  word in enumerate(textlist)]

if len(tokens) >= max_seq_length - 1:
    tokens = tokens[0:(max_seq_length - 2)]  # -2 的原因是因为序列需要加一个句首和句尾标志

ntokens = ["[CLS]"] + tokens + ["[SEP]"]

sinput_ids = tokenizer.convert_tokens_to_ids(ntokens)   
segment_ids = [0] * len(input_ids)        
input_mask = [1] * len(input_ids)

while len(input_ids) < max_seq_length:
    input_ids.append(0)
    segment_ids.append(0)
    input_mask.append(0)

assert len(input_ids) == max_seq_length
assert len(segment_ids) == max_seq_length
assert len(input_mask) == max_seq_length

input_ids = torch.tensor(input_ids, dtype=torch.long)
segment_ids = torch.tensor(segment_ids, dtype=torch.long)
input_mask = torch.tensor(input_mask, dtype=torch.long)

input_ids = input_ids.to(device)
segment_ids = segment_ids.to(device)
input_mask = input_mask.to(device)

print(input_ids.shape, input_mask.shape)

torch.Size([128]) torch.Size([128])

pred_labels = []
with torch.no_grad():
    logits = model.predict(input_ids, segment_ids, input_mask)

Traceback (most recent call last): File "/home/ubuntu/PeijiYang/predict.py", line 155, in logits = model.predict(input_ids, segment_ids, input_mask) File "/home/ubuntu/PeijiYang/models.py", line 53, in predict emissions = self.tag_outputs(input_ids, token_type_ids, input_mask) File "/home/ubuntu/PeijiYang/models.py", line 40, in tag_outputs outputs = self.bert(input_ids, token_type_ids=token_type_ids, attention_mask=input_mask) File "/home/ubuntu/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 541, in call result = self.forward(*input, **kwargs) File "/home/ubuntu/.local/lib/python3.6/site-packages/transformers/modeling_bert.py", line 725, in forward input_shape, attention_mask.shape ValueError: Wrong shape for input_ids (shape torch.Size([128])) or attention_mask (shape torch.Size([128]))

hertz-pj commented 4 years ago

input_ids,segment_ids,input_mask these three tensor missing the batch dimension. you need to expand them from torch.Size([128]) to torch.Size([1,128])

hertz-pj commented 4 years ago

you can try

input_ids = input_ids.unsqueeze(0)
segment_ids = segment_ids.unsqueeze(0)
input_mask = input_mask.unsqueeze(0)
marcusau commented 4 years ago

it works. thanks you :)

marcusau commented 4 years ago

可否一問.. 如何做online training? 我的NER model己finetune ..但部份新data...己做了標註,, 希望放入原來己fine-tuned的model內training...

麻煩你

hertz-pj commented 3 years ago

可否一問.. 如何做online training? 我的NER model己finetune ..但部份新data...己做了標註,, 希望放入原來己fine-tuned的model內training...

麻煩你

don't support online training. You need to train with a mixture of old and new data.