(BackBack)
ML//7 min read

From Seq2Seq to Transformer - Part.2

The transformer model represents another significant leap in the NLP field after the Seq2Seq model and the attention mechanism.

Transformer Model

The transformer model represents another significant leap in the NLP field after the Seq2Seq model and the attention mechanism. The main idea behind the transformer model is to remove the RNN structure from the Seq2Seq + attention model. Hence, the transformer is a model that exclusively uses attention operations (and FC operations) without relying on RNNs.

Why Remove RNNs?

Sequential data not only depends on the values of each item but also their order. For instance, "John loves Sarah" and "Sarah loves John" both contain the same words, but their order results in different meanings. Thus, a model that can handle sequential data must also be able to process this order information.

RNNs were suitable for handling sequential data since they accept inputs sequentially and update their hidden states. Models like Seq2Seq leveraged RNNs mainly because there weren't many alternatives at the time.

However, RNNs have several disadvantages:

  1. Parallelization Issue: Due to its inherent sequential nature, RNNs cannot be parallelized. This makes training on large datasets impractical due to extended training times.

  2. Long Distance Dependency Problem: Relationships between distant items in a sequence don't learn well due to the gradient vanishing/exploding problem.

The attention operation provides a solution to both of these issues. It allows for parallelization and directly compares each query with all keys, addressing the long-distance dependency issue. Theoretically, a model using only attention operations should learn faster than an RNN-based model, allowing for training on larger datasets and producing better performance.

How RNNs were Eliminated: Positional Encoding

While RNN structures naturally incorporated order information, attention operations do not consider it. Therefore, in the transformer model, a vector containing positional information, termed 'positional encoding,' of the same dimension as the input embedding is added.

Fig.01 Positional Encoding
Fig.01 Positional Encoding

To incorporate order information, the transformer model adds positional encoding to the input embedding.

