# The Luong Attention Mechanism

The Luong attention sought to introduce several improvements over the Bahdanau model for neural machine translation, particularly by introducing two new classes of attentional mechanisms: a global approach that attends to all source words, and a local approach that only attends to a selected subset of words in predicting the target sentence.

In this tutorial, you will discover the Luong attention mechanism for neural machine translation.

After completing this tutorial, you will know:

The operations performed by the Luong attention algorithm.

How the global and local attentional models work.

How the Luong attention compares to the Bahdanau attention.

Let’s get started.

## Tutorial Overview

This tutorial is divided into five parts; they are:

Introduction to the Luong Attention

The Luong Attention Algorithm

The Global Attentional Model

The Local Attentional Model

Comparison to the Bahdanau Attention

## Prerequisites

For this tutorial, we assume that you are already familiar with:

The concept of attention

The attention mechanism

The Bahdanau attention mechanism

## Introduction to the Luong Attention

Luong et al. (2015) inspire themselves from previous attention models, to propose two attention mechanisms:

In this work, we design, with simplicity and effectiveness in mind, two novel types of attention-based models: a global approach which always attends to all source words and a local one that only looks at a subset of source words at a time.

– Effective Approaches to Attention-based Neural Machine Translation, 2015.

The global attentional model resembles the model of Bahdanau et al. (2014) in attending to all source words, but aims to simplify it architecturally.

The local attentional model is inspired from the hard and soft attention models of Xu et al. (2016), and attends to only a few of the source positions.

The two attentional models share many of the steps in their prediction of the current word, but differ mainly in their computation of the context vector.

Let’s first take a look at the overarching Luong attention algorithm, and then delve into the differences between the global and local attentional models afterwards.

## The Luong Attention Algorithm

The attention algorithm of Luong et al. performs the following operations:

The encoder generates a set of annotations, $H = mathbfh_i, i = 1, dots, T$, from the input sentence.

The current decoder hidden state is computed as: $mathbfs_t = textRNN_textdecoder(mathbfs_t-1, y_t-1)$. Here, $mathbfs_t-1$ denotes the previous hidden decoder state, and $y_t-1$ the previous decoder output.

An alignment model, $a(.)$ uses the annotations and the current decoder hidden state to compute the alignment scores: $e_t,i = a(mathbfs_t, mathbfh_i)$.

A softmax function is applied to the alignment scores, effectively normalizing them into weight values in a range between 0 and 1: $alpha_t,i = textsoftmax(e_t,i)$.

These weights together with the previously computed annotations are used to generate a context vector through a weighted sum of the annotations: $mathbfc_t = sum^T_i=1 alpha_t,i mathbfh_i$.

An attentional hidden state is computed based on a weighted concatenation of the context vector and the current decoder hidden state: $widetildemathbfs_t = tanh(mathbfW_c [mathbfc_t ; ; ; mathbfs_t])$.

The decoder produces a final output by feeding it a weighted attentional hidden state: $y_t = textsoftmax(mathbfW_y widetildemathbfs_t)$.

Steps 2-7 are repeated until the end of the sequence.

## The Global Attentional Model

The global attentional model considers all of the source words in the input sentence when generating the alignment scores and, eventually, when computing the context vector.

The idea of a global attentional model is to consider all the hidden states of the encoder when deriving the context vector, $mathbfc_t$.

– Effective Approaches to Attention-based Neural Machine Translation, 2015.

In order to do so, Luong et al. propose three alternative approaches for computing the alignment scores. The first approach is similar to Bahdanau’s and is based upon the concatenation of $mathbfs_t$ and $mathbfh_i$, while the second and third approaches implement multiplicative attention (in contrast to Bahdanau’s additive attention):

$$a(mathbfs_t, mathbfh_i) = mathbfv_a^T tanh(mathbfW_a [mathbfs_t ; ; ; mathbfh_i)]$$

$$a(mathbfs_t, mathbfh_i) = mathbfs^T_t mathbfh_i$$

$$a(mathbfs_t, mathbfh_i) = mathbfs^T_t mathbfW_a mathbfh_i$$

Here, $mathbfW_a$ is a trainable weight matrix and, similarly, $mathbfv_a$ is a weight vector.

Intuitively, the use of the dot product in multiplicative attention can be interpreted as providing a similarity measure between the vectors, $mathbfs_t$ and $mathbfh_i$, under consideration.

… if the vectors are similar (that is, aligned), the result of the multiplication will be a large value and the attention will be focused on the current t,i relationship.

– Advanced Deep Learning with Python, 2019.

The resulting alignment vector, $mathbfe_t$, is of variable-length according to the number of source words.

## The Local Attentional Model

In attending to all source words, the global attentional model is computationally expensive and could potentially become impractical for translating longer sentences.

The local attentional model seeks to address these limitations by focusing on a smaller subset of the source words to generate each target word. In order to do so, it takes inspiration from the hard and soft attention models of the image caption generation work of Xu et al. (2016):

Soft attention is equivalent to the global attention approach, where weights are softly placed over all the source image patches. Hence, soft attention considers the source image in its entirety.

Hard attention attends to a single image patch at a time.

The local attentional model of Luong et al. generates a context vector by computing a weighted average over the set of annotations, $mathbfh_i$, within a window centered over an aligned position, $p_t$:

$$[p_t – D, p_t + D]$$

While a value for $D$ is selected empirically, Luong et al. consider two approaches in computing a value for $p_t$:

Monotonic alignment: where the source and target sentences are assumed to be monotonically aligned and, hence, $p_t = t$.

Predictive alignment: where a prediction of the aligned position is based upon trainable model parameters, $mathbfW_p$ and $mathbfv_p$, and the source sentence length, $S$:

$$p_t = S cdot textsigmoid(mathbfv^T_p tanh(mathbfW_p, mathbfs_t))$$

To favour source words nearer to the window centre, a Gaussian distribution is centered around $p_t$ when computing the alignment weights.

This time round, the resulting alignment vector, $mathbfe_t$, has a fixed length of $2D + 1$.

## Comparison to the Bahdanau Attention

The Bahdanau model and the global attention approach of Luong et al. are mostly similar, but there are key differences between the two:

While our global attention approach is similar in spirit to the model proposed by Bahdanau et al. (2015), there are several key differences which reflect how we have both simplified and generalized from the original model.

– Effective Approaches to Attention-based Neural Machine Translation, 2015.

Most notably, the computation of the alignment scores, $e_t$, in the Luong global attentional model depends on the current decoder hidden state, $mathbfs_t$, rather than on the previous hidden state, $mathbfs_t-1$, as in the Bahdanau attention.

Luong et al. drop the bidirectional encoder in use by the Bahdanau model, and instead utilize the hidden states at the top LSTM layers for both encoder and decoder.

The global attentional model of Luong et al. investigates the use of multiplicative attention, as an alternative to the Bahdanau additive attention.

## Further Reading

This section provides more resources on the topic if you are looking to go deeper.

### Books

Advanced Deep Learning with Python, 2019.

### Papers

Effective Approaches to Attention-based Neural Machine Translation, 2015.

## Summary

In this tutorial, you discovered the Luong attention mechanism for neural machine translation.

Specifically, you learned:

The operations performed by the Luong attention algorithm.

How the global and local attentional models work.

How the Luong attention compares to the Bahdanau attention.

Do you have any questions?

Ask your questions in the comments below and I will do my best to answer.

The post The Luong Attention Mechanism appeared first on Machine Learning Mastery.