Stanford CS224N: NLP with Deep Learning | Winter 2019 | Lecture 14 – Transformers and Self-Attention

Stanford CS224N: NLP with Deep Learning | Winter 2019 | Lecture 14 – Transformers and Self-Attention

Introduction

The introduction of the invited speakers and an overview of what will be covered in the lecture.

Invited Speakers

  • Ashish Vaswani will talk about self-attention for generative models, specifically transformers.
  • Anna Huang will discuss some applications of this work, including music applications.

Purpose of Talk

The purpose of the talk is to discuss self-attention and its ability to model inductive biases that could be useful for problems related to variable length data.

Learning Representations

  • Deep learning is all about representation learning.
  • Building the right tools for learning representations is important in achieving empirical success.
  • Self attention has the ability to model inductive biases that potentially could be useful for problems related to variable length data.

Recurrent Neural Networks (RNNs)

  • RNNs have been the primary workhorse for deep learning up until this point.
  • They produce a continuous representation at each timestep by consuming a string or sentence sequentially.
  • Parallelization is limited with recurrence because you have to wait until a particular time point to produce a representation.

Dataset Structure and Symmetries

The importance of understanding dataset structure and symmetries when applying machine learning techniques.

Understanding Dataset Structure

  • It's important to ask what structure exists in your dataset or what symmetries exist.
  • Is there a model that exists with inductive biases that can model these properties?

Self Attention for Variable Length Data

Self attention as a tool for learning representations primarily of variable length data.

Representation Learning

  • Deep learning is all about representation learning.
  • Building the right tools for learning representations is important in achieving empirical success.

Recurrent Neural Networks (RNNs)

  • RNNs have been the primary workhorse for deep learning up until this point.
  • They produce a continuous representation at each timestep by consuming a string or sentence sequentially.
  • Parallelization is limited with recurrence because you have to wait until a particular time point to produce a representation.

Self Attention

  • Self attention has the ability to model inductive biases that potentially could be useful for problems related to variable length data.
  • It allows for parallel models, which is beneficial in the realm of large data.

Convolutional Sequence Models and Self-Attention

This section discusses the limitations of convolutional sequence models and how self-attention can be used to overcome these limitations.

Limitations of Convolutional Sequence Models

  • Convolutional sequence models have limited receptive field convolutions that consume sentences in depth, making it difficult to efficiently capture long-distance interactions.
  • These models are trivial to parallelize but still require a linear or logarithmic number of steps for long-distance interactions.
  • Attention has been a critical mechanism in neural machine translation, allowing the decoder to attend to content from the encoder.

Self-Attention Mechanism

  • Self-attention is a content-based memory retrieval mechanism that allows for efficient representation of variable length sequences.
  • The attention mechanism compares content with all words and produces a weighted combination of the entire neighborhood based on compatibility or comparison scores.
  • Attention gets multiplicative interactions by multiplying attention probabilities with activations, making it trivial to parallelize.

Advantages of Self-Attention

  • Self-attention allows for constant path length, enabling all pairs or positions to interact simultaneously.
  • Attention gets multiplicative interactions by construction, which is why it beats RNNs even without explicit gating mechanisms.

Self-Attention within Recurrent Neural Networks

In this section, the speaker discusses previous work on self-attention within recurrent neural networks and memory networks. The speaker notes that these models did not show empirical success in conditional modeling like translation.

Memory Networks

  • Memory networks by Weston and Sukhbaatar had a version of recurrent attention.
  • They used a fixed query at every step which left something to be desired.
  • They questioned whether it would work on large-scale machine translation systems or text generation systems.

Transformer Model

In this section, the speaker introduces the transformer model and explains how attention is used for computing representations of input.

Encoder

  • Attention is primarily used for computing representations of input.
  • Attention is permutation invariant, so position representations are added to maintain order.
  • Two types of position representations were tried: sinusoids with no entropy invented and learned representations.
  • The encoder has a self-attention layer that recomputes representation for every position simultaneously using attention, followed by a feed-forward layer with residual connections.

Decoder

  • The decoder side mimics a language model using self attention.
  • Causality is imposed by masking out positions that can't be looked at.
  • A causal self attention layer is followed by encoder-decoder attention where we attend to the last layer of the encoder and a feed-forward layer.
  • This tripled repeats several times before ending with standard cross entropy loss.

Computing Attention

In this section, the speaker explains how attention is computed in the transformer model.

