jidasheng / bi-lstm-crf

A PyTorch implementation of the BI-LSTM-CRF model.
MIT License
246 stars 48 forks source link

list index out of range when trying to predict #10

Closed guilhermenoronha closed 4 years ago

guilhermenoronha commented 4 years ago

Hello,

My code is getting the error of 'list index out of range' when I try to predict a sentence from the model. A analyzed your code but couldn't figure out what could it be.

Here is my code:

    model = WordsTagger(model_dir='name_model')
    tags, sequences = model([["meu", "amigo", "e", "senhor", "."]])
    print(tags)  
    print(sequences)

Here is the log:

`Traceback (most recent call last):

  File "C:\Users\guilh\OneDrive\Textos e Documentação\workspace\BI-LSTM-CRF Cartas\bilstmprocessor.py", line 30, in <module>
    predict_text()

  File "C:\Users\guilh\OneDrive\Textos e Documentação\workspace\BI-LSTM-CRF Cartas\bilstmprocessor.py", line 26, in predict_text
    tags, sequences = model([["meu", "amigo", "e", "senhor", "."]])

  File "C:\Users\guilh\anaconda3\envs\bilstm_cartas\lib\site-packages\bi_lstm_crf\app\predict.py", line 40, in __call__
    return tags, self.tokens_from_tags(sentences, tags, begin_tags=begin_tags)

  File "C:\Users\guilh\anaconda3\envs\bilstm_cartas\lib\site-packages\bi_lstm_crf\app\predict.py", line 64, in tokens_from_tags
    tokens_list = [_tokens(sentence, ts) for sentence, ts in zip(sentences, tags_list)]

  File "C:\Users\guilh\anaconda3\envs\bilstm_cartas\lib\site-packages\bi_lstm_crf\app\predict.py", line 64, in <listcomp>
    tokens_list = [_tokens(sentence, ts) for sentence, ts in zip(sentences, tags_list)]

  File "C:\Users\guilh\anaconda3\envs\bilstm_cartas\lib\site-packages\bi_lstm_crf\app\predict.py", line 56, in _tokens
    begins = [b for idx, b in enumerate(begins) if idx == 0 or ts[idx] != "O" or ts[idx - 1] != "O"]

  File "C:\Users\guilh\anaconda3\envs\bilstm_cartas\lib\site-packages\bi_lstm_crf\app\predict.py", line 56, in <listcomp>
    begins = [b for idx, b in enumerate(begins) if idx == 0 or ts[idx] != "O" or ts[idx - 1] != "O"]

IndexError: list index out of range`

I'd appreciate any help. Thanks in advance. Best regards.

jidasheng commented 4 years ago

Hello, I can't fix the problem with the information you provide. You can go inside the code in predict.py and print more info. More specifically, print all inputs of the function "tokens_from_tags", the value of "begins" before the crash.

guilhermenoronha commented 4 years ago

Please, check if the prints I've sent to you are enough. If not, let me know it. Best regards.

print 1 print 2 print 3

jidasheng commented 4 years ago

The bug is fixed in the new version 0.2.1. You can upgrade and try again. Thanks for submitting the issue!

guilhermenoronha commented 4 years ago

Worked! Thx!