基于Pytorch理解attention decoder网络结构

2019.1.4更新

Pytorch的tutorials上目前的attention不是论文上原本的attention,是有问题的,详见讨论:https://github.com/spro/practical-pytorch/issues/84

可以看https://github.com/spro/practical-pytorch/blob/master/seq2seq-translation/seq2seq-translation.ipynb 这个链接上的代码,是符合原论文的attention.

pytorch目前用于计算权重的输入都是decoder里的信息(见下文的图), 没有包含encoder的信息,实际上应该用当前 目标语言隐层状态(q)*encoder_outputs(k).

看来目前的Pytorch还不是那么的成熟!!

___________________________________________________________________________________________

普通encoder-decoder模型的decoder只使用encoder最后输出的唯一向量(包含翻译对象的信息)来作为输入,

而attention decoder将encoder所有outputs的向量都作为输入,这种方法显然能覆盖到更多的信息。

同时为了确定哪个encoder的output对当前词decoder的影响更大,使用decoder的前一个隐层+decoder的输入词嵌入(翻译目标的词嵌入)组合生成encoder-outputs的权值,并使用反向传播更新参数。

网络架构图如下: 详见官方教程https://pytorch.org/tutorials/intermediate/seq2seq_translation_tutorial.html

attention_decoder代码解析:

class AttnDecoderRNN(nn.Module):
    def __init__(self, hidden_size, output_size, dropout_p=0.1, max_length=MAX_LENGTH):
        super(AttnDecoderRNN, self).__init__()
        self.hidden_size = hidden_size
        self.output_size = output_size  # 另一种语言的词汇量
        self.dropout_p = dropout_p
        self.max_length = max_length

        self.embedding = nn.Embedding(self.output_size, self.hidden_size)
        self.attn = nn.Linear(self.hidden_size * 2, self.max_length)
        self.attn_combine = nn.Linear(self.hidden_size * 2, self.hidden_size)
        self.dropout = nn.Dropout(self.dropout_p)
        self.gru = nn.GRU(self.hidden_size, self.hidden_size)
        self.out = nn.Linear(self.hidden_size, self.output_size)

    def forward(self, input, hidden, encoder_outputs):  # forward的参数是decoder的输入
        # decoder的input是另一种语言的词汇,要么是target,要么是上一个单元返回的output中概率最大的一个
        # 初始的hidden用的是encoder的最后一个hidden输出
        embedded = self.embedding(input).view(1, 1, -1)
        embedded = self.dropout(embedded)
        # 将embedded的256词向量和hidden的256词向量合在一起,变成512维向量
        # 再用线性全连接变成10维(最长句子词汇数),在算softmax,看
        attn_weight = F.softmax(
            self.attn(torch.cat((embedded[0], hidden[0]), 1)), dim=1
        )
        # torch.cat用于粘贴,dim=1指dim1方向粘贴
        # torch.bmm是批矩阵乘操作,attention里将encoder的输出和attention权值相乘
        # bmm: (1,1,10)*(1,10,256),权重*向量,得到attention向量
        # unsqueeze用于插入一个维度(修改维度)
        attn_applied = torch.bmm(attn_weight.unsqueeze(0),
                                 encoder_outputs.unsqueeze(0))
        output = torch.cat((embedded[0], attn_applied[0]), 1)
        output = self.attn_combine(output).unsqueeze(0)
        output = F.relu(output)
        output, hidden = self.gru(output, hidden)

        output = F.log_softmax(self.out(output[0]), dim=1)
        return output, hidden, attn_weight

    def initHidden(self):
        return torch.zeros(1, 1, self.hidden_size, device=device)

猜你喜欢

转载自blog.csdn.net/ygfrancois/article/details/85342265