NLP FROM SCRATCH: TRANSLATION WITH A SEQUENCE TO SEQUENCE NETWORK AND ATTENTION

Author: Sean Robertson

这是NLP从头学习系列的第三篇。之前都是利用自己写的函数与类来处理数据并完成NLP分类与生成任务。本节将学习利用torchtxt来处理。该项目为:教会网络从法语翻译成英语。

这个实现主要根据 sequence to sequence network,其中有两个循环神经网络一起工作,将其中一个序列翻译成另一个序列。一个编码网络将输入编码成向量,一个解码器将向量恢复成新的序列。

 

 

为了提升模型性能,使用注意力机制 attention mechanism,使得解码器学着去关注输入序列的特定范围。

推荐阅读:

I assume you have at least installed PyTorch, know Python, and understand Tensors:

It would also be useful to know about Sequence to Sequence networks and how they work:

You will also find the previous tutorials on NLP From Scratch: Classifying Names with a Character-Level RNN and NLP From Scratch: Generating Names with a Character-Level RNN helpful as those concepts are very similar to the Encoder and Decoder models, respectively.

And for more, read the papers that introduced these topics:

 

需要的库

 1 from __future__ import unicode_literals, print_function, division
 2 from io import open
 3 import unicodedata
 4 import string
 5 import re
 6 import random
 7 
 8 import torch
 9 import torch.nn as nn
10 from torch import optim
11 import torch.nn.functional as F
12 
13 device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

载入数据文件

该项目的数据是一组英文到法文的翻译对。This question on Open Data Stack Exchange pointed me to the open translation site https://tatoeba.org/ which has downloads available at https://tatoeba.org/eng/downloads - and better yet, someone did the extra work of splitting language pairs into individual text files here: https://www.manythings.org/anki/。英语到法语的数据对太大,所以下载到 data/eng-fra.txt。Download the data from here and extract it to the current directory.

类似于character-level RNN中的字母编码,我们将使用独热编码代表语言中的每个word。与语言中可能存在的几十个字符相比,有更多的单词,因此编码向量要大得多。不过,为方便高效,把数据删减到每种语言只使用几千个单词。

 

需要每个单词的一个独立索引作为输入和输出。为此利用一个帮助函数Lang,有 index (word2index) and index → word (index2word) dictionaries。也有每个词的数目word2count来使用替代稀有单词。

SOS_token = 0
EOS_token = 1


class Lang:
    def __init__(self, name):
        self.name = name
        self.word2index = {}
        self.word2count = {}
        self.index2word = {0: "SOS", 1: "EOS"}
        self.n_words = 2  # Count SOS and EOS

    def addSentence(self, sentence):
        for word in sentence.split(' '):
            self.addWord(word)

    def addWord(self, word):
        if word not in self.word2index:
            self.word2index[word] = self.n_words
            self.word2count[word] = 1
            self.index2word[self.n_words] = word
            self.n_words += 1
        else:
            self.word2count[word] += 1

编码格式turn Unicode characters to ASCII,将所有字母小写化,修剪大部分符号。

# Turn a Unicode string to plain ASCII, thanks to
# https://stackoverflow.com/a/518232/2809427
def unicodeToAscii(s):
    return ''.join(
        c for c in unicodedata.normalize('NFD', s)
        if unicodedata.category(c) != 'Mn'
    )

# Lowercase, trim, and remove non-letter characters


def normalizeString(s):
    s = unicodeToAscii(s.lower().strip())
    s = re.sub(r"([.!?])", r" \1", s)
    s = re.sub(r"[^a-zA-Z.!?]+", r" ", s)
    return s

为读取数据,将文件划分为几行,然后划分每行为数据对,文件全是英文->其他语言,所以如果我们想翻译其他语言->英语。加入reverse标志来倒置。

def readLangs(lang1, lang2, reverse=False):
    print("Reading lines...")

    # Read the file and split into lines
    lines = open('data/%s-%s.txt' % (lang1, lang2), encoding='utf-8').\
        read().strip().split('\n')

    # Split every line into pairs and normalize
    pairs = [[normalizeString(s) for s in l.split('\t')] for l in lines]

    # Reverse pairs, make Lang instances
    if reverse:
        pairs = [list(reversed(p)) for p in pairs]
        input_lang = Lang(lang2)
        output_lang = Lang(lang1)
    else:
        input_lang = Lang(lang1)
        output_lang = Lang(lang2)

    return input_lang, output_lang, pairs

