Vocabulary Learning via Optimal Transport for Neural Machine Translation论文解读

75 篇文章 7 订阅
61 篇文章 2 订阅

在这里插入图片描述

abstract

token vocabulary的选择对机器翻译结果的影响比较大
本文研究什么样才是好的词表,并且能不能不通过训练直接找到最优的词表
首先提供一种从信息理论的角度来分析词表的作用
通过optimal transport的思路来找到最佳token词典,并且有一个合适的词表大小,不用训练
降低70%的词表大小,取得0.5bleu提升
相对于BPE-search,搜索时间从384gpu->30gpu,这个的意思有些没看懂,是指vocabularies的时间。
背景:
当所有样本等几率出现的情况下,熵达到最大值(所有可能的事件等概率时不确定性最高)
对于样本等几率分布而言,样本数越大,熵值越大(可能的事件越多,不确定性越高)

1. intoduction

  1. 用更小的词表来压缩数据,减少熵,可以使语料更容易训练和预测,降低过拟合的风险。
  2. byte-pair encoding BPE:就是选择最高频的sub-words或者sub-word pieces来做的。
  3. 因为词表大小也会影响下游任务的表现,如何选择合适的大小呢,只考虑频率也是不妥的,通过实验来做,需要一个一个来做,比较繁琐,导致大家现在都是只选择30k-40k左右。
  4. 一般来说减低熵和词表大小是矛盾的,词表越大,熵约大。
  5. 根据语料的熵和词表大小,选择合适的词表,并且可以再polynomial时间内解决。
  6. muv作为评价指标,muv=熵在词表大小上的微分=diff(熵)/m,m为词表大小的diff。
  7. muv和下游任务的表现基本上是正相关的,所以优化muv就是优化下游任务了?所以可以直接算出muv最大值,拿到最佳词表大小,就可以拿到一个比较好的下游表现了?
  8. 通过找到最大的muv来找到词表,并且将其转化成optimal transport problem来做,相当于是从字符的分布到词表的分布中找到最佳转移矩阵optimal transport matrix,最终是通过这个矩阵来产生词表。
    在这里插入图片描述

2. related work

  1. 字级别的词典有大小限制,会遇到oov
  2. 有基于char的,BPE等优化方法,拿到subword-level级别的词表,例如happy可能和happier比较相似,在char-lvel,并且在小的粒度上就没有oov了,还有sentencepiece方法等
  3. 一般大家只注意到频率,没有注意到词表大小,并且确定词表是一个很耗资源的工作

3. MUV是啥

  1. muv定义了增加词表大小带来的收益。
  2. 通过实验证明了muv和bleu的关系,验证了用muv来衡量的正确性。

在这里插入图片描述

4 Maximizing MUV via Optimal Transport

4.1 idea
  1. 词表是离散的,找最佳muv的搜索空间太大。
  2. 简化:从固定大小的词表中找到最佳muv,固定的步长还是固定的大小?
    S:递增整数数列
    t:timestep,代表diff t
    总结:
    固定t,找到最佳muv(找到最大熵的词表)
    枚举所有t
    在这里插入图片描述
  • 词表到转移矩阵
    在这里插入图片描述

不同的转移矩阵有不同的转移cost,目标是找到最小的cost,例如负的熵等。

4.2 optimal transport solution
  • 给定词表,要找到最大熵,公式可转化为:
    在这里插入图片描述

  • 合并,减少token太大带来的计算量问题

  • 计算转移矩阵的每一行的和和每一列的和干啥?

  • 非平衡交叉熵的正则最优化转移问题?

  • 可以用Sinkhorn算法解决,每个时间t,产生一个新的词表,根据转移矩阵p计算交叉熵分数,根据熵挑选词表,输出词表。

4.3 implementation

  1. 根据频率将所有token排序,用的是BPE方法生成的token,例如利用BPE-100K作为候选,其他的也可以,基本可以带来类似的结果。
  2. 用他们的概率初始化L
  3. 每个timestep,根据当前的词表及其转移矩阵计算最大的熵。
  4. 枚举t,选择合适的词表。
  5. 有词表后,使用贪心算法去对句子进行编码。
    • 将句子分裂到char粒度的token
    • 和并consecutive tokens到一个,如果他在词表中
    • oov token会被分到更细的粒度

在这里插入图片描述

5. experience

  1. 就用VOLT来选词表,效果居然就好了
    在这里插入图片描述

  2. 对于低资源的数据集,BPE-1k通过很多实验拿到词表,VOLT通过一次实验,0.5CPU hours,30GPU hours就找到了最优的词表。
    在这里插入图片描述

  3. VOLT是一个绿色的词表化解决方案,通过比较BPE-search:一种从BPE生成的词表中,根据BLEU分数选择最优词表的方法,来证明,VOLT在一个cpu花费少量时间就能算出来0.5h,BPE-search在需要在GPU上花费上百小时。
    在这里插入图片描述