Linear Transformation

  • To re-represent position e2, it's first linearly transformed into a query.
  • Every position in your neighborhood or input is then linearly transformed into a key.
  • These linear transformations can be thought of as features.
  • A bilinear form is used to project these vectors into a space where dot product is a good proxy for similarity.

Convex Combination

  • A softmax computer convex combination is used based on the logit.
  • Based on this convex combination, e2 is re-expressed in terms of all the vectors of all these positions.
  • Before doing the convex combination, another linear transformation is done to produce values.

Introduction to Attention Mechanism

In this section, the speaker introduces the attention mechanism and explains why it is attractive.

Why is Attention Mechanism Attractive?

  • The attention mechanism is fast and can be done simultaneously for all positions with just two matmuls and a softmax.
  • It has a favorable computation profile compared to RNNs or convolutions because it's quadratic in the length of your sequence rather than quadratic in the dimension.

Imposing Causality on Decoder Side

In this section, the speaker explains how causality is imposed on the decoder side.

Imposing Causality

  • To impose causality, 10 e- minus 10 e9 is added to the logits which results in zero probabilities on those positions.

Computation Profile of RNNs or Convolutions

In this section, the speaker compares the computational profile of RNNs or convolutions with that of attention mechanism.

Computation Profile Comparison

  • RNNs or convolutions are quadratic in dimension while attention mechanism is quadratic in length of sequence.
  • Attention mechanism becomes very attractive when your dimension is much larger than your length as it happens in machine translation.

Applying Convolutional Filter on Language Sequences

In this section, the speaker discusses applying convolutional filter on language sequences.

Applying Convolutional Filter

  • A convolutional filter can pick out different information from different places based on relative distances between words.
  • With a single attention layer, you have only mixing proportions available to you, so you can't pick out different pieces of information from different places.

Multiple Attention Layers

In this section, the speaker explains how multiple attention layers can be used to pick out different pieces of information from different places.

Multiple Attention Layers

  • Multiple attention layers can be used for who, what and whom in parallel.
  • Each head carries with it a softmax which increases the number of softmaxes but doesn't change the amount of FLOPS.
  • With more heads or heads as a function of positions, a convolution can be simulated with more parameters.

Introduction

The speaker introduces the setup of their experiment and discusses some of the results they achieved.

Experiment Setup

  • Used a 32,000-word vocabulary and WordPiece encodings.
  • Tested on WMT14 dataset with 2013 dev set.
  • Achieved state-of-the-art results in English-German and English-French translation.

Architecture Design

The speaker discusses the architecture design of their model and its advantages.

Model Architecture

  • Transformer architecture was not designed to have better expressivity than an LSTM.
  • Gradient dynamics and attention are simple, making it easier for stochastic gradient descent to train the model well.
  • Explicitly models all pairwise connections between words, allowing for clear relationships between any two words.

Frameworks

  • Work was initially pushed out in tensor2tensor but may change with arrival of JAX.
  • Other frameworks include Sockeye from Amazon and Fairseq from Facebook.

Residual Connections

The speaker explains how residual connections work in their model and how they affect attention distribution.

Position Information

  • Position information is added at input to the model but not at every layer.
  • Residual connections carry position information to every layer, allowing subsequent layers to attend properly.

Diagonal Focus

  • Without residuals, attention distribution cannot pick up diagonal focus.
  • Severing residuals while injecting position information at every layer does not recover accuracy but does restore diagonal focus.

Repeating Structure in Images and Music

The speaker discusses repeating structure in images and music as a form of self-similarity that can be modeled using attention mechanisms.

Self-Similarity

  • Repeating structure is common in images and music, where motifs repeat in different scales.
  • Self-similarity can be modeled using attention mechanisms to capture long- and short-term relationships.

Introduction to Self-Attention Mechanism

In this section, the speaker introduces the concept of self-attention mechanism and how it can be applied to images.

Self-Attention for Images

  • Self-attention is a computational mechanism that allows you to measure how well you're doing on your held-out set.
  • Recurrent models like PixelRNN and PixelCNN are used for good compression rates in images.
  • Self-attention is beneficial for images because it naturally models self-similarity, which is exploited in image generation and denoising.
  • The encoder self-attention mechanism replaces words with pixels, computing content-based similarity between elements and constructing a convex combination that brings them together.

Transformer Architecture for Images

In this section, the speaker explains how they adapted the transformer architecture for use with images.

Position Representations

  • Position representations are two-dimensional instead of one-dimensional since we are not dealing with sequences.

Rasterization

  • A one-dimensional rasterization was used where a single query block attends into a larger memory block in a rasterized fashion along the rows.

