Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

尊敬的作者您好,请问为什么MAX_SENTENCE_LENGTH设置为-1或者是一个比较大的数例如1000,会导致CRF层报错? #97

Open
ziqiYang19960522 opened this issue Oct 24, 2019 · 5 comments

Comments

@ziqiYang19960522
Copy link

ziqiYang19960522 commented Oct 24, 2019

事情是这样的,MAX_SENTENCE_LENGTH我看您默认设置为了250。默认的时候不管是训练还是decode都是正常的,而且效果很不错。
而我的数据集中的句子可能会有长度为1000左右的,如果按照默认的250,该句子就会消失在decode生成的结果文件中。源码中我也看到有个if判断,当MAX_SENTENCE_LENGTH设置为负数时会让所有长度的句子通过。因此我设置了MAX_SENTENCE_LENGTH为-1。结果在训练模型的时候没有报错,在decode的时候显示CRF层的一个语句报错,详细报错信息如下。

File "C:\Users\jmy\PycharmProjects\LatticeLSTM\model\crf.py", line 171, in _viterbi_decode
last_partition = torch.gather(partition_history, 1, last_position).view(batch_size,tag_size,1)
RuntimeError: Invalid index in gather at C:\w\1\s\tmp_conda_3.7_021303\conda\conda-bld\pytorch_1565316900252\work\aten\src\TH/generic/THTensorEvenMoreMath.cpp:472

报错提示gather函数的index是无效的,然后我打印了gather函数的第一个参数和第三个参数,也就是partition_history和last_position。
打印的代码是这样写的:

    print(partition_history.size())
    print(last_position.size())
    last_partition = torch.gather(partition_history, 1, last_position).view(batch_size,tag_size,1)
    print("success")

打印过程如下:
torch.Size([1, 312, 7])
torch.Size([1, 1, 7])
success
torch.Size([1, 164, 7])
torch.Size([1, 1, 7])
success
torch.Size([1, 219, 7])
torch.Size([1, 1, 7])
success
torch.Size([1, 256, 7])
torch.Size([1, 1, 7])
Traceback (most recent call last):
File "main.py", line 449, in
decode_results = load_model_decode(model_dir, data, 'raw', gpu, seg)
File "main.py", line 355, in load_model_decode
speed, acc, p, r, f, pred_results = evaluate(data, model, name)
File "main.py", line 153, in evaluate
tag_seq = model(gaz_list,batch_word, batch_biword, batch_wordlen, batch_char, batch_charlen, batch_charrecover, mask)
File "C:\Users\jmy\Anaconda3\envs\python3.7\lib\site-packages\torch\nn\modules\module.py", line 547, in call
result = self.forward(*input, **kwargs)
File "C:\Users\jmy\PycharmProjects\LatticeLSTM\model\bilstmcrf.py", line 42, in forward
scores, tag_seq = self.crf._viterbi_decode(outs, mask)
File "C:\Users\jmy\PycharmProjects\LatticeLSTM\model\crf.py", line 171, in _viterbi_decode
last_partition = torch.gather(partition_history, 1, last_position).view(batch_size,tag_size,1)
RuntimeError: Invalid index in gather at C:\w\1\s\tmp_conda_3.7_021303\conda\conda-bld\pytorch_1565316900252\work\aten\src\TH/generic/THTensorEvenMoreMath.cpp:472

请问作者知道是哪里有问题吗?为什么MAX_SENTENCE_LENGTH设置会导致这里有问题。250长度的默认值就可以完美train和decode。已经困惑了很久了,望作者解惑,十分感谢。

@ziqiYang19960522
Copy link
Author

我发现问题不是出在MAX_SENTENCE_LENGTH上,而是只要句子的长度是256、512、1024这样比较特殊的数字上就会报错,其他数字就不会报错,很奇怪的问题。

@ziqiYang19960522
Copy link
Author

难道是神经网络的某个结构导致256、512这样的长度的句子会发生bug?

@jiesutd
Copy link
Owner

jiesutd commented Oct 24, 2019

这个很奇怪也很有意思,根据log, 应该是这句在256/512/2014时有问题

last_partition = torch.gather(partition_history, 1, last_position).view(batch_size,tag_size,1)

不过我没时间仔细研究了,我把issue 放这,有兴趣的人可以瞅瞅。

实际使用的话,你把MAX_SENTENCE_LENGTH设置成其他值就行了。

@jiesutd jiesutd closed this as completed Oct 24, 2019
@jiesutd jiesutd reopened this Oct 24, 2019
@jiesutd
Copy link
Owner

jiesutd commented Oct 24, 2019

还有,你得看看你的Python 和PyTorch 的版本。

@ziqiYang19960522
Copy link
Author

ziqiYang19960522 commented Oct 24, 2019 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants