back to index
Andrey Karpathy. Neural Networks: Zero to Hero
Episode 3. Building makemore Part 2: MLP
link |
Hi, everyone. Today we are continuing our implementation of MakeMore.
link |
Now, in the last lecture, we implemented the bigram language model,
link |
and we implemented it both using counts and also using a super simple neural network that has a single linear layer.
link |
Now, this is the Jupyter Notebook that we built out last lecture.
link |
And we saw that the way we approached this is that we looked at only the single previous character,
link |
and we predicted the distribution for the character that would go next in the sequence.
link |
And we did that by taking counts and normalizing them into probabilities so that each row here sums to one.
link |
Now, this is all well and good if you only have one character of previous context.
link |
And this works, and it's approachable.
link |
The problem with this model, of course, is that the predictions from this model are not very good
link |
because you only take one character of context.
link |
So the model didn't produce very name-like sounding things.
link |
Now, the problem with this approach, though, is that if we are to take more context into account
link |
when predicting the next character in a sequence, things quickly blow up.
link |
And this table, the size of this table, grows, and in fact it grows exponentially with the length of the context.
link |
Because if we only take a single character at a time, that's 27 possibilities of context.
link |
But if we take two characters in the past and try to predict a third one,
link |
suddenly the number of rows in this matrix, you can look at it that way, is 27 times 27.
link |
So there's 729 possibilities for what could have come in the context.
link |
If we take three characters as the context, suddenly we have 20,000 possibilities of context.
link |
And so that's just way too many rows of this matrix.
link |
It's way too few counts for each possibility.
link |
And the whole thing just kind of explodes and doesn't work very well.
link |
So that's why today we're going to move on to this bullet point here.
link |
And we're going to implement a multilayer perceptron model to predict the next character in a sequence.
link |
And this modeling approach that we're going to adopt follows this paper, Benjou et al. 2003.
link |
So I have the paper pulled up here.
link |
Now this isn't the very first paper that proposed the use of multilayer perceptrons or neural networks
link |
to predict the next character or token in a sequence.
link |
But it's definitely one that was very influential around that time.
link |
It is very often cited to stand in for this idea.
link |
And I think it's a very nice write-up.
link |
And so this is the paper that we're going to first look at and then implement.
link |
Now this paper has 19 pages.
link |
So we don't have time to go into the full detail of this paper, but I invite you to read it.
link |
It's very readable, interesting, and has a lot of interesting ideas in it as well.
link |
In the introduction, they describe the exact same problem I just described.
link |
And then to address it, they propose the following model.
link |
Now keep in mind that we are building a character-level language model.
link |
So we're working on the level of characters.
link |
In this paper, they have a vocabulary of 17,000 possible words,
link |
and they instead build a word-level language model.
link |
But we're going to still stick with the characters, but we'll take the same modeling approach.
link |
Now what they do is basically they propose to take every one of these words, 17,000 words,
link |
and they're going to associate to each word a, say, 30-dimensional feature vector.
link |
So every word is now embedded into a 30-dimensional space.
link |
You can think of it that way.
link |
So we have 17,000 points or vectors in a 30-dimensional space,
link |
and you might imagine that's very crowded. That's a lot of points for a very small space.
link |
Now in the beginning, these words are initialized completely randomly, so they're spread out at random.
link |
But then we're going to tune these embeddings of these words using backpropagation.
link |
So during the course of training of this neural network,
link |
these points or vectors are going to basically move around in this space.
link |
And you might imagine that, for example, words that have very similar meanings
link |
or that are indeed synonyms of each other might end up in a very similar part of the space.
link |
And conversely, words that mean very different things would go somewhere else in the space.
link |
Now their modeling approach otherwise is identical to ours.
link |
They are using a multilayer neural network to predict the next word given the previous words.
link |
And to train the neural network, they are maximizing the log likelihood of the training data just like we did.
link |
So the modeling approach itself is identical.
link |
Now here they have a concrete example of this intuition. Why does it work?
link |
Basically, suppose that, for example, you are trying to predict a dog was running in a blank.
link |
Now suppose that the exact phrase, a dog was running in a, has never occurred in the training data.
link |
And here you are at test time later, when the model is deployed somewhere,
link |
and it's trying to make a sentence, and it's saying a dog was running in a blank.
link |
And because it's never encountered this exact phrase in the training set, you're out of distribution, as we say.
link |
You don't have fundamentally any reason to suspect what might come next.
link |
But this approach actually allows you to get around that.
link |
Because maybe you didn't see the exact phrase, a dog was running in a something, but maybe you've seen similar phrases.
link |
Maybe you've seen the phrase, the dog was running in a blank.
link |
And maybe your network has learned that a and the frequently are interchangeable with each other.
link |
And so maybe it took the embedding for a and the embedding for the, and it actually put them nearby each other in the space.
link |
And so you can transfer knowledge through that embedding, and you can generalize in that way.
link |
Similarly, the network could know that cats and dogs are animals, and they co-occur in lots of very similar contexts.
link |
And so even though you haven't seen this exact phrase, or if you haven't seen exactly walking or running,
link |
you can, through the embedding space, transfer knowledge, and you can generalize to novel scenarios.
link |
So let's now scroll down to the diagram of the neural network.
link |
They have a nice diagram here.
link |
And in this example, we are taking three previous words, and we are trying to predict the fourth word in a sequence.
link |
Now these three previous words, as I mentioned, we have a vocabulary of 17,000 possible words.
link |
So every one of these basically are the index of the incoming word.
link |
And because there are 17,000 words, this is an integer between 0 and 16,999.
link |
Now there's also a lookup table that they call C.
link |
This lookup table is a matrix that is 17,000 by, say, 30.
link |
And basically what we're doing here is we're treating this as a lookup table.
link |
So every index is plucking out a row of this embedding matrix,
link |
so that each index is converted to the 30-dimensional vector that corresponds to the embedding vector for that word.
link |
So here we have the input layer of 30 neurons for three words, making up 90 neurons in total.
link |
And here they're saying that this matrix C is shared across all the words.
link |
So we're always indexing into the same matrix C over and over for each one of these words.
link |
Next up is the hidden layer of this neural network.
link |
The size of this hidden neural layer of this neural net is a hyperparameter.
link |
So we use the word hyperparameter when it's kind of like a design choice up to the designer of the neural net.
link |
And this can be as large as you'd like or as small as you'd like.
link |
So for example, the size could be 100.
link |
And we are going to go over multiple choices of the size of this hidden layer, and we're going to evaluate how well they work.
link |
So say there were 100 neurons here.
link |
All of them would be fully connected to the 90 words or 90 numbers that make up these three words.
link |
So this is a fully connected layer.
link |
Then there's a 10-inch linearity.
link |
And then there's this output layer.
link |
And because there are 17,000 possible words that could come next,
link |
this layer has 17,000 neurons, and all of them are fully connected to all of these neurons in the hidden layer.
link |
So there's a lot of parameters here because there's a lot of words.
link |
So most computation is here.
link |
This is the expensive layer.
link |
Now, there are 17,000 logits here.
link |
So on top of there, we have the softmax layer, which we've seen in our previous video as well.
link |
So every one of these logits is exponentiated, and then everything is normalized to sum to one.
link |
So then we have a nice probability distribution for the next word in the sequence.
link |
Now, of course, during training, we actually have the label.
link |
We have the identity of the next word in the sequence.
link |
That word or its index is used to pluck out the probability of that word.
link |
And then we are maximizing the probability of that word with respect to the parameters of this neural net.
link |
So the parameters are the weights and biases of this output layer, the weights and biases of the hidden layer,
link |
and the embedding lookup table C.
link |
And all of that is optimized using backpropagation.
link |
And these dashed arrows, ignore those.
link |
That represents a variation of a neural net that we are not going to explore in this video.
link |
So that's the setup, and now let's implement it.
link |
Okay, so I started a brand new notebook for this lecture.
link |
We are importing PyTorch, and we are importing matplotlib so we can create figures.
link |
Then I am reading all the names into a list of words like I did before, and I'm showing the first eight right here.
link |
Keep in mind that we have 32,000 in total.
link |
These are just the first eight.
link |
And then here I'm building out the vocabulary of characters and all the mappings from the characters as strings to integers and vice versa.
link |
Now the first thing we want to do is we want to compile the dataset for the neural network.
link |
And I had to rewrite this code.
link |
I'll show you in a second what it looks like.
link |
So this is the code that I created for the dataset creation.
link |
So let me first run it, and then I'll briefly explain how this works.
link |
So first we're going to define something called block size.
link |
And this is basically the context length of how many characters do we take to predict the next one.
link |
So here in this example we're taking three characters to predict the fourth one, so we have a block size of three.
link |
That's the size of the block that supports the prediction.
link |
Then here I'm building out the X and Y.
link |
The X are the input to the neural net, and the Y are the labels for each example inside X.
link |
Then I'm erasing over the first five words.
link |
I'm doing the first five just for efficiency while we are developing all the code.
link |
But then later we're going to come here and erase this so that we use the entire training set.
link |
So here I'm printing the word Emma.
link |
And here I'm basically showing the five examples that we can generate out of the single word Emma.
link |
So when we are given the context of just dot dot dot, the first character in the sequence is E.
link |
In this context, the label is M. When the context is this, the label is M, and so forth.
link |
And so the way I build this out is first I start with a padded context of just zero tokens.
link |
Then I iterate over all the characters.
link |
I get the character in the sequence, and I basically build out the array Y of this current character,
link |
and the array X which stores the current running context.
link |
And then here I print everything, and here I crop the context and enter the new character in the sequence.
link |
So this is kind of like a rolling window of context.
link |
Now we can change the block size here to, for example, four.
link |
And in that case we would be predicting the fifth character given the previous four.
link |
Or it can be five, and then it would look like this.
link |
Or it can be, say, ten, and then it would look something like this.
link |
We're taking ten characters to predict the eleventh one.
link |
And we're always padding with dots.
link |
So let me bring this back to three, just so that we have what we have here in the paper.
link |
And finally, the dataset right now looks as follows.
link |
From these five words, we have created a dataset of 32 examples.
link |
And each input to the neural net is three integers.
link |
And we have a label that is also an integer, Y.
link |
So X looks like this.
link |
These are the individual examples.
link |
And then Y are the labels.
link |
So given this, let's now write a neural network that takes these Xs and predicts the Ys.
link |
First, let's build the embedding lookup table C.
link |
So we have 27 possible characters, and we're going to embed them in a lower-dimensional space.
link |
In the paper, they have 17,000 words, and they embed them in spaces as small-dimensional as 30.
link |
So they cram 17,000 words into 30-dimensional space.
link |
In our case, we have only 27 possible characters.
link |
So let's cram them in something as small as, to start with, for example, a two-dimensional space.
link |
So this lookup table will be random numbers, and we'll have 27 rows, and we'll have two columns.
link |
So each one of 27 characters will have a two-dimensional embedding.
link |
So that's our matrix C of embeddings.
link |
In the beginning, initialized randomly.
link |
Now, before we embed all of the integers inside the input X using this lookup table C,
link |
let me actually just try to embed a single individual integer, like, say, 5.
link |
So we get a sense of how this works.
link |
Now, one way this works, of course, is we can just take the C and we can index into row 5.
link |
And that gives us a vector, the fifth row of C.
link |
And this is one way to do it.
link |
The other way that I presented in the previous lecture is actually seemingly different, but actually identical.
link |
So in the previous lecture, what we did is we took these integers and we used the one-hot encoding to first encode them.
link |
So f.onehot, we want to encode integer 5, and we want to tell it that the number of classes is 27.
link |
So that's the 26-dimensional vector of all zeros, except the fifth bit is turned on.
link |
Now, this actually doesn't work.
link |
The reason is that this input actually must be a Georgetown tensor.
link |
And I'm making some of these errors intentionally, just so you get to see some errors and how to fix them.
link |
So this must be a tensor, not an int. Fairly straightforward to fix.
link |
We get a one-hot vector, the fifth dimension is 1, and the shape of this is 27.
link |
And now notice that, just as I briefly alluded to in a previous video,
link |
if we take this one-hot vector and we multiply it by c, then what would you expect?
link |
Well, number one, first you'd expect an error, because expected scalar type long, but found float.
link |
So a little bit confusing, but the problem here is that one-hot, the data type of it, is long.
link |
It's a 64-bit integer, but this is a float tensor.
link |
And so PyTorch doesn't know how to multiply an int with a float,
link |
and that's why we had to explicitly cast this to a float so that we can multiply.
link |
Now, the output actually here is identical.
link |
And it's identical because of the way the matrix multiplication here works.
link |
We have the one-hot vector multiplying columns of c, and because of all the zeros,
link |
they actually end up masking out everything in c except for the fifth row, which is plucked out.
link |
And so we actually arrive at the same result.
link |
And that tells you that here we can interpret this first piece here, this embedding of the integer.
link |
We can either think of it as the integer indexing into a lookup table c,
link |
but equivalently we can also think of this little piece here as a first layer of this bigger neural net.
link |
This layer here has neurons that have no nonlinearity, there's no tanh, they're just linear neurons,
link |
and their weight matrix is c.
link |
And then we are encoding integers into one-hot and feeding those into a neural net,
link |
and this first layer basically embeds them.
link |
So those are two equivalent ways of doing the same thing.
link |
We're just going to index because it's much, much faster,
link |
and we're going to discard this interpretation of one-hot inputs into neural nets.
link |
And we're just going to index integers and use embedding tables.
link |
Now embedding a single integer like 5 is easy enough.
link |
We can simply ask PyTorch to retrieve the fifth row of c, or the row index 5 of c.
link |
But how do we simultaneously embed all of these 32 by 3 integers stored in array x?
link |
Luckily PyTorch indexing is fairly flexible and quite powerful.
link |
So it doesn't just work to ask for a single element 5 like this.
link |
You can actually index using lists.
link |
So for example, we can get the rows 5, 6, and 7, and this will just work like this.
link |
We can index with a list.
link |
It doesn't just have to be a list, it can also be actually a tensor of integers,
link |
and we can index with that.
link |
So this is an integer tensor 5, 6, 7, and this will just work as well.
link |
In fact, we can also, for example, repeat row 7 and retrieve it multiple times,
link |
and that same index will just get embedded multiple times here.
link |
So here we are indexing with a one-dimensional tensor of integers,
link |
but it turns out that you can also index with multi-dimensional tensors of integers.
link |
Here we have a two-dimensional tensor of integers.
link |
So we can simply just do c at x, and this just works.
link |
The shape of this is 32 by 3, which is the original shape,
link |
and now for every one of those 32 by 3 integers, we've retrieved the embedding vector here.
link |
So basically, we have that as an example.
link |
The example index 13, the second dimension, is the integer 1 as an example.
link |
And so here, if we do c of x, which gives us that array,
link |
and then we index into 13 by 2 of that array, then we get the embedding here.
link |
And you can verify that c at 1, which is the integer at that location, is indeed equal to this.
link |
You see they're equal.
link |
So basically, long story short, PyTorch indexing is awesome,
link |
and to embed simultaneously all of the integers in x, we can simply do c of x,
link |
and that is our embedding, and that just works.
link |
Now let's construct this layer here, the hidden layer.
link |
So we have that w1, as I'll call it, are these weights, which we will initialize randomly.
link |
Now the number of inputs to this layer is going to be 3 times 2,
link |
because we have two-dimensional embeddings and we have three of them,
link |
so the number of inputs is 6.
link |
And the number of neurons in this layer is a variable up to us.
link |
Let's use 100 neurons as an example.
link |
And then biases will be also initialized randomly as an example, and we just need 100 of them.
link |
Now the problem with this is we can't simply, normally we would take the input,
link |
in this case that's embedding, and we'd like to multiply it with these weights,
link |
and we'd like to add the bias. This is roughly what we want to do.
link |
But the problem here is that these embeddings are stacked up in the dimensions of this input tensor.
link |
So this will not work, this matrix multiplication, because this is a shape 32 by 3 by 2,
link |
and I can't multiply that by 6 by 100.
link |
So somehow we need to concatenate these inputs here together
link |
so that we can do something along these lines, which currently does not work.
link |
So how do we transform this 32 by 3 by 2 into a 32 by 6 so that we can actually perform this multiplication over here?
link |
I'd like to show you that there are usually many ways of implementing what you'd like to do in Torch.
link |
And some of them will be faster, better, shorter, etc.
link |
And that's because Torch is a very large library, and it's got lots and lots of functions.
link |
So if we just go to the documentation and click on Torch, you'll see that my slider here is very tiny,
link |
and that's because there are so many functions that you can call on these tensors
link |
to transform them, create them, multiply them, add them, perform all kinds of different operations on them.
link |
And so this is kind of like the space of possibility, if you will.
link |
Now one of the things that you can do is we can control F for concatenate.
link |
And we see that there's a function Torch.cat, short for concatenate.
link |
This concatenates a given sequence of tensors in a given dimension.
link |
And these tensors must have the same shape, etc.
link |
So we can use the concatenate operation to, in a naive way, concatenate these three embeddings for each input.
link |
So in this case we have m of the shape.
link |
And really what we want to do is we want to retrieve these three parts and concatenate them.
link |
So we want to grab all the examples.
link |
We want to grab first the zeroth index, and then all of this.
link |
So this plucks out the 32x2 embeddings of just the first word here.
link |
And so basically we want this guy, we want the first dimension, and we want the second dimension.
link |
And these are the three pieces individually.
link |
And then we want to treat this as a sequence, and we want to Torch.cat on that sequence.
link |
So this is the list.
link |
Torch.cat takes a sequence of tensors, and then we have to tell it along which dimension to concatenate.
link |
So in this case all these are 32x2, and we want to concatenate not across dimension 0, but across dimension 1.
link |
So passing in 1 gives us the result that the shape of this is 32x6, exactly as we'd like.
link |
So that basically took 32 and squashed these by concatenating them into 32x6.
link |
Now this is kind of ugly because this code would not generalize if we want to later change the block size.
link |
Right now we have three inputs, three words, but what if we had five?
link |
Then here we would have to change the code because I'm indexing directly.
link |
Well Torch comes to rescue again because there turns out to be a function called unbind.
link |
And it removes a tensor dimension.
link |
So it removes a tensor dimension, returns a tuple of all slices along a given dimension without it.
link |
So this is exactly what we need.
link |
And basically when we call torch.unbind of m and pass in dimension 1, index 1, this gives us a list of tensors exactly equivalent to this.
link |
So running this gives us a line 3, and it's exactly this list.
link |
So we can call torch.cat on it and along the first dimension.
link |
And this works, and the shape is the same.
link |
But now it doesn't matter if we have block size 3 or 5 or 10, this will just work.
link |
So this is one way to do it.
link |
But it turns out that in this case there's actually a significantly better and more efficient way.
link |
And this gives me an opportunity to hint at some of the internals of torch.tensor.
link |
So let's create an array here of elements from 0 to 17, and the shape of this is just 18.
link |
It's a single vector of 18 numbers.
link |
It turns out that we can very quickly re-represent this as different sized and dimensional tensors.
link |
We do this by calling a view, and we can say that actually this is not a single vector of 18.
link |
This is a 2 by 9 tensor, or alternatively this is a 9 by 2 tensor, or this is actually a 3 by 3 by 2 tensor.
link |
As long as the total number of elements here multiply to be the same, this will just work.
link |
And in PyTorch, this operation, calling.view, is extremely efficient.
link |
And the reason for that is that in each tensor there's something called the underlying storage.
link |
And the storage is just the numbers always as a one dimensional vector.
link |
And this is how this tensor is represented in the computer memory.
link |
It's always a one dimensional vector.
link |
But when we call.view, we are manipulating some of the attributes of that tensor that dictate how this one dimensional sequence is interpreted to be an n dimensional tensor.
link |
And so what's happening here is that no memory is being changed, copied, moved, or created when we call.view.
link |
The storage is identical, but when you call.view, some of the internal attributes of the view of this tensor are being manipulated and changed.
link |
In particular, there's something called storage offset, strides, and shapes, and those are manipulated so that this one dimensional sequence of bytes is seen as different n dimensional arrays.
link |
There's a blog post here from Eric called PyTorch internals where he goes into some of this with respect to tensor and how the view of a tensor is represented.
link |
And this is really just like a logical construct of representing the physical memory.
link |
And so this is a pretty good blog post that you can go into.
link |
I might also create an entire video on the internals of torch tensor and how this works.
link |
For here, we just note that this is an extremely efficient operation.
link |
And if I delete this and come back to our EMB, we see that the shape of our EMB is 32 by 3 by 2.
link |
But we can simply ask for PyTorch to view this instead as a 32 by 6.
link |
And the way that gets flattened into a 32 by 6 array just happens that these two get stacked up in a single row.
link |
And so that's basically the concatenation operation that we're after.
link |
And you can verify that this actually gives the exact same result as what we had before.
link |
So this is an element y equals, and you can see that all the elements of these two tensors are the same.
link |
And so we get the exact same result.
link |
So long story short, we can actually just come here, and if we just view this as a 32 by 6 instead, then this multiplication will work and give us the hidden states that we're after.
link |
So if this is h, then h slash shape is now the 100-dimensional activations for every one of our 32 examples.
link |
And this gives the desired result.
link |
Let me do two things here.
link |
Number one, let's not use 32.
link |
We can, for example, do something like EMB.shape at 0 so that we don't hardcode these numbers.
link |
And this would work for any size of this EMB.
link |
Or alternatively, we can also do negative 1.
link |
When we do negative 1, PyTorch will infer what this should be.
link |
Because the number of elements must be the same, and we're saying that this is 6, PyTorch will derive that this must be 32.
link |
Or whatever else it is if EMB is of different size.
link |
The other thing is here, one more thing I'd like to point out is here when we do the concatenation, this actually is much less efficient.
link |
Because this concatenation would create a whole new tensor with a whole new storage.
link |
So new memory is being created because there's no way to concatenate tensors just by manipulating the view attributes.
link |
So this is inefficient and creates all kinds of new memory.
link |
So let me delete this now.
link |
We don't need this.
link |
And here to calculate h, we want to also dot 10h of this to get our h.
link |
So these are now numbers between negative 1 and 1 because of the 10h.
link |
And we have that the shape is 32 by 100.
link |
And that is basically this hidden layer of activations here for every one of our 32 examples.
link |
Now there's one more thing I glossed over that we have to be very careful with, and that's this plus here.
link |
In particular, we want to make sure that the broadcasting will do what we like.
link |
The shape of this is 32 by 100, and B1's shape is 100.
link |
So we see that the addition here will broadcast these two.
link |
And in particular, we have 32 by 100 broadcasting to 100.
link |
So broadcasting will align on the right, create a fake dimension here.
link |
So this will become a 1 by 100 row vector.
link |
And then it will copy vertically for every one of these rows of 32 and do an element-wise addition.
link |
So in this case, the correcting will be happening because the same bias vector will be added to all the rows of this matrix.
link |
So that is correct. That's what we'd like.
link |
And it's always good practice to just make sure so that you don't shoot yourself in the foot.
link |
And finally, let's create the final layer here.
link |
Let's create W2 and B2.
link |
The input now is 100, and the output number of neurons will be for us 27 because we have 27 possible characters that come next.
link |
So the biases will be 27 as well.
link |
So therefore, the logits, which are the outputs of this neural net, are going to be H multiplied by W2 plus B2.
link |
Logits.shape is 32 by 27, and the logits look good.
link |
Now exactly as we saw in the previous video, we want to take these logits and we want to first exponentiate them to get our fake counts.
link |
And then we want to normalize them into a probability.
link |
So prob is counts divide, and now counts.sum along the first dimension and keep them as true, exactly as in the previous video.
link |
And so prob.shape now is 32 by 27, and you'll see that every row of prob sums to 1, so it's normalized.
link |
So that gives us the probabilities.
link |
Now, of course, we have the actual letter that comes next.
link |
And that comes from this array Y, which we created during the data set creation.
link |
So Y is this last piece here, which is the identity of the next character in the sequence that we'd like to now predict.
link |
So what we'd like to do now is just as in the previous video, we'd like to index into the rows of prob,
link |
and in each row we'd like to pluck out the probability assigned to the correct character, as given here.
link |
So first we have torch.arrange of 32, which is kind of like an iterator over numbers from 0 to 31.
link |
And then we can index into prob in the following way.
link |
prob in torch.arrange of 32, which iterates the rows, and then in each row we'd like to grab this column, as given by Y.
link |
So this gives the current probabilities, as assigned by this neural network with this setting of its weights, to the correct character in the sequence.
link |
And you can see here that this looks OK for some of these characters, like this is basically 0.2.
link |
But it doesn't look very good at all for many other characters, like this is 0.070's 1 probability, and so the network thinks that some of these are extremely unlikely.
link |
But of course we haven't trained a neural network yet, so this will improve, and ideally all of these numbers here of course are 1, because then we are correctly predicting the next character.
link |
Now just as in the previous video, we want to take these probabilities, we want to look at the lock probability,
link |
and then we want to look at the average lock probability and the negative of it to create the negative log likelihood loss.
link |
So the loss here is 17, and this is the loss that we'd like to minimize to get the network to predict the correct character in the sequence.
link |
OK, so I rewrote everything here and made it a bit more respectable.
link |
So here's our data set, here's all the parameters that we defined.
link |
I'm now using a generator to make it reproducible.
link |
I clustered all the parameters into a single list of parameters, so that for example it's easy to count them and see that in total we currently have about 3400 parameters.
link |
And this is the forward pass as we developed it, and we arrive at a single number here, the loss, that is currently expressing how well this neural network works with the current setting of parameters.
link |
Now I would like to make it even more respectable.
link |
So in particular, see these lines here, where we take the logits and we calculate the loss.
link |
We're not actually reinventing the wheel here.
link |
This is just classification, and many people use classification, and that's why there is a functional.crossentropy function in PyTorch to calculate this much more efficiently.
link |
So we could just simply call f.crossentropy, and we can pass in the logits, and we can pass in the array of targets y, and this calculates the exact same loss.
link |
So in fact we can simply put this here, and erase these three lines, and we're going to get the exact same result.
link |
Now there are actually many good reasons to prefer f.crossentropy over rolling your own implementation like this.
link |
I did this for educational reasons, but you'd never use this in practice.
link |
Number one, when you use f.crossentropy, PyTorch will not actually create all these intermediate tensors, because these are all new tensors in memory, and all this is fairly inefficient to run like this.
link |
Instead, PyTorch will cluster up all these operations, and very often have fused kernels that very efficiently evaluate these expressions that are sort of like clustered mathematical operations.
link |
Number two, the backward pass can be made much more efficient, and not just because it's a fused kernel, but also analytically and mathematically it's often a very much simpler backward pass to implement.
link |
We actually sell this with micrograd.
link |
You see here when we implemented 10h, the forward pass of this operation to calculate the 10h was actually a fairly complicated mathematical expression.
link |
But because it's a clustered mathematical expression, when we did the backward pass, we didn't individually backward through the exp and the 2 times and the minus 1 and division, etc.
link |
We just said it's 1 minus t squared, and that's a much simpler mathematical expression.
link |
And we were able to do this because we're able to reuse calculations, and because we are able to mathematically and analytically derive the derivative, and often that expression simplifies mathematically, and so there's much less to implement.
link |
So not only can it be made more efficient because it runs in a fused kernel, but also because the expressions can take a much simpler form mathematically.
link |
So that's number one.
link |
Number two, under the hood, ftat cross entropy can also be significantly more numerically well behaved.
link |
Let me show you an example of how this works.
link |
Suppose we have a logits of negative 2, 3, 0, and 5, and then we are taking the exponent of it and normalizing it to sum to 1.
link |
So when logits take on these values, everything is well and good, and we get a nice probability distribution.
link |
Now consider what happens when some of these logits take on more extreme values, and that can happen during optimization of a neural network.
link |
Suppose that some of these numbers grow very negative, let's say negative 100, then actually everything will come out fine.
link |
We still get probabilities that are well behaved and they sum to 1 and everything is great.
link |
But because of the way the exp works, if you have very positive logits, let's say positive 100 in here, you actually start to run into trouble, and we get not a number here.
link |
And the reason for that is that these counts have an inf here.
link |
So if you pass in a very negative number to exp, you just get a very small number, very near zero, and that's fine.
link |
But if you pass in a very positive number, suddenly we run out of range in our floating point number that represents these counts.
link |
So basically we're taking e and we're raising it to the power of 100, and that gives us inf because we've run out of dynamic range on this floating point number that is count.
link |
And so we cannot pass very large logits through this expression.
link |
Now let me reset these numbers to something reasonable.
link |
The way PyTorch solved this is that you see how we have a well behaved result here.
link |
It turns out that because of the normalization here, you can actually offset logits by any arbitrary constant value that you want.
link |
So if I add 1 here, you actually get the exact same result.
link |
Or if I add 2, or if I subtract 3, any offset will produce the exact same probabilities.
link |
So because negative numbers are OK, but positive numbers can actually overflow this exp, what PyTorch does is it internally calculates the maximum value that occurs in the logits.
link |
And it subtracts it. So in this case it would subtract 5.
link |
And so therefore the greatest number in logits will become 0, and all the other numbers will become some negative numbers.
link |
And then the result of this is always well behaved.
link |
So even if we have 100 here previously, not good.
link |
But because PyTorch will subtract 100, this will work.
link |
And so there's many good reasons to call cross entropy.
link |
Number one, the forward pass can be much more efficient, the backward pass can be much more efficient, and also things can be much more numerically well behaved.
link |
OK, so let's now set up the training of this neural net.
link |
We have the forward pass.
link |
We don't need these, because then we have that loss is equal to the fact that cross entropy does the forward pass.
link |
Then we need the backward pass.
link |
First we want to set the gradients to be 0.
link |
So for p in parameters we want to make sure that p.grad is none, which is the same as setting it to 0 in PyTorch.
link |
And then loss.backward to populate those gradients.
link |
Once we have the gradients we can do the parameter update.
link |
So for p in parameters we want to take all the data, and we want to nudge it learning rate times p.grad.
link |
And then we want to repeat this a few times.
link |
And let's print the loss here as well.
link |
Now this won't suffice, and it will create an error, because we also have to go for p in parameters,
link |
and we have to make sure that p.requiresGrad is set to true in PyTorch.
link |
And this should just work.
link |
OK, so we started off with loss of 17, and we're decreasing it.
link |
And you see how the loss decreases a lot here.
link |
If we just run for a thousand times we get a very, very low loss.
link |
And that means that we're making very good predictions.
link |
Now the reason that this is so straightforward right now is because we're only overfitting 32 examples.
link |
So we only have 32 examples of the first five words,
link |
and therefore it's very easy to make this neural net fit only these 32 examples,
link |
because we have 3400 parameters and only 32 examples.
link |
So we're doing what's called overfitting a single batch of the data,
link |
and getting a very low loss and good predictions.
link |
But that's just because we have so many parameters for so few examples, so it's easy to make this be very low.
link |
Now we're not able to achieve exactly zero.
link |
The reason for that is we can, for example, look at logits, which are being predicted.
link |
And we can look at the max along the first dimension.
link |
And in PyTorch, max reports both the actual values that take on the maximum number, but also the indices of these.
link |
And you'll see that the indices are very close to the labels, but in some cases they differ.
link |
For example, in this very first example, the predicted index is 19, but the label is 5.
link |
And we're not able to make loss be zero, and fundamentally that's because here,
link |
the very first or the zeroth index is the example where dot dot dot is supposed to predict e,
link |
but you see how dot dot dot is also supposed to predict an o,
link |
and dot dot dot is also supposed to predict an i, and then s as well.
link |
And so basically e, o, a, or s are all possible outcomes in a training set for the exact same input.
link |
So we're not able to completely overfit and make the loss be exactly zero,
link |
but we're getting very close in the cases where there's a unique input for a unique output.
link |
In those cases we do what's called overfit, and we basically get the exact correct result.
link |
So now all we have to do is we just need to make sure that we read in the full dataset and optimize the neural net.
link |
Okay, so let's swing back up where we created the dataset, and we see that here we only used the first five words.
link |
So let me now erase this, and let me erase the print statements, otherwise we'd be printing way too much.
link |
And so when we process the full dataset of all the words, we now had 228,000 examples instead of just 32.
link |
So let's now scroll back down, the dataset is much larger, reinitialize the weights, the same number of parameters,
link |
they all require gradients, and then let's push this print.loss.item to be here,
link |
and let's just see how the optimization goes if we run this.
link |
Okay, so we started with a fairly high loss, and then as we're optimizing, the loss is coming down.
link |
But you'll notice that it takes quite a bit of time for every single iteration, so let's actually address that.
link |
Because we're doing way too much work forwarding and backwarding 228,000 examples.
link |
In practice what people usually do is they perform forward and backward pass and update on many batches of the data.
link |
So what we will want to do is we want to randomly select some portion of the dataset, and that's a mini-batch,
link |
and then only forward, backward, and update on that little mini-batch, and then we iterate on those mini-batches.
link |
So in PyTorch we can, for example, use tors.randint, and we can generate numbers between 0 and 5 and make 32 of them.
link |
I believe the size has to be a tuple in PyTorch.
link |
So we can have a tuple of 32 numbers between 0 and 5, but actually we want x.shape of 0 here.
link |
And so this creates integers that index into our dataset, and there's 32 of them.
link |
So if our mini-batch size is 32, then we can come here and we can first do mini-batch construct.
link |
So integers that we want to optimize in this single iteration are in the ix,
link |
and then we want to index into x with ix to only grab those rows.
link |
So we're only getting 32 rows of x, and therefore embeddings will again be 32 by 3 by 2, not 200,000 by 3 by 2.
link |
And then this ix has to be used not just to index into x, but also to index into y.
link |
And now this should be mini-batches, and this should be much, much faster, so it's instant almost.
link |
So this way we can run many, many examples nearly instantly and decrease the loss much, much faster.
link |
Now because we're only dealing with mini-batches, the quality of our gradient is lower.
link |
So the direction is not as reliable. It's not the actual gradient direction.
link |
But the gradient direction is good enough, even when it's estimating on only 32 examples, that it is useful.
link |
And so it's much better to have an approximate gradient and just make more steps than it is to evaluate the exact gradient and take fewer steps.
link |
So that's why in practice this works quite well.
link |
So let's now continue the optimization.
link |
Let me take out this lost item from here and place it over here at the end.
link |
So we're hovering around 2.5 or so. However, this is only the loss for that mini-batch.
link |
So let's actually evaluate the loss here for all of x and for all of y, just so we have a full sense of exactly how well the model is doing right now.
link |
So right now we're at about 2.7 on the entire training set.
link |
So let's run the optimization for a while. OK, we're at 2.6, 2.57, 2.53.
link |
OK, so one issue, of course, is we don't know if we're stepping too slow or too fast.
link |
So this point one, I just guessed it.
link |
So one question is, how do you determine this learning rate and how do we gain confidence that we're stepping in the right sort of speed?
link |
So I'll show you one way to determine a reasonable learning rate.
link |
It works as follows. Let's reset our parameters to the initial settings.
link |
And now let's print in every step, but let's only do 10 steps or so, or maybe 100 steps.
link |
We want to find a very reasonable search range, if you will.
link |
So for example, if this is very low, then we see that the loss is barely decreasing.
link |
So that's too low, basically.
link |
So let's try this one. OK, so we're decreasing the loss, but not very quickly.
link |
So that's a pretty good low range.
link |
Now let's reset it again.
link |
And now let's try to find the place at which the loss kind of explodes.
link |
So maybe at negative one.
link |
OK, we see that we're minimizing the loss, but you see how it's kind of unstable.
link |
It goes up and down quite a bit.
link |
So negative one is probably like a fast learning rate.
link |
Let's try negative 10.
link |
OK, so this isn't optimizing. This is not working very well.
link |
So negative 10 is way too big.
link |
Negative one was already kind of big.
link |
Therefore, negative one was somewhat reasonable if I reset.
link |
So I'm thinking that the right learning rate is somewhere between negative 0.001 and negative one.
link |
So the way we can do this here is we can use torch.lenspace.
link |
And we want to basically do something like this, between 0 and 1.
link |
But number of steps is one more parameter that's required.
link |
Let's do 1000 steps.
link |
This creates 1000 numbers between 0.001 and 1.
link |
But it doesn't really make sense to step between these linearly.
link |
So instead, let me create learning rate exponent.
link |
And instead of 0.001, this will be a negative 3, and this will be a 0.
link |
And then the actual LRs that we want to search over are going to be 10 to the power of LRE.
link |
So now what we're doing is we're stepping linearly between the exponents of these learning rates.
link |
This is 0.001, and this is 1, because 10 to the power of 0 is 1.
link |
And therefore, we are spaced exponentially in this interval.
link |
So these are the candidate learning rates that we want to search over, roughly.
link |
So now what we're going to do is here, we are going to run the optimization for 1000 steps.
link |
And instead of using a fixed number, we are going to use learning rate indexing into here, LRs of i, and make this i.
link |
So basically, let me reset this to be, again, starting from random, creating these learning rates between 0.001 and 1, but exponentially stepped.
link |
And here what we're doing is we're iterating 1000 times.
link |
We're going to use the learning rate that's in the beginning very, very low.
link |
In the beginning it's going to be 0.001, but by the end it's going to be 1.
link |
And then we're going to step with that learning rate.
link |
And now what we want to do is we want to keep track of the learning rates that we used.
link |
And we want to look at the losses that resulted.
link |
And so here, let me track stats.
link |
So LRI.append LR, and loss i.append loss.item.
link |
So again, reset everything, and then run.
link |
And so basically, we started with a very low learning rate, and we went all the way up to a learning rate of negative 1.
link |
And now what we can do is we can PLT.plot, and we can plot the two.
link |
So we can plot the learning rates on the x-axis, and the losses we saw on the y-axis.
link |
And often you're going to find that your plot looks something like this.
link |
Where in the beginning you had very low learning rates, so basically barely anything happened.
link |
Then we got to a nice spot here, and then as we increased the learning rate enough, we basically started to be kind of unstable here.
link |
So a good learning rate turns out to be somewhere around here.
link |
And because we have LRI here, we actually may want to do not the learning rate, but the exponent.
link |
So that would be the LRE at i is maybe what we want to log.
link |
So let me reset this and redo that calculation.
link |
But now on the x-axis, we have the exponent of the learning rate.
link |
And so we can see the exponent of the learning rate that is good to use.
link |
It would be sort of roughly in the valley here, because here the learning rates are just way too low.
link |
And then here we expect relatively good learning rates, somewhere here.
link |
And then here things are starting to explode.
link |
So somewhere around negative 1 as the exponent of the learning rate is a pretty good setting.
link |
And 10 to the negative 1 is 0.1.
link |
So 0.1 was actually a fairly good learning rate around here.
link |
And that's what we had in the initial setting.
link |
But that's roughly how you would determine it.
link |
And so here now we can take out the tracking of these.
link |
And we can just simply set LR to be 10 to the negative 1, or basically otherwise 0.1, as it was before.
link |
And now we have some confidence that this is actually a fairly good learning rate.
link |
And so now what we can do is we can crank up the iterations.
link |
We can reset our optimization.
link |
And we can run for a pretty long time using this learning rate.
link |
Oops, and we don't want to print. It's way too much printing.
link |
So let me again reset and run 10,000 steps.
link |
So we're at 2.48 roughly. Let's run another 10,000 steps.
link |
And now let's do one learning rate decay.
link |
What this means is we're going to take our learning rate and we're going to 10x lower it.
link |
And so we're at the late stages of training potentially.
link |
And we may want to go a bit slower.
link |
Let's do one more actually at 0.1 just to see if we're making a dent here.
link |
Okay, we're still making a dent.
link |
And by the way, the bigram loss that we achieved last video was 2.45.
link |
So we've already surpassed the bigram model.
link |
And once I get a sense that this is actually kind of starting to plateau off,
link |
people like to do, as I mentioned, this learning rate decay.
link |
So let's try to decay the learning rate.
link |
And we achieve about 2.3 now.
link |
Obviously, this is janky and not exactly how you would train it in production.
link |
But this is roughly what you're going through.
link |
You first find a decent learning rate using the approach that I showed you.
link |
Then you start with that learning rate and you train for a while.
link |
And then at the end, people like to do a learning rate decay
link |
where you decay the learning rate by, say, a factor of 10 and you do a few more steps.
link |
And then you get a trained network, roughly speaking.
link |
So we achieved 2.3 and dramatically improved on the bigram language model
link |
using this simple neural net as described here, using these 3400 parameters.
link |
Now there's something we have to be careful with.
link |
I said that we have a better model because we are achieving a lower loss,
link |
2.3 much lower than 2.45 with the bigram model previously.
link |
Now that's not exactly true.
link |
And the reason that's not true is that this is actually a fairly small model.
link |
But these models can get larger and larger if you keep adding neurons and parameters.
link |
So you can imagine that we don't potentially have 1000 parameters.
link |
We could have 10,000 or 100,000 or millions of parameters.
link |
And as the capacity of the neural network grows,
link |
it becomes more and more capable of overfitting your training set.
link |
What that means is that the loss on the training set, on the data that you're training on,
link |
will become very, very low, as low as zero.
link |
But all that the model is doing is memorizing your training set verbatim.
link |
So if you take that model and it looks like it's working really well,
link |
but you try to sample from it, you will basically only get examples
link |
exactly as they are in the training set.
link |
You won't get any new data.
link |
In addition to that, if you try to evaluate the loss on some withheld names or other words,
link |
you will actually see that the loss on those can be very high.
link |
And so basically it's not a good model.
link |
So the standard in the field is to split up your data set into three splits, as we call them.
link |
We have the training split, the dev split or the validation split, and the test split.
link |
So training split, dev or validation split, and test split.
link |
And typically this would be say 80% of your data set.
link |
This could be 10% and this 10% roughly.
link |
So you have these three splits of the data.
link |
Now these 80% of your trainings of the data set, the training set,
link |
is used to optimize the parameters of the model, just like we're doing here using gradient descent.
link |
These 10% of the examples, the dev or validation split,
link |
they're used for development over all the hyperparameters of your model.
link |
So hyperparameters are, for example, the size of this hidden layer, the size of the embedding.
link |
So this is 100 or a 2 for us, but we could try different things.
link |
The strength of the regularization, which we aren't using yet so far.
link |
So there's lots of different hyperparameters and settings that go into defining a neural net.
link |
And you can try many different variations of them and see whichever one works best on your validation split.
link |
So this is used to train the parameters.
link |
This is used to train the hyperparameters.
link |
And test split is used to evaluate basically the performance of the model at the end.
link |
So we're only evaluating the loss on the test split very sparingly and very few times.
link |
Because every single time you evaluate your test loss and you learn something from it,
link |
you are basically starting to also train on the test split.
link |
So you are only allowed to test the loss on the test set very, very few times.
link |
Otherwise, you risk overfitting to it as well as you experiment on your model.
link |
So let's also split up our training data into train, dev, and test.
link |
And then we are going to train on train and only evaluate on test very, very sparingly.
link |
Okay, so here we go.
link |
Here is where we took all the words and put them into X and Y tensors.
link |
So instead, let me create a new cell here.
link |
And let me just copy-paste some code here because I don't think it's that complex.
link |
But we're going to try to save a little bit of time.
link |
I'm converting this to be a function now.
link |
And this function takes some list of words and builds the arrays X and Y for those words only.
link |
And then here I am shuffling up all the words.
link |
So these are the input words that we get.
link |
We are randomly shuffling them all up.
link |
And then we're going to set N1 to be the number of examples that is 80% of the words and N2 to be 90% of the words.
link |
So basically if length of words is 30,000, N1 is 25,000 and N2 is 28,000.
link |
And so here we see that I'm calling buildDataset() to build the training set X and Y by indexing into up to N1.
link |
So we're going to have only 25,000 training words.
link |
And then we're going to have roughly N2 minus N1, 3000 validation examples or dev examples.
link |
And we're going to have length of words basically minus N2 or 3204 examples here for the test set.
link |
So now we have Xs and Ys for all those three splits.
link |
Oh yeah, I'm printing their size here inside the function as well.
link |
But here we don't have words, but these are already the individual examples made from those words.
link |
So let's now scroll down here.
link |
And the data set now for training is more like this.
link |
And then when we reset the network, when we're training, we're only going to be training using X train, X train, and Y train.
link |
So that's the only thing we're training on.
link |
Let's see where we are on a single batch.
link |
Let's now train maybe a few more steps.
link |
Training neural networks can take a while.
link |
Usually you don't do it inline.
link |
You launch a bunch of jobs and you wait for them to finish.
link |
It can take multiple days and so on.
link |
But basically this is a very small network.
link |
So the loss is pretty good.
link |
Oh, we accidentally used a learning rate that is way too low.
link |
So let me actually come back.
link |
We used the decay learning rate of 0.01.
link |
So this will train much faster.
link |
And then here when we evaluate, let's use the dev set here.
link |
And Y dev to evaluate the loss.
link |
And let's now decay the learning rate and only do say 10,000 examples.
link |
And let's evaluate the dev loss once here.
link |
So we're getting about 2.3 on dev.
link |
And so the neural network when it was training did not see these dev examples.
link |
It hasn't optimized on them.
link |
And yet when we evaluate the loss on these dev, we actually get a pretty decent loss.
link |
And so we can also look at what the loss is on all of training set.
link |
And so we see that the training and the dev loss are about equal.
link |
So we're not overfitting.
link |
This model is not powerful enough to just be purely memorizing the data.
link |
And so far we are what's called underfitting because the training loss and the dev or test losses are roughly equal.
link |
So what that typically means is that our network is very tiny, very small.
link |
And we expect to make performance improvements by scaling up the size of this neural net.
link |
So let's do that now.
link |
So let's come over here and let's increase the size of the neural net.
link |
The easiest way to do this is we can come here to the hidden layer, which currently has 100 neurons.
link |
And let's just bump this up.
link |
So let's do 300 neurons.
link |
And then this is also 300 biases.
link |
And here we have 300 inputs into the final layer.
link |
So let's initialize our neural net.
link |
We now have 10,000 parameters instead of 3,000 parameters.
link |
And then we're not using this.
link |
And then here what I'd like to do is I'd like to actually keep track of that.
link |
OK, let's just do this.
link |
Let's keep stats again.
link |
And here when we're keeping track of the loss, let's just also keep track of the steps.
link |
And let's just have an eye here.
link |
And let's train on 30,000.
link |
Or rather say, let's try 30,000.
link |
And we are at 0.1.
link |
And we should be able to run this and optimize the neural net.
link |
And then here basically I want to plt.plot the steps against the loss.
link |
So these are the x's and the y's.
link |
And this is the loss function and how it's being optimized.
link |
Now you see that there's quite a bit of thickness to this.
link |
And that's because we are optimizing over these mini-batches.
link |
And the mini-batches create a little bit of noise in this.
link |
Where are we in the defset?
link |
So we still haven't optimized this neural net very well.
link |
And that's probably because we made it bigger.
link |
It might take longer for this neural net to converge.
link |
And so let's continue training.
link |
Yeah, let's just continue training.
link |
One possibility is that the batch size is so low that we just have way too much noise in the training.
link |
And we may want to increase the batch size so that we have a bit more correct gradient.
link |
And we're not thrashing too much.
link |
And we can actually optimize more properly.
link |
This will now become meaningless because we've reinitialized these.
link |
So this looks not pleasing right now.
link |
But the problem is a tiny improvement, but it's so hard to tell.
link |
Let's try to decrease the learning rate by a factor of two.
link |
Okay, we're at 2.32.
link |
Let's continue training.
link |
We basically expect to see a lower loss than what we had before.
link |
Because now we have a much, much bigger model.
link |
And we were underfitting.
link |
So we'd expect that increasing the size of the model should help the neural net.
link |
Okay, so that's not happening too well.
link |
Now, one other concern is that even though we've made the hidden layer much, much bigger,
link |
it could be that the bottleneck of the network right now are these embeddings that are two-dimensional.
link |
It can be that we're just cramming way too many characters into just two dimensions.
link |
And the neural net is not able to really use that space effectively.
link |
And that is sort of like the bottleneck to our network's performance.
link |
So just by decreasing the learning rate, I was able to make quite a bit of progress.
link |
Let's run this one more time.
link |
And then evaluate the training and the dev loss.
link |
Now, one more thing after training that I'd like to do is I'd like to visualize the embedding vectors for these characters
link |
before we scale up the embedding size from 2.
link |
Because we'd like to make this bottleneck potentially go away.
link |
But once I make this greater than 2, we won't be able to visualize them.
link |
So here, we're at 2.23 and 2.24.
link |
So we're not improving much more.
link |
And maybe the bottleneck now is the character embedding size, which is 2.
link |
So here I have a bunch of code that will create a figure.
link |
And then we're going to visualize the embeddings that were trained by the neural net on these characters.
link |
Because right now the embedding size is just 2.
link |
So we can visualize all the characters with the X and the Y coordinates as the two embedding locations for each of these characters.
link |
And so here are the X coordinates and the Y coordinates, which are the columns of C.
link |
And then for each one, I also include the text of the little character.
link |
So here what we see is actually kind of interesting.
link |
The network has basically learned to separate out the characters and cluster them a little bit.
link |
So for example, you see how the vowels A, E, I, O, U are clustered up here?
link |
So what that's telling us is that the neural net treats these as very similar, right?
link |
Because when they feed into the neural net, the embedding for all these characters is very similar.
link |
And so the neural net thinks that they're very similar and kind of interchangeable, if that makes sense.
link |
Then the points that are really far away are, for example, Q.
link |
Q is kind of treated as an exception, and Q has a very special embedding vector, so to speak.
link |
Similarly, dot, which is a special character, is all the way out here.
link |
And a lot of the other letters are sort of clustered up here.
link |
And so it's kind of interesting that there's a little bit of structure here after the training.
link |
And it's definitely not random, and these embeddings make sense.
link |
So we're now going to scale up the embedding size and won't be able to visualize it directly.
link |
But we expect that, because we're underfitting, and we made this layer much bigger and did not sufficiently improve the loss,
link |
we're thinking that the constraint to better performance right now could be these embedding vectors.
link |
So let's make them bigger.
link |
So let's scroll up here, and now we don't have two-dimensional embeddings.
link |
We are going to have, say, ten-dimensional embeddings for each word.
link |
Then this layer will receive 3 times 10, so 30 inputs will go into the hidden layer.
link |
Let's also make the hidden layer a bit smaller.
link |
So instead of 300, let's just do 200 neurons in that hidden layer.
link |
So now the total number of elements will be slightly bigger, at 11,000.
link |
And then here we have to be a bit careful, because the learning rate we set to 0.1.
link |
Here we are hardcoding 6.
link |
Obviously, if you're working in production, you don't want to be hardcoding magic numbers.
link |
But instead of 6, this should now be 30.
link |
Let's run for 50,000 iterations, and let me split out the initialization here outside,
link |
so that when we run this cell multiple times, it's not going to wipe out our loss.
link |
In addition to that, instead of logging loss.item, let's do log10.
link |
I believe that's a function of the loss, and I'll show you why in a second.
link |
Let's optimize this.
link |
Basically, I'd like to plot the log loss instead of the loss, because when you plot the loss,
link |
many times it can have this hockey stick appearance, and log squashes it in, so it just looks nicer.
link |
The x-axis is step i, and the y-axis will be the loss i.
link |
And then here this is 30.
link |
Ideally, we wouldn't be hardcoding these, because let's look at the loss.
link |
It's again very thick, because the minibatch size is very small.
link |
But the total loss over the training set is 2.3, and the def set is 2.38 as well.
link |
Let's try to now decrease the learning rate by a factor of 10, and train for another 50,000 iterations.
link |
We'd hope that we would be able to beat 2.32.
link |
But again, we're just doing this very haphazardly, so I don't actually have confidence that
link |
our learning rate is set very well, that our learning rate decay, which we just do at random, is set very well.
link |
The optimization here is kind of suspect, to be honest, and this is not how you would do it typically in production.
link |
In production, you would create parameters or hyperparameters out of all these settings,
link |
and then you would run lots of experiments and see whichever ones are working well for you.
link |
We have 2.17 now, and 2.2.
link |
So you see how the training and the validation performance are starting to slightly slowly depart.
link |
Maybe we're getting the sense that the neural net is getting good enough, or that the number of parameters is large enough,
link |
that we are slowly starting to overfit.
link |
Let's maybe run one more iteration of this and see where we get.
link |
Basically, you would be running lots of experiments, and then you are slowly scrutinizing whichever ones give you the best dev performance.
link |
Then once you find all the hyperparameters that make your dev performance good,
link |
you take that model and you evaluate the test set performance a single time.
link |
That's the number that you report in your paper or wherever else you want to talk about and brag about your model.
link |
Let's then rerun the plot and rerun the train and dev.
link |
Because we're getting lower loss now, it is the case that the embedding size of these was holding us back very likely.
link |
2.16 and 2.19 is what we're roughly getting.
link |
There are many ways to go from here.
link |
We can continue tuning the optimization.
link |
We can continue, for example, playing with the size of the neural net.
link |
Or we can increase the number of words or characters in our case that we are taking as an input.
link |
Instead of just three characters, we could be taking more characters than as an input.
link |
That could further improve the loss.
link |
I changed the code slightly.
link |
We have here 200,000 steps of the optimization.
link |
In the first 100,000, we're using a learning rate of 0.1.
link |
Then in the next 100,000, we're using a learning rate of 0.01.
link |
This is the loss that I achieve.
link |
These are the performance on the training and validation loss.
link |
In particular, the best validation loss I've been able to obtain in the last 30 minutes or so is 2.17.
link |
Now I invite you to beat this number.
link |
You have quite a few knobs available to you to, I think, surpass this number.
link |
Number one, you can of course change the number of neurons in the hidden layer of this model.
link |
You can change the dimensionality of the embedding lookup table.
link |
You can change the number of characters that are feeding in as an input, as the context, into this model.
link |
And then, of course, you can change the details of the optimization.
link |
How long are we running?
link |
What is the learning rate?
link |
How does it change over time?
link |
How does it decay?
link |
You can change the batch size, and you may be able to actually achieve a much better convergence speed
link |
in terms of how many seconds or minutes it takes to train the model and get your result in terms of really good loss.
link |
And then, of course, I actually invite you to read this paper.
link |
It is 19 pages, but at this point, you should actually be able to read a good chunk of this paper
link |
and understand pretty good chunks of it.
link |
And this paper also has quite a few ideas for improvements that you can play with.
link |
So all those are knobs available to you, and you should be able to beat this number.
link |
I'm leaving that as an exercise to the reader.
link |
And that's it for now, and I'll see you next time.
link |
Before we wrap up, I also wanted to show how you would sample from the model.
link |
So we're going to generate 20 samples.
link |
At first, we begin with all dots, so that's the context.
link |
And then until we generate the 0th character again,
link |
we're going to embed the current context using the embedding table C.
link |
Now, usually here, the first dimension was the size of the training set,
link |
but here we're only working with a single example that we're generating,
link |
so this is just dimension 1, just for simplicity.
link |
And so this embedding then gets projected into the hidden state.
link |
You get the logits.
link |
Now we calculate the probabilities.
link |
For that, you can use f.softmax of logits,
link |
and that just basically exponentiates the logits and makes them sum to 1.
link |
And similar to cross-entropy, it is careful that there's no overflows.
link |
Once we have the probabilities, we sample from them using torch.multinomial
link |
to get our next index, and then we shift the context window to append the index and record it.
link |
And then we can just decode all the integers to strings and print them out.
link |
And so these are some example samples, and you can see that the model now works much better.
link |
So the words here are much more word-like or name-like.
link |
So we have things like ham, joe's, lilla.
link |
It's starting to sound a little bit more name-like.
link |
So we're definitely making progress, but we can still improve on this model quite a lot.
link |
Okay, sorry, there's some bonus content.
link |
I wanted to mention that I want to make these notebooks more accessible,
link |
and so I don't want you to have to install Jupyter Notebooks and Torch and everything else.
link |
So I will be sharing a link to a Google Colab,
link |
and the Google Colab will look like a notebook in your browser.
link |
And you can just go to a URL, and you'll be able to execute all of the code that you saw in the Google Colab.
link |
And so this is me executing the code in this lecture, and I shortened it a little bit.
link |
But basically, you're able to train the exact same network and then plot and sample from the model,
link |
and everything is ready for you to tinker with the numbers right there in your browser, no installation necessary.
link |
So I just wanted to point that out, and the link to this will be in the video description.