5.3 讨论
  1. VOLT生成的词表能达到SOTA结果。
  2. 领先sentencePiece和wordPiece方法非常多,这两个是分到细粒度词的一种分词方法。
  3. 各种架构都适应,transformer-big结构,seq2seq结构都适应。
  4. VOLT能在训练阶段带来加速,主要因为降低embedding参数的大小,对于softmax计算,因为在gpu中已经并行计算了,提升不大。
  5. VOLT和BPE词表高度重叠,相当于直接可以拿到BPE的结果?

6. 结论

不用训练,直接可能产生词表搜索的方法,利用信息论理论,通过两步优化,离散化目标函数和最优化转移方案来实现。实验表明效果非常有效。

  1. 构造目标函数
  2. optimal transport solution解目标函数极值的方法。
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
The code you provided defines a named tuple `Hypothesis` with two fields, `value` and `score`. This is a convenient way to store and manipulate hypotheses in the context of sequence-to-sequence models. The `NMT` class is a PyTorch module that implements a simple neural machine translation model. It consists of a bidirectional LSTM encoder, a unidirectional LSTM decoder, and a global attention mechanism based on Luong et al. (2015). Here's a breakdown of the code: ```python from collections import namedtuple import torch import torch.nn as nn import torch.nn.functional as F Hypothesis = namedtuple('Hypothesis', ['value', 'score']) class NMT(nn.Module): def __init__(self, src_vocab_size, tgt_vocab_size, emb_size, hidden_size): super(NMT, self).__init__() self.src_embed = nn.Embedding(src_vocab_size, emb_size) self.tgt_embed = nn.Embedding(tgt_vocab_size, emb_size) self.encoder = nn.LSTM(emb_size, hidden_size, bidirectional=True) self.decoder = nn.LSTMCell(emb_size + hidden_size, hidden_size) self.attention = nn.Linear(hidden_size * 2, hidden_size) self.out = nn.Linear(hidden_size, tgt_vocab_size) self.hidden_size = hidden_size def forward(self, src, tgt): batch_size = src.size(0) src_len = src.size(1) tgt_len = tgt.size(1) # Encode the source sentence src_embedded = self.src_embed(src) encoder_outputs, (last_hidden, last_cell) = self.encoder(src_embedded) # Initialize the decoder states decoder_hidden = last_hidden.view(batch_size, self.hidden_size) decoder_cell = last_cell.view(batch_size, self.hidden_size) # Initialize the attention context vector context = torch.zeros(batch_size, self.hidden_size, device=src.device) # Initialize the output scores outputs = torch.zeros(batch_size, tgt_len, self.hidden_size, device=src.device) # Decode the target sentence for t in range(tgt_len): tgt_embedded = self.tgt_embed(tgt[:, t]) decoder_input = torch.cat([tgt_embedded, context], dim=1) decoder_hidden, decoder_cell = self.decoder(decoder_input, (decoder_hidden, decoder_cell)) attention_scores = self.attention(encoder_outputs) attention_weights = F.softmax(torch.bmm(attention_scores, decoder_hidden.unsqueeze(2)).squeeze(2), dim=1) context = torch.bmm(attention_weights.unsqueeze(1), encoder_outputs).squeeze(1) output = self.out(decoder_hidden) outputs[:, t] = output return outputs ``` The `__init__` method initializes the model parameters and layers. It takes four arguments: - `src_vocab_size`: the size of the source vocabulary - `tgt_vocab_size`: the size of the target vocabulary - `emb_size`: the size of the word embeddings - `hidden_size`: the size of the encoder and decoder hidden states The model has four main components: - `src_embed`: an embedding layer for the source sentence - `tgt_embed`: an embedding layer for the target sentence - `encoder`: a bidirectional LSTM encoder that encodes the source sentence - `decoder`: a unidirectional LSTM decoder that generates the target sentence The attention mechanism is implemented in the `forward` method. It takes two arguments: - `src`: the source sentence tensor of shape `(batch_size, src_len)` - `tgt`: the target sentence tensor of shape `(batch_size, tgt_len)` The method first encodes the source sentence using the bidirectional LSTM encoder. The encoder outputs and final hidden and cell states are stored in `encoder_outputs`, `last_hidden`, and `last_cell`, respectively. The decoder is initialized with the final hidden and cell states of the encoder. At each time step, the decoder takes as input the embedded target word and the context vector, which is a weighted sum of the encoder outputs based on the attention scores. The decoder output and hidden and cell states are updated using the LSTMCell module. The attention scores are calculated by applying a linear transform to the concatenated decoder hidden state and encoder outputs, followed by a softmax activation. The attention weights are used to compute the context vector as a weighted sum of the encoder outputs. Finally, the decoder hidden state is passed through a linear layer to produce the output scores for each target word in the sequence. The output scores are stored in the `outputs` tensor and returned by the method.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值