因为有大量的样本句子并并想快速训练,将把数据集精简为相对简短的句子。这里的最大长度是10个单词(包括结束标点符号),我们过滤掉翻译成“我是”或“他是”等形式的句子(包括前面替换的撇号)。

MAX_LENGTH = 10

eng_prefixes = (
    "i am ", "i m ",
    "he is", "he s ",
    "she is", "she s ",
    "you are", "you re ",
    "we are", "we re ",
    "they are", "they re "
)


def filterPair(p):
    return len(p[0].split(' ')) < MAX_LENGTH and \
        len(p[1].split(' ')) < MAX_LENGTH and \
        p[1].startswith(eng_prefixes)


def filterPairs(pairs):
    return [pair for pair in pairs if filterPair(pair)]

数据处理的全部步骤:

  • 读入文本文件并划分为行,然后行划分为数据对
  • 规范化文本,根据长度和内容过滤
  • 根据数据对中的句子制作词列表
def prepareData(lang1, lang2, reverse=False):
    input_lang, output_lang, pairs = readLangs(lang1, lang2, reverse)
    print("Read %s sentence pairs" % len(pairs))
    pairs = filterPairs(pairs)
    print("Trimmed to %s sentence pairs" % len(pairs))
    print("Counting words...")
    for pair in pairs:
        input_lang.addSentence(pair[0])
        output_lang.addSentence(pair[1])
    print("Counted words:")
    print(input_lang.name, input_lang.n_words)
    print(output_lang.name, output_lang.n_words)
    return input_lang, output_lang, pairs


input_lang, output_lang, pairs = prepareData('eng', 'fra', True)
print(random.choice(pairs))

 

The Seq2Seq Model

一个循环神经网络或者RNN,在序列上进行操作,并利用输出作为序列步骤中的输入。

Sequence to Sequence network, or seq2seq network, or Encoder Decoder network,包含了两个RNN:编码器和解码器。编码器读取输入序列,输出一个单一向量。解码器读取向量并输出一个序列。

 

 

和但RNN序列预测不同,其中每个输入对应于一个输出,而seq2seq模型对于序列长度和顺序没有要求,对于两种语言间的翻译比较理想。

考虑句子: “Je ne suis pas le chat noir” → “I am not the black cat”.输入序列的大多数单词有一个直接的翻译,但是稍微顺序不同。例如“chat noir” and “black cat”.因为“ne/pas”结构,在输入序列中含有一个单词。很难直接从输入的单词序列中产生正确的译文。

利用seq2seq模型编码可以创建一个单一向量,理想情况下,编码输入序列的“意义”到单一向量(N维句子空间中的一个点)。

 

编码器

seq2seq的编码器是一个RNN,输出是每个单词的一些值。对于每个输入单词,编码器输出一个向量和一个隐藏态,利用隐藏态作为下一个输入。

 

 

 

class EncoderRNN(nn.Module):
    def __init__(self, input_size, hidden_size):
        super(EncoderRNN, self).__init__()
        self.hidden_size = hidden_size

        self.embedding = nn.Embedding(input_size, hidden_size)
        self.gru = nn.GRU(hidden_size, hidden_size)

    def forward(self, input, hidden):
        embedded = self.embedding(input).view(1, 1, -1)
        output = embedded
        output, hidden = self.gru(output, hidden)
        return output, hidden

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

 

解码器

解码器是另一个RNN,输入向量输出翻译好的序列。

简单解码器:最简单的seq2seq解码器,仅仅利用编码器的最后输出。最后的输出有时称为context vector,因为从整个序列中编码内容。这个context向量也用作解码器的初始隐藏态。在每一步解码,解码器给定输入和隐藏态。初始的输入为起始串<SOS>,最开始的隐藏态是context vector(也就是编码器的最后的那个隐藏态)。

