LLMs, Unboxed (Inside LLMs)

Large Language Model (LLM) is a term you have probably heard frequently in recent years, especially in the field of AI. LLMs are often discussed alongside terms such as chatbots, AI, AGI, and even 'black boxes.' However, at their core, LLMs are neural networks trained to learn patterns in language.

For a formal introduction, a Large Language Model (LLM) is a type of neural network trained to predict the next token based on the tokens that came before it. That is the core idea behind an LLM.

A hierarchical view of how the different fields are related.

Think of it this way: imagine you have read thousands of books. Someone gives you a paragraph and asks you to predict what comes next. Because you have been exposed to so much language, you have learned patterns, grammar, vocabulary, writing styles, facts, and relationships between concepts. You can use those patterns to make a reasonable prediction about what comes next.

LLMs do something similar. They learn patterns from the data they are trained on. The major difference is that, unlike humans, they do not learn primarily by consciously reading and understanding text. Instead, they process enormous amounts of data through mathematical computations and adjust billions of numerical parameters to become better at predicting the next token.

How LLMs Are Created

Creating a modern LLM can broadly be understood as happening in two major phases:

  • Pretraining
  • Fine-Tuning

Pretraining an LLM involves learning to predict the next token from large amounts of text. The resulting pretrained model can then be fine-tuned on a smaller, labeled dataset for specific tasks.

A useful analogy is the difference between learning to read and write and then learning to write a technical manual.

During pretraining, the model develops broad capabilities by learning from enormous amounts of general-purpose data. Fine-tuning then builds on those foundations and teaches the model to perform particular tasks or behave in specific ways.

You do not need to relearn reading and writing every time you specialize in a new subject. Similarly, a model does not start from scratch during fine-tuning; it builds upon what it learned during pretraining.

Pretraining

During the pretraining phase, the model is trained on a massive collection of mostly unlabeled text. This can include books, Wikipedia articles, web pages, research papers, transcripts, and many other sources of text.

The important word here is 'unlabeled.'. Humans do not need to manually go through all of this text and label each example with the correct answer. Instead, the text itself provides the learning signal.

This is known as self-supervised learning because the supervision, the correct answer the model should learn, comes directly from the data.

For next-token prediction, the correct answer is simply the actual next token that appears in the original text.

Example: Suppose the text is: "The cat sat on the mat."

When the model sees: “The cat sat on the ...”, the correct next token is “mat.”

The text therefore provides both the input and the expected output naturally.

The model repeats this process across an enormous amount of text, making predictions, measuring how wrong those predictions are, and adjusting its parameters to improve future predictions.

After pretraining is complete, what we have is a pretrained model. When that model is broad and capable enough to be adapted to many different tasks, it is commonly referred to as a foundation model. This foundation model can then be further trained or adapted through methods such as fine-tuning to perform specific tasks or follow particular instructions.

Fine-Tuning

Fine-tuning takes a pretrained base model and trains it further on a smaller, more specialized dataset.

The purpose is to teach the model to perform particular tasks or behave in particular ways.

For example, a pretrained model may already have a broad understanding of language, but fine-tuning can help it become better at following instructions, answering questions in a particular format, or performing a specialized task.

The key idea is that fine-tuning builds on the knowledge and representations learned during pretraining rather than starting from scratch.

What Does 'Large' Mean in LLM?

The word 'large' in Large Language Model generally refers to the enormous scale of both the training data and the model itself.

Amount of Data

LLMs are trained on extremely large amounts of text from many different sources, including books, websites, articles, research papers, code, transcripts, and other documents.

The amount of training data can reach billions or even trillions of words, depending on the model and its training process.

This large and diverse collection of text exposes the model to many different language patterns, ideas, writing styles, facts, and relationships. This helps the model learn how language is used in different situations.

Number of Parameters

The other meaning of 'large' refers to the number of parameters inside the neural network.

Think of parameters as billions of tiny 'knobs' inside the model.

These parameters determine how information flows through the neural network. During training, the model continuously adjusts these values so that its predictions become more accurate

Parameters are the adjustable numerical values inside a neural network. An LLM can contain billions of parameters, with each parameter influencing how information is transformed as it passes through the network. During training, these parameters are gradually adjusted to reduce prediction errors. Once training is complete, the resulting parameter values encode patterns and representations the model has learned from its training data.

Importantly, however, it is better to think of parameters as encoding learned patterns and representations, rather than as individual containers of specific facts. A particular fact is generally not stored in one specific parameter.

Why Are LLMs Sometimes Called 'Black Boxes'?

One reason LLMs are sometimes described as black boxes is that, although we know the architecture and mathematical operations involved, it can be extremely difficult to understand exactly how the billions of parameters collectively produce a particular output.

We know that the model contains parameters and that those parameters are adjusted during training. We also know the mathematical operations performed by the network.

What is much harder to determine is:

  • Which parameters contribute to a particular prediction?

  • How do different parts of the network interact?

  • Where exactly is a particular piece of knowledge represented?

  • How does the model combine different patterns to produce a specific answer?

