Implementing an MLP Character-Level Language Model with Core Machine Learning Concepts (Makemore Part 2)

Andrej Karpathy

Summary:

This video demonstrates building a Multi-Layer Perceptron (MLP) character-level language model, expanding on the previous bigram model. It addresses the limitation of exponential growth in context by introducing word embeddings, where characters are mapped to lower-dimensional feature vectors. The process involves:

  • Setting up the training dataset with a block_size to define context length.
  • Implementing an embedding lookup table and understanding PyTorch tensor view operations for efficient data handling.
  • Constructing the hidden layer with tanh non-linearity and an output layer to predict the next character.
  • Calculating the negative log likelihood loss, transitioning to PyTorch's efficient and numerically stable F.cross_entropy function.
  • Developing a training loop using mini-batches for faster, approximate gradient updates.
  • Strategies for finding an optimal learning rate by plotting loss against exponentially spaced learning rate exponents.
  • Splitting the dataset into train, validation, and test sets to prevent overfitting and evaluate model generalization.
  • Experiments with increasing hidden layer size and embedding dimensionality, showing how these hyperparameters affect performance.
  • Visualizing 2D character embeddings, revealing interesting clusters (e.g., vowels together).
  • Finally, the model is used to sample new, more "name-like" words, showcasing improved language generation compared to the bigram model.
    MLP Language Model Architecture
    MLP Language Model Architecture [ 00:05:50 ]

Introduction and Motivation [00:00:00]

The video continues the "makemore" series, moving from simple bigram models to a more advanced neural network. The previous bigram model used only a single preceding character, leading to poor name generation and an exponential increase in table size when considering longer contexts.

Image showing the limitation of previous character-level models where increasing context length leads to exponential table growth.
Image showing the limitation of previous character-level models where increasing context length leads to exponential table growth. [ 00:01:00 ]

Bengio et al. 2003 (MLP Language Model) Paper Walkthrough [00:01:48]

The new approach is based on the influential Bengio et al. [2003] paper, "A Neural Probabilistic Language Model," which introduced using neural networks for language modeling.

(Re-)building Our Training Dataset [00:09:03]

The dataset consists of a list of names.

Implementing the Embedding Lookup Table [00:12:19]

Implementing the Hidden Layer + Internals of torch.Tensor: Storage, Views [00:18:35]

Implementing the Output Layer [00:29:15]

Implementing the Negative Log Likelihood Loss [00:29:53]

Summary of the Full Network [00:32:17]

All parameters (embedding table C, W1, B1, W2, B2) are defined. The initial loss (before training) is around 17, indicating random predictions.

Summary of the full network's parameters and the initial forward pass calculation of the loss.
Summary of the full network's parameters and the initial forward pass calculation of the loss. [ 00:32:40 ]

Introducing F.cross_entropy and Why [00:32:49]

PyTorch's torch.nn.functional.cross_entropy function should be used in practice for classification loss.

Implementing the Training Loop, Overfitting One Batch [00:37:56]

Training on the Full Dataset, Minibatches [00:41:25]

Finding a Good Initial Learning Rate [00:45:40]

Splitting Up the Dataset into Train/Val/Test Splits and Why [00:53:20]

Experiment: Larger Hidden Layer [01:00:49]

Visualizing the Character Embeddings [01:05:27]

Experiment: Larger Embedding Size [01:07:16]

Summary of Our Final Code, Conclusion [01:11:46]

The final training loop involves 200,000 steps, with an initial learning rate of 0.1 for the first 100,000 steps, then decaying to 0.01 for the remaining 100,000 steps. The best validation loss achieved is 2.17.

Final training loss plot and the corresponding training and validation loss values.
Final training loss plot and the corresponding training and validation loss values. [ 01:12:00 ]

Sampling from the Model [01:13:24]