特别是在 opennmt-py 中。现在有很多关于这个主题的问题,例如在 OpenNMT-py 中的翻译过程中获取对齐/注意以及 opennmt 论坛上的以下主题https://github.com/OpenNMT/OpenNMT-py/issues/575。我使用后者建议的代码。然而,似乎没有人能解决我遇到的问题。我尝试运行以下简单的代码片段。
import onmt
import onmt.inputters
import onmt.translate
import onmt.model_builder
from collections import namedtuple
Opt = namedtuple('Opt', ['models', 'data_type', 'reuse_copy_attn', "gpu"])
opt = Opt("/home/Desktop/hidden-att/model/hidden-2/seed-0/LSTMlang1_step_400.pt", "text",False, 0)
fields, model, model_opt = onmt.model_builder.load_test_model(opt,{"reuse_copy_attn":False})
我得到这个错误跟踪。
Traceback (most recent call last):
File "<ipython-input-63-94c1f45c429f>", line 1, in <module>
runfile('/home/Desktop/hidden-att/graph_hidden_exp.py', wdir='/home/Desktop/hidden-att')
File "/home/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 786, in runfile
execfile(filename, namespace)
File "/home/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "/home/Desktop/hidden-att/graph_hidden_exp.py", line 33, in <module>
fields, model, model_opt = onmt.model_builder.load_test_model(opt,{"reuse_copy_attn":False})
File "../../Documents/NMT/OpenNMT-py/onmt/model_builder.py", line 85, in load_test_model
map_location=lambda storage, loc: storage)
File "/home/anaconda3/lib/python3.7/site-packages/torch/serialization.py", line 387, in load
return _load(f, map_location, pickle_module, **pickle_load_args)
File "/home/anaconda3/lib/python3.7/site-packages/torch/serialization.py", line 549, in _load
_check_seekable(f)
File "/home/anaconda3/lib/python3.7/site-packages/torch/serialization.py", line 194, in _check_seekable
raise_err_msg(["seek", "tell"], e)
File "/home/anaconda3/lib/python3.7/site-packages/torch/serialization.py", line 187, in raise_err_msg
raise type(e)(msg)
AttributeError: 'dict' object has no attribute 'seek'. You can only torch.load from a file that is seekable. Please pre-load the data into a buffer like io.BytesIO and try to load from it instead.
那么有没有人经历过并解决了这个问题?或者知道去哪里看?我猜这与加载的文件有关,但它是用 opennmt-py 以相当标准的方式训练的。