| Map > Data Science > Predicting the Future > Modeling > Classification > Large Language Model | ||||||||||||||||||||
Large Language Model |
||||||||||||||||||||
| At its simplest, a Large Language Model (LLM) does this: "given the words so far, predict what comes next". For example: | ||||||||||||||||||||
|
The patient has high blood |
||||||||||||||||||||
|
The model might assign probabilities: |
||||||||||||||||||||
|
pressure 0.72 |
||||||||||||||||||||
| It selects pressure, producing: | ||||||||||||||||||||
|
The patient has high blood pressure |
||||||||||||||||||||
| Then it predicts the next token again. So fundamentally: | ||||||||||||||||||||
|
LLM = next-token prediction + a very large neural network trained on enormous amounts of text |
||||||||||||||||||||
| Our first model will be deliberately a tiny LLM. | ||||||||||||||||||||
|
Vocabulary |
||||||||||||||||||||
| For example, suppose our vocabulary is: | ||||||||||||||||||||
|
["I", "am", "a", "doctor", "scientist", "."] |
||||||||||||||||||||
| We convert: | ||||||||||||||||||||
|
"I am a doctor" |
||||||||||||||||||||
| into: | ||||||||||||||||||||
|
[0, 1, 2, 3] |
||||||||||||||||||||
| The numbers themselves don't mean anything. They are simply IDs. | ||||||||||||||||||||
| 1- Create a training data | ||||||||||||||||||||
| Suppose our training text is: | ||||||||||||||||||||
|
I am a scientist. |
||||||||||||||||||||
| The model learns examples such as: | ||||||||||||||||||||
|
||||||||||||||||||||
| The important concept is: The target is the next token. | ||||||||||||||||||||
| 2- Create a tokenizer | ||||||||||||||||||||
| For our first model, we'll use a very simple word tokenizer. | ||||||||||||||||||||
| Python
text = """ |
||||||||||||||||||||
| You might get: | ||||||||||||||||||||
|
['.', 'I', 'a', 'am', 'data', 'doctor', 'like', 'science', 'scientist'] |
||||||||||||||||||||
| Now: | ||||||||||||||||||||
| Python
sentence = "I am a doctor ." |
||||||||||||||||||||
| For example: | ||||||||||||||||||||
|
[1, 3, 2, 5, 0] |
||||||||||||||||||||
| 3- Create input/target pairs | ||||||||||||||||||||
|
Python import torch |
||||||||||||||||||||
| If the text is: | ||||||||||||||||||||
|
I am a scientist . |
||||||||||||||||||||
| We have: | ||||||||||||||||||||
|
X: |
||||||||||||||||||||
| So the model learns: | ||||||||||||||||||||
|
I → am |
||||||||||||||||||||
| 4- The embedding layer | ||||||||||||||||||||
| This is one of the most important ideas. Instead of representing: | ||||||||||||||||||||
|
doctor = 5 |
||||||||||||||||||||
| as meaningless numbers, we represent each word with a vector. For example: | ||||||||||||||||||||
|
doctor → [0.21, -0.43, 0.82, 0.17] |
||||||||||||||||||||
| The model learns these vectors during training. | ||||||||||||||||||||
| Python
embedding = torch.nn.Embedding(num_embeddings=len(vocab), embedding_dim=16) x =
torch.tensor([word_to_id["doctor"]]) |
||||||||||||||||||||
| Now every word (e.g., doctor) becomes a 16-dimensional vector. | ||||||||||||||||||||
|
[-0.3165, -2.1291, 0.2706, 0.3934, -0.3485, 1.9182, 0.8745, 0.2270, |
||||||||||||||||||||
| 5- Our first language model | ||||||||||||||||||||
| Let's create a very small neural network. | ||||||||||||||||||||
|
Token |
||||||||||||||||||||
| Python
import torch |
||||||||||||||||||||
| 6- Train the model | ||||||||||||||||||||
| We need a loss function. The standard loss for language modeling is Cross-Entropy Loss. | ||||||||||||||||||||
| Python
model = TinyLLM(vocab_size=len(vocab),
embedding_dim=32) |
||||||||||||||||||||
| Now train: | ||||||||||||||||||||
| Python
for epoch in range(1000): |
||||||||||||||||||||
| You should see the loss generally decrease. | ||||||||||||||||||||
|
Epoch: 0 Loss: 2.492607355117798 |
||||||||||||||||||||
| 7- Ask model to predict | ||||||||||||||||||||
| Now give it a word. | ||||||||||||||||||||
| Python
word = "I" |
||||||||||||||||||||
| Find the most likely word: | ||||||||||||||||||||
| Python
prediction =
torch.argmax(probabilities, dim=-1) |
||||||||||||||||||||
| Hopefully: | ||||||||||||||||||||
|
am |
||||||||||||||||||||
| You have built a tiny language model. | ||||||||||||||||||||
| 8- Give the model a context window | ||||||||||||||||||||
| A real LLM needs to understand a sequence. For example: | ||||||||||||||||||||
|
The dog chased the cat because it was hungry. |
||||||||||||||||||||
| The meaning of it depends on the surrounding context. | ||||||||||||||||||||
| This is where the architecture becomes much more interesting. Instead of: | ||||||||||||||||||||
|
I → am |
||||||||||||||||||||
| We train: | ||||||||||||||||||||
|
I am → a |
||||||||||||||||||||
| For a context length of 3: | ||||||||||||||||||||
|
||||||||||||||||||||
| 9- The missing part: Attention | ||||||||||||||||||||
| This is the breakthrough behind modern LLMs. | ||||||||||||||||||||
| Suppose the model receives: | ||||||||||||||||||||
|
The patient took aspirin because he had chest pain |
||||||||||||||||||||
| When predicting something later, the model needs to decide: Which previous words are important? | ||||||||||||||||||||
| Attention allows the model to calculate relationships such as: | ||||||||||||||||||||
|
patient ←→ he |
||||||||||||||||||||
| Instead of treating every previous token equally. Conceptually, The model learns which tokens should pay attention to which other tokens. | ||||||||||||||||||||
| 10- Transformers | ||||||||||||||||||||
| Modern LLMs are essentially stacks of Transformer blocks. A simplified LLM looks like: | ||||||||||||||||||||
|
TEXT |
||||||||||||||||||||
| GPT-type models are essentially very large versions of this idea, with many engineering improvements. | ||||||||||||||||||||
| Let's build a complete tiny GPT-style Transformer in Python, train it on a small English text, and then generate new text. The goal is educational: you'll be able to see exactly how tokenization → embeddings → attention → transformer → prediction → generation works. | ||||||||||||||||||||
![]() |
||||||||||||||||||||