在OpenNMT-py中进行翻译时获得对齐/注意 [英] Getting alignment/attention during translation in OpenNMT-py

查看:310
本文介绍了在OpenNMT-py中进行翻译时获得对齐/注意的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人在Opennmt-py中翻译时知道如何获得比对权重吗?通常,唯一的输出是结果语句,我已经尝试为注意力权重找到调试标志或类似标志.到目前为止,我一直没有成功.

Does anyone know how to get the alignments weights when translating in Opennmt-py? Usually the only output are the resulting sentences and I have tried to find a debugging flag or similar for the attention weights. So far, I have been unsuccessful.

推荐答案

您可以获取注意矩阵.请注意,它与 alignment (对齐)不同,后者是来自统计(而非神经)机器翻译的术语.

You can get the attention matrices. Note that it is not the same as alignment which is a term from statistical (not neural) machine translation.

github上的一个线程对此进行了讨论.这是讨论的摘录.当您从该模式获得翻译时,注意力将集中在attn字段中.

There is a thread on github discussing it. Here is a snippet from the discussion. When you get the translations from the mode, the attentions are in the attn field.

import onmt
import onmt.io
import onmt.translate
import onmt.ModelConstructor
from collections import namedtuple

# Load the model.
Opt = namedtuple('Opt', ['model', 'data_type', 'reuse_copy_attn', "gpu"])

opt = Opt("PATH_TO_SAVED_MODEL", "text", False, 0)
fields, model, model_opt =  onmt.ModelConstructor.load_test_model(
    opt, {"reuse_copy_attn" : False})

# Test data
data = onmt.io.build_dataset(
    fields, "text", "PATH_TO_DATA", None, use_filter_pred=False)
data_iter = onmt.io.OrderedIterator(
    dataset=data, device=0,
    batch_size=1, train=False, sort=False,
    sort_within_batch=True, shuffle=False)

# Translator
translator = onmt.translate.Translator(
    model, fields, beam_size=5, n_best=1,
    global_scorer=None, cuda=True)

builder = onmt.translate.TranslationBuilder(
   data, translator.fields, 1, False, None)

batch = next(data_iter)
batch_data = translator.translate_batch(batch, data)
translations = builder.from_batch(batch_data)
translations[0].attn # <--- here are the attentions

这篇关于在OpenNMT-py中进行翻译时获得对齐/注意的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