f(p)={sin(p1000i/d)(i=2k)cos(p1000(i1/d))(i=2k+1)f(p)=\begin{cases} \sin(\frac{p}{1000^{i/d}}) &(i=2k) \\ \cos(\frac{p}{1000^{(i-1/d)}}) &(i=2k+1)\end{cases}

The positional encoding ensures that even the same word will have different embedding vectors based on its position, successfully conveying positional information.

However, the method mentioned above isn't the only way to generate vectors with positional information.

Structure of the Transformer Model

Fig.02 Transformer
Fig.02 Transformer

Like the Seq2Seq model, the transformer model comprises an encoder and a decoder.

Fig.03 Transformer Encoder
Fig.03 Transformer Encoder

The transformer encoder consists of multiple stacked encoder layers. The output of a previous encoder layer serves as the input to the next encoder layer.

Each encoder layer is divided into a multi-head self-attention sub-layer (orange block) and a position-wise fully connected feed-forward sub-layer (blue block). The output of each sub-layer is passed through a residual connection, added to its input, and then normalized (yellow block) before being fed to the next sub-layer.

Fig.04 Transformer Decoder
Fig.04 Transformer Decoder

The transformer decoder consists of multiple stacked decoder layers. The output of a previous decoder layer serves as the input to the next decoder layer.

Each decoder layer comprises a masked multi-head self-attention sub-layer (first orange block), a multi-head cross-attention sub-layer (second orange block), and a position-wise fully connected feed-forward sub-layer (blue block). Similar to the encoder, the output of each sub-layer in the decoder undergoes a residual connection, is added to its input, and then normalized (yellow block) before being fed to the subsequent sub-layer.

Let's delve deeper into each operation.

Multi-head Attention Operation

Multi-head attention operation is a technique designed to execute the attention operation in parallel multiple times. Here, "head" refers to the entity executing the attention operation. Thus, "multi-head" signifies performing attention operations multiple times (on the same sequence). Performing attention operations hh times on the same source indicates the ability to focus with hh different "focus methods", or in more layman's terms, viewing the sequence from hh different "perspectives".

To be specific, when the sequence length is LL and the dimension of the input embedding is dd, a multi-head attention operation with hh heads works as follows:

  1. Inputs QQ, KK, and VV are given.

    • In the encoder layer and decoder layer using multi-head self attention,Q=K=V=XQ = K = V = X (the output from the previous layer).

    • In the decoder layer using multi-head cross attention, Q=XQ = X (the output from the previous layer) and K=V=YK = V = Y (the output from the encoder).

    • XX, YY, and ZZ are matrices of size(L,d)(L, d).

  2. Creating Query, Key, Value for Each Head:

    • If all heads use the same query, key, and value, it won't provide a multi-perspective multi-head attention operation. Thus, weight matrices WiQW^Q_i , WiKW^K_i, and WiVW^V_i are multiplied with QQ, KK, and VV respectively to generate the query, key, and value for the ii th-th head: QiQ_i, KiK_i, and ViV_i.

    • These matrices are of dimensions: (d,dk)(d, d_k), (d,dk)(d, d_k), and (d,dv)(d, d_v)respectively. The resulting matrices QiQ_i, KiK_i, and ViV_i are of dimensions(L,dk)(L, d_k), (L,dk)(L, d_k), and (L,dv)(L, d_v) respectively.

    • dkd_k is the dimension of the query and key for each head, while dvd_v is the dimension of the value.

    • During actual implementation, rather than multiplying hh matrices WiQW_i^Q, WiKW_i^K, and WiVW_i^V individually, matrices WQW^Q, WKW^K, and WVW^V of size (d,h×dk)(d, h \times d_k) are used. After multiplication, the result is split into the desired sizes.

  3. Calculate Attention:

    • Each head performs the scaled dot-product attention operation using QiQ_i, KiK_i, and ViV_i.
    headi=Attention(Qi,Ki,Vi)=softmax(QiKiTdk)Vihead_i = Attention(Q_i,K_i,V_i)=softmax(\frac{Q_iK_i^T}{\sqrt{d_k}})V_i
    • The resultant attention value is a matrix of size (L,dv)(L, d_v).

    • Typically, during implementation, vector operations are used to calculate all at once rather than separately for each head.

  4. Concatenate Each Head's Attention Value:

    • All attention values from each head are concatenated, then multiplied with the weight matrix WOW^O. The result is the final output of the multi-head attention.
    MultiHead(Q,K,V)=Concat(head1,...,headh)WOMultiHead(Q,K,V)=Concat(head_1,...,head_h)W^O
    • WOW^O is a matrix of size (dv,d)=(d,d)(d_v, d)=(d,d).

It should be noted that the original Transformer paper used d=512,h=8,dv=64d=512,h=8,d_v=64.

Thanks to extensive vector operations, the cost of the multi-head attention operation is roughly similar to the cost of a single-head attention operation

Masked Attention Operation

In the decoder, a specialized form of attention operation called the masked multi-head self attention operation is used. This masking ensures that during attention calculations, the model cannot refer to future tokens in the sequence, thus mimicking the real-time generation of sequences.

Several methods can implement masking, but in the Transformer model, a mask matrix is utilized. The mask matrix MM is an (L×L)(L \times L) matrix of 00s and negative infinity ()(-\infty ), where LL is the length of the input sequence. The values in the matrix are defined as:

Given this mask matrix, the procedure for masked attention is:

  1. Compute Attention Scores: For the provided query QQ, key KK, and value VV, compute the scaled dot product attention scores SS:
S=Q×KTdS = \frac{Q \times K^T}{\sqrt{d}}

Where dd is the dimension of the query and key vectors.

  1. Apply Mask: To the attention scores, add the mask matrix MM:

    Smasked=S+MS_{masked} = S + M
  2. Compute Attention Distribution: Calculate the softmax of the masked attention scores SmaskedS_{masked} to obtain the attention distribution α\alpha.

    α=softmax(Smasked)\alpha=softmax(S_{masked})
  3. Calculate Attention Value: Multiply the attention distribution α\alpha with the value matrix VV to obtain the attention value VattentionV_{attention}.

Vattention=αVV_{attention} = \alpha V

Position-wise Fully Connected Feed-Forward Operation

The position-wise fully connected feed-forward operation in the Transformer model can be defined using linear operations followed by a ReLU activation. Specifically:

FF(X)=ReLU(X×W1+b1)×W2+b2FF(X) = ReLU(X \times W_1 + b_1) \times W_2 + b_2

Where:

Layer Normalization Operation

In both the Transformer's encoder and decoder layers, the output of each sub-layer undergoes a residual connection followed by layer normalization. Mathematically, if XX is the input to a sublayer with output YY, then the output OO of the layer normalization is:

O=LayerNorm(X+Y)O = LayerNorm(X + Y)

Layer normalization is a type of normalization that operates across the features (as opposed to batch normalization, which operates across batches) and is commonly used in NLP tasks and the Transformer architecture. It helps in stabilizing the activations in the network and speeds up training.