如题,本人在用pytorch实现命名实体识别,目前已经用服务器跑出模型,正在进行预测的操作。预测的传入参数是一条string,但目前发现随着参数字数的增多,就会出现“index out of range in self”的问题,具体报错信息如下。请问有可能是什么原因呢
Traceback (most recent call last):
File "crf_predict.py", line 114, in <module>
print(get_crf_ners(text))
File "crf_predict.py", line 91, in get_crf_ners
pred_tags, tokens = crf.predict(text)
File "crf_predict.py", line 45, in predict
_, y_hat = self.model(xx)
File "C:\Users\10627\.conda\envs\tf1.x\lib\site-packages\torch\nn\modules\module.py", line 1194, in _call_impl
return forward_call(*input, **kwargs)
File "E:\StudyData\Junior2\RXA\1raw-Bert-BiLSTM-CRF-pytorch-master\Bert-BiLSTM-CRF-pytorch-master\crf.py", line 168, in forward
lstm_feats = self._get_lstm_features(sentence) # [8, 180,768]
File "E:\StudyData\Junior2\RXA\1raw-Bert-BiLSTM-CRF-pytorch-master\Bert-BiLSTM-CRF-pytorch-master\crf.py", line 160, in _get_lstm_features
embeds = self._bert_enc(sentence) # [8, 75, 768]
File "E:\StudyData\Junior2\RXA\1raw-Bert-BiLSTM-CRF-pytorch-master\Bert-BiLSTM-CRF-pytorch-master\crf.py", line 109, in _bert_enc
encoded_layer, _ = self.bert(x)
File "C:\Users\10627\.conda\envs\tf1.x\lib\site-packages\torch\nn\modules\module.py", line 1194, in _call_impl
return forward_call(*input, **kwargs)
File "C:\Users\10627\.conda\envs\tf1.x\lib\site-packages\pytorch_pretrained_bert\modeling.py", line 730, in forward
embedding_output = self.embeddings(input_ids, token_type_ids)
File "C:\Users\10627\.conda\envs\tf1.x\lib\site-packages\torch\nn\modules\module.py", line 1194, in _call_impl
return forward_call(*input, **kwargs)
File "C:\Users\10627\.conda\envs\tf1.x\lib\site-packages\pytorch_pretrained_bert\modeling.py", line 268, in forward
position_embeddings = self.position_embeddings(position_ids)
return forward_call(*input, **kwargs)
File "C:\Users\10627\.conda\envs\tf1.x\lib\site-packages\torch\nn\modules\sparse.py", line 162, in forward
File "C:\Users\10627\.conda\envs\tf1.x\lib\site-packages\torch\nn\functional.py", line 2210, in embedding
return torch.embedding(weight, input, padding_idx, scale_grad_by_freq, sparse)
IndexError: index out of range in self
报错的部分代码
def forward(self, sentence): # dont confuse this with _forward_alg above.
# Get the emission scores from the BiLSTM
lstm_feats = self._get_lstm_features(sentence) # [8, 180,768]
# Find the best path, given the features.
score, tag_seq = self._viterbi_decode(lstm_feats)
return score, tag_seq
def _get_lstm_features(self, sentence):
"""sentence is the ids"""
# self.hidden = self.init_hidden()
embeds = self._bert_enc(sentence+1) # [8, 75, 768]
# 过lstm
enc, _ = self.lstm(embeds)
lstm_feats = self.fc(enc)
return lstm_feats # [8, 75, 16]
def _bert_enc(self, x):
"""
x: [batchsize, sent_len]
enc: [batch_size, sent_len, 768]
"""
with torch.no_grad():
encoded_layer, _ = self.bert(x)
enc = encoded_layer[-1]
return enc