Skip to content
0:00 / 0:00

#pytorch#makemore#bigram

The spelled-out intro to language modeling: building makemore

Andrej Karpathy

1.7M subscribers • 6 videos on Hallway

YouTube

Intermediate. Assumes working knowledge.

1.3M views4 years ago2 recommendations

AI and machine learningTransformers and LLMsNeural networks and deep learningPython

Andrej Karpathy builds a character-level bigram language model from scratch using PyTorch, explaining tensors, training, and loss evaluation.

We implement a bigram character-level language model, which we will further complexify in followup videos into a modern Transformer language model, like GPT. In this video, the focus is on (1) introducing torch.Tensor and its subtleties and use in efficiently evaluating neural networks and (2) the overall framework of language modeling that includes model training, sampling, and the evaluation of a loss (e.g. the negative log likelihood for classification). Links: - makemore on github: github.com/karpathy/makemore - jupyter notebook I built in this video: github.com/karpathy/nn-zero-to-hero/blob/master/lectures/mak… - my website: karpathy.ai - my twitter: twitter.com/karpathy - (new) Neural Networks: Zero to Hero series Discord channel: discord.gg/3zy8kqD9Cp , for people who'd like to chat more and go beyond youtube comments Useful links for practice: - Python + Numpy tutorial from CS231n cs231n.github.io/python-numpy-tutorial/ . We use torch.tensor instead of numpy.array in this video. Their design (e.g. broadcasting, data types, etc.) is so similar that practicing one is basically practicing the other, just be careful with some of the APIs - how various functions are named, what arguments they take, etc. - these details can vary. - PyTorch tutorial on Tensor pytorch.org/tutorials/beginner/basics/tensorqs_tutorial.html - Another PyTorch intro to Tensor pytorch.org/tutorials/beginner/nlp/pytorch_tutorial.html Exercises: E01: train a trigram language model, i.e. take two characters as an input to predict the 3rd one. Feel free to use either counting or a neural net. Evaluate the loss; Did it improve over a bigram model? E02: split up the dataset randomly into 80% train set, 10% dev set, 10% test set. Train the bigram and trigram models only on the training set. Evaluate them on dev and test splits. What can you see? E03: use the dev set to tune the strength of smoothing (or regularization) for the trigram model - i.e. try many possibilities and see which one works best based on the dev set loss. What patterns can you see in the train and dev set loss as you tune this strength? Take the best setting of the smoothing and evaluate on the test set once and at the end. How good of a loss do you achieve? E04: we saw that our 1-hot vectors merely select a row of W, so producing these vectors explicitly feels wasteful. Can you delete our use of F.one_hot in favor of simply indexing into rows of W? E05: look up and use F.cross_entropy instead. You should achieve the same result. Can you think of why we'd prefer to use F.cross_entropy instead? E06: meta-exercise! Think of a fun/interesting exercise and complete it. Chapters: intro reading and exploring the dataset exploring the bigrams in the dataset counting bigrams in a python dictionary counting bigrams in a 2D torch tensor ("training the model") visua

Why it's here

2 people recommend this

What people say. Engineers point newcomers here for an invaluable, step-by-step introduction to how language models work under the hood.

islewison Hacker News • Recommends

On “Markov Chains are the Original Language Models”

I resource I found invaluable in demystifying LLM's was Andrej Karpathy's Youtube series. In particular, his "Makemore" video built a basic LLM in the same vein as OP's. It's long (2h), but well worth the time if anyone is curious about the technical workings of LLMs. link to this video

georgehillon Hacker News • Recommends

On “Show HN: Next-token prediction in JavaScript”

You've made something interesting. Many people have already answered this here 1, so there's no point in me explaining it again. Dropping some pointers that will be helpful for you: - link - Highly recommend this one link to this video - link - link 1: ex: link

chrsigon Hacker News • Mentions

On “GPT in 500 Lines of SQL”

This is beautiful. I'd actually been going down this same rabbit hole with sqlite, I hadn't gotten far enough to bring a neural net into it. I'd been inspired by the makemore lecture series0. At the 1hr mark or so, he switches from counting to using a nn, which is about as far as I've gotten. Breaking it down into a relational model is actually a really great exercise. 0 link to this video

caprockon Hacker News • Mentions

On “The n-gram Language Model”

For reference, this project is from Eureka Labs, which is the new education startup from Andrej Karpathy. * link It appears to be a generalized version of the bi-gram project, which he links to here: * link to this video It's cool to see him putting in even more effort on the edu work. It's a real gift he has, and was visible all the way back in his grad student projects.