This is an active area of research known broadly as mechanistic interpretability and AI interpretability.

How Does The Model Learn?

At a simple level, training works by repeatedly comparing the model’s prediction with the correct next word.

For example, suppose we have the sentence:

'The cat sat on the _____.'

The correct next word is 'mat', but the model predicts 'floor' as the most likely answer. The model has made a prediction error.

This error is measured using a mathematical value called loss. In simple terms, loss tells us how wrong the model’s prediction was. The larger the error, the higher the loss.


An optimization algorithm then adjusts the model’s parameters to reduce this loss.

This process is repeated again and again across enormous amounts of text, allowing the model to gradually improve its predictions.

The overall goal of training is to minimize the loss, or in simple terms, reduce the model’s prediction errors.

Emergent Abilities

One of the most interesting properties of sufficiently capable language models is that they can sometimes perform tasks they were not explicitly programmed to perform.

These capabilities can arise from the general patterns and representations learned during large-scale pretraining. In some contexts, researchers refer to such capabilities as emergent abilities.

An LLM might, for example, learn enough about languages during pretraining to perform translation even if it was not specifically trained using a dedicated 'translation task.'

This happens because the training data contains many different kinds of information and relationships. By learning to predict the next token across diverse text, the model can develop representations that are useful for tasks beyond simple next-token prediction.

Zero-Shot And Few-Shot Learning

Two techniques commonly used to take advantage of these capabilities are zero-shot learning and few-shot learning. These are also common prompting approaches when interacting with modern language models.

Zero-Shot Learning

In zero-shot learning, the model is asked to perform a task without being given examples of that task in the prompt.

For example, imagine an LLM that was trained primarily through next-token prediction and was never explicitly trained on a dedicated translation task.

You could still ask: “Translate ‘hello’ to Spanish.”

The model might respond: “Hola.”

Why can it do this? During pretraining, the model may have encountered large amounts of English and Spanish text, including examples of translations and contexts in which words from the two languages correspond to one another.

The model can therefore use the patterns it learned during pretraining to perform a task without being given an example in the prompt.

Few-Shot Learning

In few-shot learning, you provide the model with a small number of examples and then ask it to perform the same type of task on a new input.

For example, you might provide:

Happy: “This film was fantastic and moving!”

Sad: “This film was terrible and boring.”

Angry: “This film was frustrating and confusing!”

Then ask: 'Classify this review: ‘This film was wonderful and inspiring!’'

The model can infer the pattern from the examples and classify the new review accordingly.

The important point is that you did not necessarily need to explicitly explain a complete classification algorithm. The examples themselves demonstrate the task.

Why Can Next-Token Prediction Lead To These Capabilities?

The deeper reason is that language is highly interconnected.

Grammar, vocabulary, facts, concepts, reasoning patterns, and task structures are not isolated from one another. They appear together in the text on which the model is trained.

By predicting the next token billions or trillions of times across diverse examples, the model is forced to learn increasingly rich patterns about how language works and how concepts relate to one another.

So, although the training objective is simply next-token prediction, achieving good performance on that objective requires the model to capture much more than simple word sequences.

It can learn representations of:

  • Grammar and syntax

  • Vocabulary and semantics

  • Facts and relationships

  • Writing styles

  • Patterns of explanation

  • Common forms of reasoning

  • Relationships between concepts

  • Structures associated with different tasks

This does not mean that an LLM 'thinks' exactly like a human or that every capability is guaranteed to emerge simply by increasing the amount of data and parameters. Rather, the model can develop useful general-purpose representations as a consequence of learning from sufficiently large and diverse datasets.

A useful analogy is a student who has developed a strong understanding of mathematics.

Such a student may encounter a problem they have never seen before. They may still be able to solve it because they have learned underlying principles that generalize beyond the exact problems they practiced.

Similarly, an LLM can sometimes perform a task it has never explicitly been taught because the patterns and representations learned during pretraining can generalize to new situations.

At its foundation, however, the mechanism remains surprisingly simple:

The model predicts the next token, measures its error, adjusts its parameters, and repeats this process at enormous scale.

From this relatively simple training objective, surprisingly broad capabilities can emerge.

Conclusion

Large Language Models may seem complex, but their basic idea is relatively simple. They are neural networks trained on enormous amounts of text to learn patterns in language and predict what comes next.

Through pretraining, the model learns general patterns from a large and diverse collection of text. This produces a pretrained model that can then be adapted through fine-tuning for specific tasks and behaviors.

The interesting part is what happens at scale. With large amounts of data and billions of adjustable parameters, the model can learn patterns that allow it to perform tasks such as classification, summarization, translation, question answering, and even tasks it was not explicitly trained to perform.

At the foundation, however, the process remains the same:

  1. Predict the next word 
  2. Measure the error
  3. Adjust the parameters

Repeat

A simple training objective, repeated on a massive scale, can produce models with surprisingly broad language capabilities.

Comments

Popular posts from this blog

Understanding Convolutional Neural Networks

Okay... So, What Exactly Is Machine Learning Again?