ChingKai Chau 2023-03-09 20:36 采纳率: 28.6%
浏览 21

关于pytorch运行bert-bilstm-crf的问题,如何解决?

如题,本人在用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

  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2023-03-09 22:36
    关注
    评论

报告相同问题?

问题事件

  • 创建了问题 3月9日

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路