我在google colab中运行代码,但是遇到一些错误。
torch:1.1.0,torchtext:0.4.0,我更新了它们,但是没用
[2019-09-14 02:57:15,574 INFO] * src vocab size = 49766
[2019-09-14 02:57:15,574 INFO] * history vocab size = 49766
[2019-09-14 02:57:15,574 INFO] * tgt vocab size = 49766
[2019-09-14 02:57:15,574 INFO] Building model...
Traceback (most recent call last):
File "/content/drive/My Drive/ReDR/train.py", line 109, in <module>
main(opt)
File "/content/drive/My Drive/ReDR/train.py", line 41, in main
single_main(opt, -1)
File "/content/drive/My Drive/ReDR/onmt/train_single.py", line 86, in main
model = build_model(model_opt, opt, fields, checkpoint)
File "/content/drive/My Drive/ReDR/onmt/model_builder.py", line 235, in build_model
vocab = torch.load(opt.drqa_vocab_path)
File "/usr/local/lib/python3.6/dist-packages/torch/serialization.py", line 387, in load
return _load(f, map_location, pickle_module, **pickle_load_args)
File "/usr/local/lib/python3.6/dist-packages/torch/serialization.py", line 574, in _load
result = unpickler.load()
AttributeError: Can't get attribute '_default_unk_index' on <module 'torchtext.vocab' from '/usr/local/lib/python3.6/dist-packages/torchtext/vocab.py'>
感谢您的帮助!
答案 0 :(得分:0)
存在相同的问题,https://github.com/ZJULearning/ReDR/issues/3
中提供了解决方案编辑ReDR / onmt / model_builder.py
添加
from torchtext import vocab
try:
vocab._default_unk_index
except AttributeError:
def _default_unk_index():
return 0
vocab._default_unk_index = _default_unk_index
应该可以解决问题
答案 1 :(得分:0)
检查在保存词汇表时使用的torchtext版本,然后加载相同的词汇表。 torchtext github issue讨论了相同的问题。解决方案是将其降级到保存它的torchtext版本。或升级torchtext版本,重新创建vocab pickle文件,然后使用它。
vocab保存为torchtext 0.3.1
vocab使用torchtext 0.4.0加载
解决方案是,将Torchtext从0.4.0降级到0.3.1,然后加载vocab应该起作用
pip install torchtext==0.3.1
对我来说,这种解决方案有效。
vocab保存为torchtext 0.3.1
vocab使用torchtext 0.4.0加载
解决方案是,将torchtext从0.3.1升级到0.4.0,然后使用新的torchtext再次保存vocab。现在加载应该可以正常工作了。
pip install torchtext==0.4.0