# The Transformer Attention Mechanism

Before the introduction of the Transformer model, the use of attention for neural machine translation was being implemented by RNN-based encoder-decoder architectures. The Transformer model revolutionized the implementation of attention by dispensing of recurrence and convolutions and, alternatively, relying solely on a self-attention mechanism.

We will first be focusing on the Transformer attention mechanism in this tutorial, and subsequently reviewing the Transformer model in a separate one.

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

After completing this tutorial, you will know:

How the Transformer attention differed from its predecessors.

How the Transformer computes a scaled-dot product attention.

How the Transformer computes multi-head attention.

Let’s get started.

## Tutorial Overview

This tutorial is divided into two parts; they are:

Introduction to the Transformer Attention

The Transformer Attention

Scaled-Dot Product Attention

Multi-Head Attention

## Prerequisites

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

The concept of attention

The attention mechanism

The Bahdanau attention mechanism

The Luong attention mechanism

## Introduction to the Transformer Attention

We have, thus far, familiarised ourselves with the use of an attention mechanism in conjunction with an RNN-based encoder-decoder architecture. We have seen that two of the most popular models that implement attention in this manner have been those proposed by Bahdanau et al. (2014) and Luong et al. (2015).

The Transformer architecture revolutionized the use of attention by dispensing of recurrence and convolutions, on which the formers had extensively relied.

… the Transformer is the first transduction model relying entirely on self-attention to compute representations of its input and output without using sequence-aligned RNNs or convolution.

– Attention Is All You Need, 2017.

In their paper, Attention Is All You Need, Vaswani et al. (2017) explain that the Transformer model, alternatively, relies solely on the use of self-attention, where the representation of a sequence (or sentence) is computed by relating different words in the same sequence.

Self-attention, sometimes called intra-attention is an attention mechanism relating different positions of a single sequence in order to compute a representation of the sequence.

– Attention Is All You Need, 2017.

The Transformer Attention

The main components in use by the Transformer attention are the following:

$mathbfq$ and $mathbfk$ denoting vectors of dimension, $d_k$, containing the queries and keys, respectively.

$mathbfv$ denoting a vector of dimension, $d_v$, containing the values.

$mathbfQ$, $mathbfK$ and $mathbfV$ denoting matrices packing together sets of queries, keys and values, respectively.

$mathbfW^Q$, $mathbfW^K$ and $mathbfW^V$ denoting projection matrices that are used in generating different subspace representations of the query, key and value matrices.

$mathbfW^O$ denoting a projection matrix for the multi-head output.

In essence, the attention function can be considered as a mapping between a query and a set of key-value pairs, to an output.

The output is computed as a weighted sum of the values, where the weight assigned to each value is computed by a compatibility function of the query with the corresponding key.

– Attention Is All You Need, 2017.

Vaswani et al. propose a scaled dot-product attention, and then build on it to propose multi-head attention. Within the context of neural machine translation, the query, keys and values that are used as inputs to the these attention mechanisms, are different projections of the same input sentence.

Intuitively, therefore, the proposed attention mechanisms implement self-attention by capturing the relationships between the different elements (in this case, the words) of the same sentence.

## Scaled Dot-Product Attention

The Transformer implements a scaled dot-product attention, which follows the procedure of the general attention mechanism that we had previously seen.

As the name suggests, the scaled dot-product attention first computes a dot product for each query, $mathbfq$, with all of the keys, $mathbfk$. It, subsequently, divides each result by $sqrtd_k$ and proceeds to apply a softmax function. In doing so, it obtains the weights that are used to scale the values, $mathbfv$.

In practice, the computations performed by the scaled dot-product attention can be efficiently applied on the entire set of queries simultaneously. In order to do so, the matrices, $mathbfQ$, $mathbfK$ and $mathbfV$, are supplied as inputs to the attention function:

$$textattention(mathbfQ, mathbfK, mathbfV) = textsoftmax left( fracQK^Tsqrtd_k right) V$$

Vaswani et al. explain that their scaled dot-product attention is identical to the multiplicative attention of Luong et al. (2015), except for the added scaling factor of $tfrac1sqrtd_k$.

This scaling factor was introduced to counteract the effect of having the dot products grow large in magnitude for large values of $d_k$, where the application of the softmax function would then return extremely small gradients that would lead to the infamous vanishing gradients problem. The scaling factor, therefore, serves to pull the results generated by the dot product multiplication down, hence preventing this problem.

Vaswani et al. further explain that their choice of opting for multiplicative attention instead of the additive attention of Bahdanau et al. (2014), was based on the computational efficiency associated with the former.

… dot-product attention is much faster and more space-efficient in practice, since it can be implemented using highly optimized matrix multiplication code.

– Attention Is All You Need, 2017.

The step-by-step procedure for computing the scaled-dot product attention is, therefore, the following:

