Lecture Note: Multimodal Alignment 2 - Contextualized Representation

11-777 Multimodal Machine Learning CMU Fall 2023
Instructor: Dr. Louis-Philippe Morency | Co-lecturer: Dr. Paul Pu Liang
Author: Yi-Jing Sie

Overview

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.

Transformer - Core Components

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.

Self-Attention

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.

Multi-Head Attention

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.

Grid Image
Grid Image

Position Embeddings

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.

BERT - Language Pretraining

Bidirectional Encoder Representations from Transformers (BERT) applies the Transformer's self-attention architecture to build a language representation model with several advantages:

  • It jointly learns representations at the token level and the sentence level.
  • It uses the same network architecture for pre-training and fine-tuning, so pretrained weights can be used directly.
  • It can learn the relationship between sentences at once, by feeding them into the model separated by a special [SEP] token.
  • It captures bidirectional, long-range interactions between tokens through its pretraining tasks.

Pre-training BERT

BERT learns deep bidirectional representations by pre-training with two self-supervised objectives:

  • Masked Language Model (Masked LM): Some input tokens are randomly replaced with a [MASK] token, and the model is trained to predict the original tokens from their bidirectional context.
  • Next Sentence Prediction (NSP): Given a pair of sentences, the model predicts whether the second sentence genuinely follows the first sentence or not (binary classification).

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.

Fine-Tuning BERT

The same pretrained architecture adapts to many downstream tasks by adding a small task-specific output layer:

  • Sentence-level Classification for One Sentence: sentiment analysis, document classification. Applying softmax on the representation of a special token [CLS].
  • Token-level Classification for One Sentence part-of-speech tagging, slot filling. Applying softmax to each token's representation.
  • Sentence-level Classification for Two Sentences: natural language inference. Applying softmax on the representation of a special token [CLS].
  • Question Answering: The task is to predict the answer text span to a given question from the given passage.
    To achieve this, the model learns a start vector and an end vector only during fine-tuning. It computes the dot product between each token's representation and these vectors, followed by a softmax, to identify the most likely start and end positions of the answer span within the passage.

Sequence-to-Sequence with Transformer

Transformers can also perform sequence-to-sequence tasks like machine translation by connecting three pieces of attention:

  • Encoder: A stack of self-attention blocks that encodes the source sequence into representations.
  • Encoder-Decoder Attention: where the decoder's representations act as the query, and the encoder's representations act as the key and value. This lets each decoding step attend back over the entire source sequence when deciding what to generate next.
  • Decoder: A stack of Encoder-Decoder Attention and masked self-attention blocks to generate outputs. The masking prevents each position from attending to future tokens it hasn't generated yet.

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.

Key Takeaways

Transformer

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

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.