Pixel Recursive Super Resolution

In this section, the speaker discusses the Pixel Recursive Super Resolution model and its performance in comparison to other models.

Model Architecture

  • The model uses a similar attention mechanism as another model called PixelSNAIL.
  • The team tried both super-resolution and unconditional and conditional image generation.
  • Achieved better perplexity than existing models on ImageNet.

Performance Comparison

  • PixelSNAIL outperformed them on bits per dimension.
  • Structured objects were easy to capture, but camouflaged objects turned into mush.
  • Super-resolution results were much better due to a lot of conditioning information available.
  • Better facial orientation and structure than previous work in super resolution.

Image Completion

  • Image completion was used to test if the model captures interesting structures in the world and gets diversity.
  • Horse images showed hallucination of humans by the model, indicating that it captures some structure about the world.
  • The chair object was completely refused by the model.

Introduction to Music Generation

In this section, the speaker introduces the concept of music generation and explains how it is similar to language. They also discuss the raw representation used in music generation.

Raw Representation in Music Generation

  • The raw representation used in music generation is analogous to language.
  • It involves a symbolic representation where MIDI pianos are used and professional amateur musicians perform on them.
  • At each time step, modeling music as a sequential process involves turning notes on and off and adding dynamics information.

Recurrent Neural Networks for Modeling Music

In this section, the speaker discusses how recurrent neural networks (RNNs) have been traditionally used for modeling music due to their ability to handle repetition.

Traditional Use of RNNs for Modeling Music

  • RNNs have been traditionally used for modeling music due to their ability to handle repetition.
  • However, they struggle with longer contexts and can only look at a blurry version of past events.

Transformer Model for Music Generation

In this section, the speaker discusses how transformer models have been used for music generation and compares their performance with that of RNNs.

Comparison between Transformer Models and RNNs

  • Transformer models have been used for music generation due to their ability to compress long sequences into fixed-length vectors.
  • However, they struggle with repetition coming at a distance.
  • The speaker demonstrates samples from an RNN, a transformer model, and a music transformer model with relative attention.
  • The samples show that while the transformer model deteriorates beyond its trained length, it can hold motifs consistently.
  • The self-similarity in music makes it challenging for traditional transformer models but not so much for those with relative attention.

Unconditioned Music Generation

In this section, the speaker discusses unconditioned music generation and how it can be used to create motifs.

Unconditioned Music Generation

  • Unconditioned music generation involves creating motifs without any priming.
  • The speaker demonstrates a sample of unconditioned music generated by a model.
  • The sample shows repetition with gaps in between, and the model looks at relevant parts even if they are not immediately preceding.

Introduction

In this section, the speaker introduces the topic of attention mechanisms and how they are used in music generation.

Attention Mechanisms

  • The regular attention mechanism is a weighted average of past history.
  • There are structural moments where you can see more clean sections that it's attending to.
  • The positional sinusoids allow for close-by positions to have a very similar cross-section into multiple sinusoids.

Regular Attention Mechanism

In this section, the speaker explains how the regular attention mechanism works and its limitations.

Weighted Average of Past History

  • The regular attention mechanism is a weighted average of past history.
  • However far it is, we have direct access to it.
  • If there are motifs that occurred early on in the piece, we're still able to retrieve those.

Limitations

  • All the past becomes kind of a bag of words with no structure of which came before or after.
  • Positional sinusoids help capture structure but have limitations.

Relative Attention Mechanism

In this section, the speaker introduces relative attention mechanisms and their advantages over regular attention mechanisms.

Advantages Over Regular Attention Mechanisms

  • Captures translational invariance and relational information forward.
  • Allows for capturing distance information explicitly.
  • Can imagine relative attention with multiple heads at play as some combination of these advantages.

Memory Requirements for Music Generation

In this section, the speaker discusses memory requirements for music generation using relative attention mechanisms.

Memory Requirements

  • Music samples are in the range of 2,000 time-steps which means 2,000 tokens need to fit in memory.
  • Original formulation relied on building a 3D tensor that's very large in memory.
  • Directly multiplying queries and embedding distances reduces memory requirements.

Introduction

In this section, the speaker introduces himself and presents a one-minute music sample. He then proceeds to discuss the use of relative attention in music and machine translation.

