Chammarychammary

Building makemore Part 2: MLP

Andrej Karpathy · 1:15:39 · 3 years ago

This lecture builds a multilayer perceptron (MLP) language model to overcome the limitations of simple bigram counting by using learned character embeddings and context windows to predict the next character in a sequence.

  • Context limitation — simple models struggle because the number of required character combinations grows exponentially with the sequence length .

  • Bengio paper approach — words are assigned vector embeddings in a shared space, allowing the model to learn relationships where similar items cluster together .

  • Input structure — the code converts a chosen "block size" of previous characters into a set of numeric inputs for the neural network .

  • Embeddings — the model uses a lookup table (matrix C) where each character gets a small, dense numeric vector; PyTorch indexing makes retrieving these vectors for entire batches very efficient .

  • Network architecture — the neural network relies on:

    • Input layer (embedded characters)
    • Hidden layer (fully connected with tanh non-linearity)
    • Output layer (logits for the next character)
  • Loss function — standardizing on F.cross_entropy is preferred over manual calculations because it is mathematically stable, robust to extreme values, and optimized to run faster .

  • Optimization — instead of calculating the gradient on the entire dataset, processing small, random subsets (mini-batches) allows for faster updates even if the gradient estimate is noisy .

  • Learning rate tuning — finding the ideal speed for updates involves testing a range of values exponentially and plotting the loss to find the "valley" where the model converges reliably .

  • Data splitting — to prevent the model from simply memorizing the input, the data is divided into three separate sets:

    • Training (for gradient descent)
    • Development/Validation (for tuning hyperparameters)
    • Test (for final performance evaluation)
  • How does F.cross_entropy handle large logit values compared to manual calculation?

  • Why is it necessary to use separate datasets for training, development, and testing?