This post continues the discussion on multimodal alignment, where the goal is to identify and model connections between all elements across modalities.
The challenge can be divided into 3 sub-challenges: discrete alignment, which identifies correspondences between discrete elements, continuous alignment, which handles modalities when there are no explicitly segmented elements, and contextualized representations, which jointly perform alignment and representation learning so that elements are aligned while being embedded into a shared representation space. This post focuses on the third sub-challenge.
Specifically, this post dives into contextualized "sequence" representation: how to encode a sequence so that each element's representation reflects the context around it. First, it quickly reviews the core components of the Transformer architecture, including self-attention, positional embeddings, and multi-head attention. Then, it examines the key ideas behind BERT, before concluding with how Transformers perform sequence-to-sequence (seq2seq) translation.
This post serves as background on the Transformer, including what it is, how it works, and its applications and extension. All the examples use unimodal text data for simplicity. Therefore, if you are already familiar with Transformers and BERT, feel free to skip ahead to the next post, where these concepts are extended to multimodal data.
This section briefly reviews the core components of the Transformer, including self-attention, positional embeddings, and multi-head attention, rather than the complete Transformer architecture.
For each target input in the sequence, self-attention computes its output representation as a weighted sum over the sequence, where the attention weight distribution reflects the relevance of each element in the sequence to the target one. In the Transformer's version of self-attention, every input is projected into three vectors using learned weight matrices: Query, Key, and Value.
The attention weight between a target element and each one in the sequence comes from the scaled dot product of the target element's query and another element's key, and the output representation is the weighted sum of all elements' value vectors using those computed attention weights.
A single attention computation may not capture every kind of relationship in the data. Multi-head attention runs several independent query/key/value projections in parallel, each with its own weight matrices, letting the model attend simultaneously to multiple subspaces of the input before linearly combining the per-head outputs into the final representation.


Because a self-attention module has no built-in notion of order, that is, shuffling the input would not change the computation, the model needs an explicit way to encode position information. A simple, naive approach represents each position as a one-hot vector, passes it through a linear embedding, and combines it with the token embedding, either by summing the two or concatenating them.
Bidirectional Encoder Representations from Transformers (BERT) applies the Transformer's self-attention architecture to build a language representation model with several advantages:
[SEP] token.BERT learns deep bidirectional representations by pre-training with two self-supervised objectives:
[MASK] token, and the model is trained to predict the original tokens from their bidirectional context.To make this possible, BERT's input embeddings are the sum of three separate embeddings: the token embeddings, the segmentation (sentence A/B) embeddings, and the position embeddings.
The same pretrained architecture adapts to many downstream tasks by adding a small task-specific output layer:
[CLS].[CLS].Transformers can also perform sequence-to-sequence tasks like machine translation by connecting three pieces of attention:
Together, these components let a Transformer read an entire source sequence, generate a target sequence one token at a time, and continually reference the source while doing so.
Self-attention is the modern default for contextualizing sequences. Compared to bi-directional LSTMs and convolutions, self-attention is parallelizable, captures long-range dependencies, and computes attention weights dynamically.
BERT demonstrates the importance of bidirectional pre-training for language representations. To achieve this, BERT pre-trains on two objectives: Masked Language Model and Next Sentence Prediction.