Relative Attention in Music and Machine Translation

  • Relative attention is a powerful mechanism for music and has also been helpful in machine translation.
  • Convolutional neural networks achieve translational equivariance, which means that they produce the same activation regardless of the image's absolute location. Similarly, relative attention achieves this effect by removing the notion of absolute position from the model.
  • Relative self-attention might be useful for graph problems since it allows you to impose different notions of similarity between different elements.

Self-Supervised Learning

In this section, the speaker discusses generative models of images and their usefulness in self-supervised learning.

Generative Models of Images

  • Generative models of images are useful for self-supervised learning where you pre-train a model on a lot of unlabeled data before transferring it.

Graph Neural Networks

In this section, the speaker talks about how relative attention can be used within graphs and its similarities with message passing neural networks.

Message Passing Neural Networks

  • Message passing neural networks pass messages between pairs of nodes in a graph. They are similar to multi-head attention.
  • Self-attention is like a message passing mechanism where interactions occur between all nodes instead of just pairs.

Introduction to Self-Attention

In this section, the speaker introduces Self-Attention and its advantages in sequence modeling.

Properties of Self-Attention

  • Self-Attention has a constant path length between any two positions, making it useful in sequence modeling.
  • It allows for unbounded memory and is computationally trivial to parallelize.
  • Self-Attention can model self-similarity and expressive timing in music. It also extends naturally to graphs.

Auto-regressive Generation with Self-Attention

In this section, the speaker discusses the challenges of auto-regressive generation and how Self-Attention models are being used to address them.

Challenges of Auto-regressive Generation

  • Outputs are multi-modal, meaning there are multiple correct translations for a given input.
  • Generation is sequential, which makes it computationally expensive.

Approaches to Addressing Challenges

Non-Auto-regressive Models

  • Non-autoregressive transformers and fast decoding have been developed to speed up generation time.

Latent Space Decision Making

  • Some papers use latent space decision making learned through word alignments or autoencoders to speed up generation time.

Blockwise Parallel Decoding

  • Blockwise parallel decoding involves running multiple models simultaneously and rescore using a more efficient model.

Scaling Up with Self Attention

In this section, the speaker discusses how Self Attention has been beneficial in transfer learning and scaling up models.

Transfer Learning

  • Self-Attention has been beneficial in transfer learning, with GPT from OpenAI and BERT being classic examples.

Scaling Up

  • Efficient optimizers like Mesh-Tensorflow have allowed for training models several orders of magnitude larger than the original models.
  • Universal transformers bring back recurrence in depth inside the transformer, while also using local attention.

Self-Supervision and Multitask Learning

In this section, the speaker discusses the benefits of self-supervision and how it can reduce the need for large labeled datasets. The speaker also talks about multitask learning and how it can help overcome the quadratic problem with Self-Attention.

Self-Supervision

  • Self-supervision can be very beneficial as it reduces the need for large labeled datasets.
  • Understanding transfer learning is becoming a reality in NLP with BERT and other models.
  • Ongoing research is being conducted to understand what's happening in self-supervised models.

Multitask Learning

  • Multitask learning can help overcome the quadratic problem with Self-Attention.
  • The speaker expresses interest in pursuing research on multitask learning.

Turn any video into a summary like this

YouTube links, meetings, lectures. With transcripts, search, and chat.

Video description

For more information about Stanford’s Artificial Intelligence professional and graduate programs, visit: https://stanford.io/3niIw41 Professor Christopher Manning, Stanford University, Ashish Vaswani & Anna Huang, Google http://onlinehub.stanford.edu/ Professor Christopher Manning Thomas M. Siebel Professor in Machine Learning, Professor of Linguistics and of Computer Science Director, Stanford Artificial Intelligence Laboratory (SAIL) To follow along with the course schedule and syllabus, visit: http://web.stanford.edu/class/cs224n/index.html#schedule 0:00 Introduction 2:07 Learning Representations of Variable Length Data 2:28 Recurrent Neural Networks 4:51 Convolutional Neural Networks? 14:06 Attention is Cheap! 16:05 Attention head: Who 16:26 Attention head: Did What? 16:35 Multihead Attention 17:34 Machine Translation: WMT-2014 BLEU 19:07 Frameworks 19:31 Importance of Residuals 23:26 Non-local Means 26:18 Image Transformer Layer 30:56 Raw representations in music and language 37:52 Attention: a weighted average 40:08 Closer look at relative attention 42:41 A Jazz sample from Music Transformer 44:42 Convolutions and Translational Equivariance 45:12 Relative positions Translational Equivariance 50:21 Sequential generation breaks modes. 50:32 Active Research Area #naturallanguageprocessing #deeplearning