Compute the alignment scores by multiplying the set of queries packed in matrix, $mathbfQ$,with the keys in matrix, $mathbfK$. If matrix, $mathbfQ$, is of size $m times d_k$ and matrix, $mathbfK$, is of size, $n times d_k$, then the resulting matrix will be of size $m times n$:

$$

mathbfQK^T =

beginbmatrix

e_11 & e_12 & dots & e_1n \

e_21 & e_22 & dots & e_2n \

vdots & vdots & ddots & vdots \

e_m1 & e_m2 & dots & e_mn \

endbmatrix

$$

Scale each of the alignment scores by $tfrac1sqrtd_k$:

$$

fracmathbfQK^Tsqrtd_k =

beginbmatrix

tfrace_11sqrtd_k & tfrace_12sqrtd_k & dots & tfrace_1nsqrtd_k \

tfrace_21sqrtd_k & tfrace_22sqrtd_k & dots & tfrace_2nsqrtd_k \

vdots & vdots & ddots & vdots \

tfrace_m1sqrtd_k & tfrace_m2sqrtd_k & dots & tfrace_mnsqrtd_k \

endbmatrix

$$

And follow the scaling process by applying a softmax operation in order to obtain a set of weights:

$$

textsoftmax left( fracmathbfQK^Tsqrtd_k right) =

beginbmatrix

textsoftmax ( tfrace_11sqrtd_k & tfrace_12sqrtd_k & dots & tfrace_1nsqrtd_k ) \

textsoftmax ( tfrace_21sqrtd_k & tfrace_22sqrtd_k & dots & tfrace_2nsqrtd_k ) \

vdots & vdots & ddots & vdots \

textsoftmax ( tfrace_m1sqrtd_k & tfrace_m2sqrtd_k & dots & tfrace_mnsqrtd_k ) \

endbmatrix

$$

Finally, apply the resulting weights to the values in matrix, $mathbfV$, of size, $n times d_v$:

$$

beginaligned

& textsoftmax left( fracmathbfQK^Tsqrtd_k right) cdot mathbfV \

=&

beginbmatrix

textsoftmax ( tfrace_11sqrtd_k & tfrace_12sqrtd_k & dots & tfrace_1nsqrtd_k ) \

textsoftmax ( tfrace_21sqrtd_k & tfrace_22sqrtd_k & dots & tfrace_2nsqrtd_k ) \

vdots & vdots & ddots & vdots \

textsoftmax ( tfrace_m1sqrtd_k & tfrace_m2sqrtd_k & dots & tfrace_mnsqrtd_k ) \

endbmatrix

cdot

beginbmatrix

v_11 & v_12 & dots & v_1d_v \

v_21 & v_22 & dots & v_2d_v \

vdots & vdots & ddots & vdots \

v_n1 & v_n2 & dots & v_nd_v \

endbmatrix

endaligned

$$

## Multi-Head Attention

Building on their single attention function that takes matrices, $mathbfQ$, $mathbfK$, and $mathbfV$, as input, as we have just reviewed, Vaswani et al. also propose a multi-head attention mechanism.

Their multi-head attention mechanism linearly projects the queries, keys and values $h$ times, each time using a different learned projection. The single attention mechanism is then applied to each of these $h$ projections in parallel, to produce $h$ outputs, which in turn are concatenated and projected again to produce a final result.

The idea behind multi-head attention is to allow the attention function to extract information from different representation subspaces, which would, otherwise, not be possible with a single attention head.

The multi-head attention function can be represented as follows:

$$textmultihead(mathbfQ, mathbfK, mathbfV) = textconcat(texthead_1, dots, texthead_h) mathbfW^O$$

Here, each $texthead_i$, $i = 1, dots, h$, implements a single attention function characterized by its own learned projection matrices:

$$texthead_i = textattention(mathbfQW^Q_i, mathbfKW^K_i, mathbfVW^V_i)$$

The step-by-step procedure for computing multi-head attention is, therefore, the following:

Compute the linearly projected versions of the queries, keys and values through a multiplication with the respective weight matrices, $mathbfW^Q_i$, $mathbfW^K_i$ and $mathbfW^V_i$, one for each $texthead_i$.

Apply the single attention function for each head by (1) multiplying the queries and keys matrices, (2) applying the scaling and softmax operations, and (3) weighting the values matrix, to generate an output for each head.

Concatenate the outputs of the heads, $texthead_i$, $i = 1, dots, h$.

Apply a linear projection to the concatenated output through a multiplication with the weight matrix, $mathbfW^O$, to generate the final result.

## 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

Attention Is All You Need, 2017.

Neural Machine Translation by Jointly Learning to Align and Translate, 2014.

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

## Summary

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

Specifically, you learned:

How the Transformer attention differed from its predecessors.

How the Transformer computes a scaled-dot product attention.

How the Transformer computes multi-head attention.

Do you have any questions?

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

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