class DecoderRNN(nn.Module):
    def __init__(self, hidden_size, output_size):
        super(DecoderRNN, self).__init__()
        self.hidden_size = hidden_size

        self.embedding = nn.Embedding(output_size, hidden_size)
        self.gru = nn.GRU(hidden_size, hidden_size)
        self.out = nn.Linear(hidden_size, output_size)
        self.softmax = nn.LogSoftmax(dim=1)

    def forward(self, input, hidden):
        output = self.embedding(input).view(1, 1, -1)
        output = F.relu(output)
        output, hidden = self.gru(output, hidden)
        output = self.softmax(self.out(output[0]))
        return output, hidden

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

下面整的是基于注意力机制的解码器。

注意力使得解码器关注于编码器输出的不同部分。首先我们计算注意力权重,这个权重会被乘到编码器的输出上来实现加权。结果(attn_applied)将会包含输入序列特定位置的讯息。帮助解码器选择正确的输出。

计算注意力权重可以利用另一个前馈层attn,利用解码器的输入和隐藏态作为输入。因为训练数据中有各种尺寸的序列,为了训练我们选择一个最大长度的序列(输入长度,为了编码输出)。最大长度的序列将利用所有的注意力权重,短的序列将仅仅利用初始的几个权重。

 

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):
        embedded = self.embedding(input).view(1, 1, -1)
        embedded = self.dropout(embedded)

        attn_weights = F.softmax(
            self.attn(torch.cat((embedded[0], hidden[0]), 1)), dim=1)
        attn_applied = torch.bmm(attn_weights.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_weights

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

拓展注意力机制模型:There are other forms of attention that work around the length limitation by using a relative position approach. Read about “local attention” in Effective Approaches to Attention-based Neural Machine Translation.

 

训练

准备训练数据

为了训练,每个数据对需要一个输入tensor(输入序列的单词索引)和目标tensor(目标序列的单词索引)。

 

def indexesFromSentence(lang, sentence):
    return [lang.word2index[word] for word in sentence.split(' ')]


def tensorFromSentence(lang, sentence):
    indexes = indexesFromSentence(lang, sentence)
    indexes.append(EOS_token)
    return torch.tensor(indexes, dtype=torch.long, device=device).view(-1, 1)


def tensorsFromPair(pair):
    input_tensor = tensorFromSentence(input_lang, pair[0])
    target_tensor = tensorFromSentence(output_lang, pair[1])
    return (input_tensor, target_tensor)

 

训练模型

为了训练将输入序列输入到编码器,然后跟踪每个输出和最近的隐藏态。然后解码器给定<SOS>作为首先输入,最编码器后的隐藏态作为第一个隐藏态。

“Teacher forcing”是利用真实目标输出作为每次输入,而非利用解码器的预测作为下一次输入。利用teacher forcing使得收敛较快但是可能会不稳定。

你可以观察到教师强制网络的输出,这些网络以连贯的语法阅读,但却远远偏离了正确的翻译——直觉上,它已经学会了表示输出语法,并且一旦教师告诉它前几个单词,它就可以“拾起”它的意思,但它还没有正确地学会如何从翻译中造句。
由于Pythorch的自动求导机制,我们可以随机选择使用教师强制或不使用简单的if语句。将教师强制比率调高以使用更多的比率。

 

teacher_forcing_ratio = 0.5


def train(input_tensor, target_tensor, encoder, decoder, encoder_optimizer, decoder_optimizer, criterion, max_length=MAX_LENGTH):
    encoder_hidden = encoder.initHidden()

    encoder_optimizer.zero_grad()
    decoder_optimizer.zero_grad()

    input_length = input_tensor.size(0)
    target_length = target_tensor.size(0)

    encoder_outputs = torch.zeros(max_length, encoder.hidden_size, device=device)

    loss = 0

    for ei in range(input_length):
        encoder_output, encoder_hidden = encoder(
            input_tensor[ei], encoder_hidden)
        encoder_outputs[ei] = encoder_output[0, 0]

    decoder_input = torch.tensor([[SOS_token]], device=device)

    decoder_hidden = encoder_hidden

    use_teacher_forcing = True if random.random() < teacher_forcing_ratio else False

    if use_teacher_forcing:
        # Teacher forcing: Feed the target as the next input
        for di in range(target_length):
            decoder_output, decoder_hidden, decoder_attention = decoder(
                decoder_input, decoder_hidden, encoder_outputs)
            loss += criterion(decoder_output, target_tensor[di])
            decoder_input = target_tensor[di]  # Teacher forcing

    else:
        # Without teacher forcing: use its own predictions as the next input
        for di in range(target_length):
            decoder_output, decoder_hidden, decoder_attention = decoder(
                decoder_input, decoder_hidden, encoder_outputs)
            topv, topi = decoder_output.topk(1)
            decoder_input = topi.squeeze().detach()  # detach from history as input

            loss += criterion(decoder_output, target_tensor[di])
            if decoder_input.item() == EOS_token:
                break

    loss.backward()

    encoder_optimizer.step()
    decoder_optimizer.step()

    return loss.item() / target_length

帮助函数打印时间进度:

import time
import math


def asMinutes(s):
    m = math.floor(s / 60)
    s -= m * 60
    return '%dm %ds' % (m, s)


def timeSince(since, percent):
    now = time.time()
    s = now - since
    es = s / (percent)
    rs = es - s
    return '%s (- %s)' % (asMinutes(s), asMinutes(rs))

整个训练过程:

  • 开启计时
  • 初始化优化器和损失
  • 建立训练集
  • 记录损失用于画图

然后调用train函数多次并打印过程。

def trainIters(encoder, decoder, n_iters, print_every=1000, plot_every=100, learning_rate=0.01):
    start = time.time()
    plot_losses = []
    print_loss_total = 0  # Reset every print_every
    plot_loss_total = 0  # Reset every plot_every

    encoder_optimizer = optim.SGD(encoder.parameters(), lr=learning_rate)
    decoder_optimizer = optim.SGD(decoder.parameters(), lr=learning_rate)
    training_pairs = [tensorsFromPair(random.choice(pairs))
                      for i in range(n_iters)]
    criterion = nn.NLLLoss()

    for iter in range(1, n_iters + 1):
        training_pair = training_pairs[iter - 1]
        input_tensor = training_pair[0]
        target_tensor = training_pair[1]

        loss = train(input_tensor, target_tensor, encoder,
                     decoder, encoder_optimizer, decoder_optimizer, criterion)
        print_loss_total += loss
        plot_loss_total += loss

        if iter % print_every == 0:
            print_loss_avg = print_loss_total / print_every
            print_loss_total = 0
            print('%s (%d %d%%) %.4f' % (timeSince(start, iter / n_iters),
                                         iter, iter / n_iters * 100, print_loss_avg))

        if iter % plot_every == 0:
            plot_loss_avg = plot_loss_total / plot_every
            plot_losses.append(plot_loss_avg)
            plot_loss_total = 0

    showPlot(plot_losses)

 

打印结果

import matplotlib.pyplot as plt
plt.switch_backend('agg')
import matplotlib.ticker as ticker
import numpy as np


def showPlot(points):
    plt.figure()
    fig, ax = plt.subplots()
    # this locator puts ticks at regular intervals
    loc = ticker.MultipleLocator(base=0.2)
    ax.yaxis.set_major_locator(loc)
    plt.plot(points)

 

评估

评估和训练基本上是一样的,但是没有目标,所以我们只是在每一步将解码器的预测反馈给自己。每次它预测一个单词,我们就把它加到输出字符串中,如果它预测EOS标记,我们就停在那里。我们还存储解码器的注意输出,以便以后显示。

def evaluate(encoder, decoder, sentence, max_length=MAX_LENGTH):
    with torch.no_grad():
        input_tensor = tensorFromSentence(input_lang, sentence)
        input_length = input_tensor.size()[0]
        encoder_hidden = encoder.initHidden()

        encoder_outputs = torch.zeros(max_length, encoder.hidden_size, device=device)

        for ei in range(input_length):
            encoder_output, encoder_hidden = encoder(input_tensor[ei],
                                                     encoder_hidden)
            encoder_outputs[ei] += encoder_output[0, 0]

        decoder_input = torch.tensor([[SOS_token]], device=device)  # SOS

        decoder_hidden = encoder_hidden

        decoded_words = []
        decoder_attentions = torch.zeros(max_length, max_length)

        for di in range(max_length):
            decoder_output, decoder_hidden, decoder_attention = decoder(
                decoder_input, decoder_hidden, encoder_outputs)
            decoder_attentions[di] = decoder_attention.data
            topv, topi = decoder_output.data.topk(1)
            if topi.item() == EOS_token:
                decoded_words.append('<EOS>')
                break
            else:
                decoded_words.append(output_lang.index2word[topi.item()])

            decoder_input = topi.squeeze().detach()

        return decoded_words, decoder_attentions[:di + 1]

我们可以对训练集中的随机句子进行评估,并打印出输入、目标和输出,从而做出一些主观的质量判断:

def evaluateRandomly(encoder, decoder, n=10):
    for i in range(n):
        pair = random.choice(pairs)
        print('>', pair[0])
        print('=', pair[1])
        output_words, attentions = evaluate(encoder, decoder, pair[0])
        output_sentence = ' '.join(output_words)
        print('<', output_sentence)
        print('')

 

训练与评估


有了所有这些帮助函数(看起来像是额外的工作,但它使运行多个实验更容易),我们可以实际初始化网络并开始训练。
记住,输入的句子是经过严格筛选的。对于这个小数据集,我们可以使用由256个隐藏节点和单个GRU层组成的相对较小的网络。在MacBook CPU上运行大约40分钟后。将得到一些合理的结果。

hidden_size = 256
encoder1 = EncoderRNN(input_lang.n_words, hidden_size).to(device)
attn_decoder1 = AttnDecoderRNN(hidden_size, output_lang.n_words, dropout_p=0.1).to(device)

trainIters(encoder1, attn_decoder1, 75000, print_every=5000)

 

evaluateRandomly(encoder1, attn_decoder1)

 

注意力机制可视化

注意机制的一个有用特性是它的高解释性输出。因为它用于对输入序列的特定编码器输出进行加权,所以我们可以想象在每一个时间步骤中,查看网络最集中的位置。
只需运行plt.matshow(attentions)即可看到attention输出显示为一个矩阵,其中列是输入步骤,行是输出步骤:

output_words, attentions = evaluate(
    encoder1, attn_decoder1, "je suis trop froid .")
plt.matshow(attentions.numpy())

 为了获得更好的观看体验,将额外添加轴和标签:

def showAttention(input_sentence, output_words, attentions):
    # Set up figure with colorbar
    fig = plt.figure()
    ax = fig.add_subplot(111)
    cax = ax.matshow(attentions.numpy(), cmap='bone')
    fig.colorbar(cax)

    # Set up axes
    ax.set_xticklabels([''] + input_sentence.split(' ') +
                       ['<EOS>'], rotation=90)
    ax.set_yticklabels([''] + output_words)

    # Show label at every tick
    ax.xaxis.set_major_locator(ticker.MultipleLocator(1))
    ax.yaxis.set_major_locator(ticker.MultipleLocator(1))

    plt.show()


def evaluateAndShowAttention(input_sentence):
    output_words, attentions = evaluate(
        encoder1, attn_decoder1, input_sentence)
    print('input =', input_sentence)
    print('output =', ' '.join(output_words))
    showAttention(input_sentence, output_words, attentions)


evaluateAndShowAttention("elle a cinq ans de moins que moi .")

evaluateAndShowAttention("elle est trop petit .")

evaluateAndShowAttention("je ne crains pas de mourir .")

evaluateAndShowAttention("c est un jeune directeur plein de talent .")

 

 

 

 

 

 

 

 

 

Exercises

  • Try with a different datasetReplace the embeddings with pre-trained word embeddings such as word2vec or GloVe
    • Another language pair
    • Human → Machine (e.g. IOT commands)
    • Chat → Response
    • Question → Answer
  • Try with more layers, more hidden units, and more sentences. Compare the training time and results.
  • If you use a translation file where pairs have two of the same phrase (am test \t am test), you can use this as an autoencoder. Try this:
    • Train as an autoencoder
    • Save only the Encoder network
    • Train a new Decoder for translation from there
posted @ 2020-04-16 19:58  三年一梦  阅读(480)  评论(0)    收藏  举报