Skip to content

Commit 47826a5

Browse files
committed
[skip ci] fix typos
1 parent f36ddee commit 47826a5

1 file changed

Lines changed: 29 additions & 18 deletions

File tree

32_language_modeling_1.ipynb

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@
5050
"The purpose of this multi-part notebook is to give a gentle introduction to the PyTorch library, with a focus on language modeling.\n",
5151
"At high-level, we will build a progressively more complex **character-level language model** that can generate more text similar to the training data.\n",
5252
"\n",
53-
"The final result is not meant to be a \"production-ready\" language model, but rather a simple yet effective example of how to use PyTorch for language modeling. Along the way, we will learn the fundamental building blocks that lay the groundwork for more complex models, including the base models that powers the state-of-the-art LLMs and derived products, like our friendly and always helpful assistant ChatGPT.\n",
53+
"The final result is not meant to be a \"production-ready\" language model, but rather a simple yet effective example of how to use PyTorch for language modeling.\n",
54+
"Along the way, we will learn the fundamental building blocks that lay the groundwork for more complex models, including the base models that power the state-of-the-art LLMs and derived products, like our friendly and always helpful assistant ChatGPT.\n",
5455
"\n",
5556
"The final implementation will allow you to experiment with different models, starting from the most simple and basic one (a **bigram** model) to a more complex **RNN** and finally a **Transformer** model.\n"
5657
]
@@ -454,7 +455,7 @@
454455
"source": [
455456
"The results are quite terrible, although they're reasonable given the simplicity of the model and the patterns we're trying to capture.\n",
456457
"\n",
457-
"The core problem is that a bigram model looks only the the frequency of a pair of tokens, but it has zero information of what's most likely to come before or after those two tokens.\n",
458+
"The core problem is that a bigram model looks only at the frequency of a pair of tokens, but it has zero information of what's most likely to come before or after those two tokens.\n",
458459
"You can imagine that the obvious next step is a **trigram** model, which looks at the frequency of a triplet of tokens.\n",
459460
"\n",
460461
"Let's now improve a bit our code: the first thing is to compute **all** the probabilities once, and then sample from them.\n",
@@ -545,9 +546,11 @@
545546
"source": [
546547
"We have built a bigram language model by counting letter combination frequencies, then normalizing and sampling with that probability base.\n",
547548
"\n",
548-
"We trained the model, we sampled from the model (iteratively, character-wise). But its still bad at coming up with names.\n",
549+
"We trained the model, we sampled from the model (iteratively, character-wise).\n",
550+
"But its still bad at coming up with names.\n",
549551
"\n",
550-
"But how bad? We know that the model's \"knowledge\" is represented by `P`, but how can we boil down the model's quality in one value?\n",
552+
"But how bad?\n",
553+
"We know that the model's \"knowledge\" is represented by `P`, but how can we boil down the model's quality in one value?\n",
551554
"\n",
552555
"First, let's look at the bigrams we created from the dataset: the bigrams to `emma` are `.e, em, mm, ma, a.`.\n",
553556
"**What probability does the model assign to each of those bigrams?**"
@@ -622,7 +625,8 @@
622625
"We calculated a negative log-likelihood, because this follows the convention of setting the goal to minimize the **loss function**, the function that drives the optimization (i.e., training) process.\n",
623626
"The lower the loss/negative log-likelihood, the better the model.\n",
624627
"\n",
625-
"We got $2.45$ for the model. The lower, the better.\n",
628+
"We got $2.45$ for the model.\n",
629+
"The lower, the better.\n",
626630
"We need to find the parameters that reduce this value.\n",
627631
"\n",
628632
"**Goal:** Maximize likelihood of the trained data w. r. t. model parameters in `P`\n",
@@ -850,12 +854,14 @@
850854
"Something like: $\\text{output} = \\text{activation}(\\text{weights} \\cdot \\text{input} + \\text{bias})$\n",
851855
"\n",
852856
"If we were to feed our characters as integer indexes, we would have a sequence of integer indexes as input.\n",
853-
"If `a` is 1 and `z` is 25, the weight applied to `z` will have 25 times more impact on the output than `a`. This creates an arbitrary and misleading mathematical relationship.\n",
857+
"If `a` is 1 and `z` is 25, the weight applied to `z` will have 25 times more impact on the output than `a`.\n",
858+
"This creates an arbitrary and misleading mathematical relationship.\n",
854859
"\n",
855860
"Moreover, during the training (optimization) phase, the updates to the weights will be proportional to their input values.\n",
856861
"Larger input values will cause larger updates to the weights, which can lead to unstable training.\n",
857862
"\n",
858-
"And lastly, the network has no reference to the potential value range. It doesn't know that the values are constrained to a specific set (like 0-25 for letters).\n",
863+
"And lastly, the network has no reference to the potential value range.\n",
864+
"It doesn't know that the values are constrained to a specific set (like 0-25 for letters).\n",
859865
"\n",
860866
"To address all these issues, we can use **one-hot encoding**.\n",
861867
"One-hot encoding each letter means creating a vector where only one position has a value of 1 (corresponding to that letter's position in the alphabet) and all other positions are 0.\n",
@@ -921,7 +927,7 @@
921927
"metadata": {},
922928
"source": [
923929
"One problem is the `dtype` of the one-hot encoded tensor.\n",
924-
"It's `torch.int64` by default (inferred from our data), but we need `torch.float32` to have a the input suitable for the mathematical operations the network will perform.\n",
930+
"It is `torch.int64` by default (inferred from our data), but we need `torch.float32` to have an input suitable for the mathematical operations the network will perform.\n",
925931
"\n",
926932
"We can convert it using `.float()`:"
927933
]
@@ -972,7 +978,8 @@
972978
"metadata": {},
973979
"source": [
974980
"`W` is a **single** neuron.\n",
975-
"Multiplying it by `xenc` makes it 'react' to the one-hot encoded input. The result is a $5\\times 1$ vector.\n",
981+
"Multiplying it by `xenc` makes it 'react' to the one-hot encoded input.\n",
982+
"The result is a $5\\times 1$ vector.\n",
976983
"\n",
977984
"`.emma` has $5$ characters, we have $1$ neuron.\n",
978985
"When this neuron processes the 5 characters of \".emma\", it produces 5 activation values, one for each character.\n",
@@ -1041,7 +1048,7 @@
10411048
"metadata": {},
10421049
"source": [
10431050
"We want the neurons per input (per character) to come up with a $27$-dimensional activation of values that could be transformed into a normal distribution on what character to choose next.\n",
1044-
"We've seen that with the Bigram's probability distribution, given info per character on what character ist most likely to follow.\n",
1051+
"We've seen that with the Bigram's probability distribution, given info per character on what character is most likely to follow.\n",
10451052
"\n",
10461053
"Right now, for every character we get $27$ numbers, positive and negative, but not following a normal distribution.\n",
10471054
"\n",
@@ -1091,7 +1098,7 @@
10911098
"source": [
10921099
"It might seem unusual, but after this transformation, we have a set of numbers that we can use just like the actual counts from the bigram model.\n",
10931100
"\n",
1094-
"All the values are non-negativethink of them as \"pseudo-counts.\"\n",
1101+
"All the values are non-negative: think of them as \"pseudo-counts.\"\n",
10951102
"Now, our goal is simply to adjust the weights `W` so that the network produces the correct character indices as output."
10961103
]
10971104
},
@@ -1127,7 +1134,8 @@
11271134
"id": "77",
11281135
"metadata": {},
11291136
"source": [
1130-
"We can now evaluate how well our neural network predicts the next character in a sequence. For each bigram (pair of consecutive characters), we:\n",
1137+
"We can now evaluate how well our neural network predicts the next character in a sequence.\n",
1138+
"For each bigram (pair of consecutive characters), we:\n",
11311139
"\n",
11321140
"- Feed the input character to the neural network.\n",
11331141
"- Get the predicted probability distribution for the next character.\n",
@@ -1195,9 +1203,11 @@
11951203
"- In each case, the probability assigned to the correct next character is relatively low, meaning the model is not yet confident in its predictions.\n",
11961204
"- The most likely character predicted by the model is often not the correct one.\n",
11971205
"- The negative log likelihood values are relatively high, indicating the model is “surprised” by the true next character.\n",
1198-
"- The final line reports the average negative log likelihood (loss) across all bigrams: 3.44. This is a key metric for training—the goal is to minimize this value by adjusting the model’s weights.\n",
1206+
"- The final line reports the average negative log likelihood (loss) across all bigrams: 3.44.\n",
1207+
"This is a key metric for training — the goal is to minimize this value by adjusting the model’s weights.\n",
11991208
"\n",
1200-
"The network is currently not very accurate at predicting the next character, as shown by the low probabilities for the correct answers and the high loss. This is expected at the start, before any training."
1209+
"The network is currently not very accurate at predicting the next character, as shown by the low probabilities for the correct answers and the high loss.\n",
1210+
"This is expected at the start, before any training."
12011211
]
12021212
},
12031213
{
@@ -1216,10 +1226,11 @@
12161226
"Remember that we started with `W` as a completely random matrix of floats.\n",
12171227
"Hoping that random initialization would yield a good solution is like hoping that a random collection of Lego bricks will build a house.\n",
12181228
"\n",
1219-
"Instead, we will actively improve the model’s predictions. Specifically, we will adjust the weights in the matrix `W` to increase the probability of correctly predicting the second character in each bigram.\n",
1229+
"Instead, we will actively improve the model’s predictions.\n",
1230+
"Specifically, we will adjust the weights in the matrix `W` to increase the probability of correctly predicting the second character in each bigram.\n",
12201231
"\n",
12211232
"This is done by computing how the loss changes with respect to each weight (i.e., calculating the gradients), and then updating the weights in a way to reduce the overall loss.\n",
1222-
"This processcalled **gradient-based optimization**enables the neural network to learn from its mistakes and become better at predicting the next character in the sequence."
1233+
"This processcalled **gradient-based optimization**enables the neural network to learn from its mistakes and become better at predicting the next character in the sequence."
12231234
]
12241235
},
12251236
{
@@ -1415,7 +1426,7 @@
14151426
"id": "95",
14161427
"metadata": {},
14171428
"source": [
1418-
"A this step, we can optionally tell PyTorch to use a GPU if available."
1429+
"At this step, we can optionally tell PyTorch to use a GPU if available."
14191430
]
14201431
},
14211432
{
@@ -1532,7 +1543,7 @@
15321543
"While our current neural network doesn't outperform the simpler bigram approach, its architecture allows for natural extension to more complex patterns.\n",
15331544
"As we add layers and consider longer sequences of characters, the neural network framework will scale elegantly.\n",
15341545
"\n",
1535-
"Consider the fundamental scaling challenge: with a bigram model looking at just the previous character, we need to store $27^2 = 729$ probabilities (one for each possible character pair).\n",
1546+
"Consider the fundamental scaling challenge: with a bigram model looking at just the previous character, we need to store $27^2 = 729$ probabilities (one for each possible character pairs).\n",
15361547
"If we wanted to consider the previous 10 characters to make better predictions:\n",
15371548
"\n",
15381549
"- A traditional n-gram approach would require storing $27^{10} \\approx 205$ trillion different probability values—completely impractical in terms of memory and impossible to train with limited data.\n",

0 commit comments

Comments
 (0)