特别是在opennmt-py中。现在,有关此主题的问题很多,例如Getting alignment/attention during translation in 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以相当标准的方式进行训练的。
答案 0 :(得分:0)
您可以在翻译脚本中添加--attn_debug
参数以查看注意力权重。
translate.py ... \
-attn_debug \
...