512kg / dima

Understanding artificial intelligence: the technical foundations

Dima ·

1. Machine learning

Traditional software is deterministic: given the same input and identical conditions, it produces the same result. The programmer explicitly defines the instructions and rules that govern its behaviour.

AI models work differently. Instead of programming every possible response by hand, they are trained on data to learn patterns and statistical relationships. That is why their behaviour is usually probabilistic: the same input can produce different results, depending on the model’s configuration and the generation parameters.

Models such as GPT, Claude and Gemini learn during training from large datasets. Afterwards they receive inputs through an API or a conversational interface and generate responses as text, images, audio or other formats, depending on their capabilities.

Machine learning is the branch of artificial intelligence that makes it possible to build systems that learn from data without all of their rules being specified by hand.

2. Neural networks

Neural networks are the basis of many modern AI systems. They are made of interconnected units of computation that transform information through mathematical operations and whose parameters are adjusted during training.

A neural network is usually organised in three components:

  1. Input layer: receives the data and converts it into numerical representations the network can process.
  2. Hidden layers: progressively transform the information through mathematical operations, weights, biases and activation functions. The weights determine how much each connection influences the result.
  3. Output layer: produces the values used to build the final prediction. In a language model, these values can represent scores for the possible next tokens.

In essence, every layer computes

h = f(W·x + b)

It multiplies the input x by a weight matrix W, adds a bias vector b and applies a non-linear activation function f, such as ReLU, which is max(0, z). Without that non-linearity, stacking layers would be equivalent to a single linear transformation.

For example, if the model receives the text “San”, it can assign a high probability to “Francisco” and a much lower one to a word with no contextual relation.

Fig. 1 — Forward pass through a tiny network. Three inputs, four hidden units with ReLU, three outputs and a softmax. The weights are hand-picked toy values, not a trained model, but every number on screen is computed from them. Gold lines are positive weights, blue lines negative; thickness is magnitude.

These predictions do not come from rules written by hand for each sentence. They are the result of the patterns the network learned during training.

Ultimately, a neural network is a mathematical system made of linear algebra operations, non-linear functions and parameters optimised with methods from calculus and statistics. Libraries such as PyTorch abstract much of this complexity and make it possible to build and train networks without implementing every operation from scratch.

3. Transformers

The Transformer architecture, introduced in the research paper Attention Is All You Need (Vaswani et al., 2017), was a fundamental advance in the development of language models.

Before processing text, models split it into units called tokens. These tokens are converted into numerical vectors, known as embeddings, which give a mathematical representation of linguistic information.

Embeddings make it possible to represent relationships between words in high-dimensional vector spaces. For example, certain semantic relationships between “king” and “queen” can resemble those between “man” and “woman”. The classic example is arithmetic: vector(“king”) − vector(“man”) + vector(“woman”) lands close to vector(“queen”). These relationships are not universal rules: they depend on how the vectors were learned and represented.

Fig. 2 — Vector arithmetic with embeddings. Toy vectors in two dimensions, placed by hand. The offset from “man” to “king” is added to “woman”, and the word nearest to the result is “queen”. Real embeddings have hundreds or thousands of dimensions, and the analogy only holds approximately.

The central feature of Transformers is the attention mechanism, especially self-attention.

This mechanism lets each token incorporate contextual information from other tokens in the sequence. Instead of interpreting each word in isolation, the model can weigh which parts of the context are most relevant for processing it.

Attention(Q, K, V) = softmax(Q·Kᵀ / √dₖ) · V

Each token emits a query that is compared with the keys of all tokens. The resulting scores, normalised with a softmax, weight the values that are added up to form its new representation.

Consider the sentence:

“The cat lay down on the rug because it was tired.”

To interpret “it” correctly, the model needs to relate that word to the subject of the sentence. Self-attention makes these contextual connections possible through calculations that determine the relative relevance of the different tokens.

Fig. 3 — Self-attention for one token. The query vector of “it” is compared with the key vector of every token (dot product, scaled by √d). A softmax turns the scores into weights α that add up to 1. Toy example: one head, hand-written 4-dimensional vectors and whole words as tokens. In a trained Transformer the vectors are learned and there are many heads and layers.

In addition, Transformers can process multiple positions of a sequence in parallel during training. This makes effective use of GPU parallelism and makes it feasible to train models with very large numbers of parameters and amounts of data.

Because attention on its own does not distinguish the order of the tokens, the model adds position information to each embedding (positional encoding).

4. Tokenisation

Tokenisation converts text into units a model can process numerically.

A token can correspond to a whole word, part of a word, a punctuation mark or a sequence of characters. The exact split depends on the tokeniser.

For example, a rare word may be split into several tokens, while a common one may be represented by a single token.

The most widespread tokenisers, such as BPE (byte-pair encoding), build their vocabulary by repeatedly merging the most frequent pairs of symbols in a corpus.

Fig. 4 — Learning a tokeniser with byte-pair encoding (BPE). Start from single characters, then repeatedly merge the most frequent adjacent pair into a new symbol. “_” marks the end of a word and “×” is how often each word appears in this four-word toy corpus. Ties go to the pair found first. Real tokenisers run tens of thousands of merges over very large corpora.

Each token is associated with a numerical identifier that lets the model process it. These identifiers are then turned into embeddings that feed the neural network.

Tokenisation affects both the efficiency and the cost of language models. Inputs and outputs are usually measured in tokens, and the number of units processed influences resource consumption and context limits.

As a rough guide, with common tokenisers one token is about four characters of English, around three quarters of a word; in other languages, Spanish included, the ratio is different.

So a token is not necessarily a word, and the number of tokens in a text depends on the language, the content and the tokeniser.

5. Pretraining

Pretraining is the phase in which the model learns general patterns from large datasets.

In autoregressive language models, the usual objective is to predict the next token from the previous ones. During training, the model receives sequences of text and computes a probability distribution over the possible tokens.

The difference between its predictions and the expected tokens is quantified by a loss function. The backpropagation algorithm computes how the model’s parameters contribute to the error, and an optimiser uses that information to update them.

In practice the loss is cross-entropy: for each position, L = −log p(correct token). If the model assigns a probability of 0.25 to the expected token, the loss is −ln 0.25 ≈ 1.39; if it assigns 0.9, it drops to 0.105. The update follows the gradient:

θ ← θ − η·∇L

where θ are the parameters and η is the learning rate.

This process is repeated over huge numbers of examples.

In simplified form, the training loop is:

  1. The model receives a sequence of tokens.
  2. It computes its predictions.
  3. The prediction is compared with the expected result.
  4. The loss is computed.
  5. The gradients are obtained through backpropagation.
  6. The optimiser updates the parameters.
  7. The process is repeated with new examples.
Fig. 5 — The training loop, running for real. A next-token model with a single weight table (a bigram model: it only sees the previous token) is trained by gradient descent on a four-sentence corpus, with whole words as tokens. The first two steps are shown phase by phase; the rest run at full speed. The dashed line is the lowest loss this corpus allows.

With enough data, computing capacity and a suitable architecture, the model learns regularities of language, contextual relationships and patterns present in the training data.

However, predicting the next token correctly does not guarantee that every answer is true, that the model understands the world the way a person does, or that it can solve any task.

6. Fine-tuning

Pretraining provides general capabilities, but it does not necessarily turn the model into an assistant that follows instructions reliably.

Fine-tuning adapts a previously trained model using additional data selected to improve its behaviour on specific tasks.

For example, a model intended for customer support can be trained on representative conversations to learn to answer questions, follow procedures and keep a consistent format.

During supervised fine-tuning, the model receives examples of inputs and desired responses. Its parameters are updated to increase the probability of generating the expected responses.

Data quality is fundamental. A small but well-selected training set can be more useful for a specific task than a larger collection of inconsistent or irrelevant examples.

Fine-tuning does not mean training the model from scratch. Usually one starts from a pretrained model and modifies its parameters to specialise it. It does not require changing all of them either: techniques such as LoRA keep the original weights frozen and train a small number of additional parameters, which makes adaptation cheaper.

7. Reinforcement learning from human feedback (RLHF)

Reinforcement learning from human feedback (RLHF) is a technique used to steer a model’s behaviour towards the responses people consider preferable.

A classic RLHF process can include the following stages:

  1. The model generates several possible responses to the same instruction.
  2. Human evaluators compare the responses and rank them by criteria such as usefulness, accuracy and adherence to instructions.
  3. A reward model is trained to estimate which responses best match those preferences.
  4. That reward signal is used to optimise the original model through reinforcement learning (in the classic formulation, with the PPO algorithm).

The goal is to improve the model’s behaviour, not simply to increase its ability to predict text.

For example, in a customer-support system, feedback can favour responses that solve problems correctly, avoid unfounded claims and follow company policy.

Even so, the result depends on the quality of the evaluations, the criteria set and the optimisation method. Human preferences can be inconsistent and do not by themselves guarantee that the responses are true.

RLHF is also only one of several possible alignment techniques. Modern systems can use other optimisation methods based on human or model-generated preferences, such as DPO (Direct Preference Optimization), which does without an explicit reward model.

8. Inference

Once trained and adapted, the model can be used for inference: applying the learned parameters to new inputs to produce predictions or generate content.

In an autoregressive language model, the basic process is to compute a probability distribution for the next token, select one according to the generation strategy and append it to the sequence. The model repeats this procedure until it produces a complete response or reaches a set limit.

Selection does not always mean choosing the most probable token. It can use sampling techniques and parameters such as temperature to modify the distribution of the responses.

The temperature T divides the scores before the softmax:

pᵢ = exp(zᵢ / T) / Σⱼ exp(zⱼ / T)

With T < 1 the distribution concentrates on the most probable tokens; with T > 1 it flattens. Other techniques, such as top-k and top-p (nucleus sampling), restrict sampling to the most probable candidates.

Fig. 6 — Inference, one token at a time. The model trained in Fig. 5 generates text: it computes the distribution for the next token, samples one, appends it to the context and repeats until it produces “.” or reaches 10 tokens. Move the temperature: low values almost always pick the most probable token; high values flatten the distribution and let unlikely tokens through.

Inference can also include multimodal inputs. Depending on its architecture, a model can process text, images or audio and generate one or several output modalities.

Some models incorporate training and inference techniques aimed at solving complex problems through intermediate reasoning steps. However, generating a step-by-step explanation does not guarantee that the reasoning is correct.

In production, inference brings practical considerations such as latency, memory consumption, computational cost, concurrency and context length. Quantisation (representing the weights with 8 or 4 bits instead of 16 or 32), batching and other optimisation techniques reduce costs or improve performance, although they can involve trade-offs in accuracy.

9. Model evaluation

Evaluation measures whether a model meets the goals it was developed for.

No single metric adequately describes all the capabilities of an AI system. Evaluation has to fit the type of task, the risks and the real conditions of use.

Common methods include:

  • Benchmarks: standardised sets of tests that allow the performance of different models to be compared.
  • Automatic evaluation: metrics or programmatic checks that verify specific properties of the responses.
  • Human evaluation: review of the usefulness, accuracy, clarity and suitability of the results.
  • Safety evaluation: tests designed to detect unwanted behaviour, vulnerabilities and dangerous responses.
  • Evaluation in production: analysis of the system’s behaviour with real users and inputs.

A high benchmark score does not prove that the model is better at every task. Results can depend on the data used, the design of the tests and how closely they resemble real conditions of use.

It is also important to avoid contamination of the evaluation data: if a model saw the answers to a test during training, its score may overestimate its ability to generalise.

A solid evaluation therefore combines independent tests, appropriate metrics and error analysis.

10. Knowledge distillation

Distillation is a technique for transferring part of the behaviour of a large model, called the teacher, to a smaller one, called the student.

Instead of training the small model only on correct answers, the process can use the probability distributions produced by the large model. These distributions carry information about how the different possible answers relate to each other.

In the original formulation (Hinton et al., 2015), both distributions are softened with a high temperature and the student minimises its divergence from the teacher’s.

The student model is trained to approximate the teacher’s behaviour, which can allow it to deliver competitive results at a lower inference cost.

For example, a company can use a large model to generate training data and then train a smaller model specialised in one specific task.

Distillation does not guarantee that the small model keeps all the capabilities of the original. The result depends on the method, the data, the architecture and the tasks being optimised.

Its main advantage is that it can reduce memory, latency and compute requirements, making deployment easier in environments with limited resources.

Conclusion

Modern AI systems are built by combining neural architectures, large datasets, mathematical optimisation and procedures for training and evaluation.

Pretraining lets the model learn general patterns; fine-tuning adapts it to tasks and instructions; alignment techniques steer its responses; and inference applies what was learned to new inputs.

From these foundations, developers can choose models, adapt them, evaluate their limitations and deploy them in real applications.

Understanding these mechanisms makes it possible to work with artificial intelligence without treating it as a black box: its results come from concrete mathematical operations and learning processes, with capabilities, costs and limitations that can be analysed and measured.

Sources

  1. Vaswani et al. (2017). Attention Is All You Need.
  2. Mikolov, Yih & Zweig (2013). Linguistic Regularities in Continuous Space Word Representations.
  3. Sennrich, Haddow & Birch (2015). Neural Machine Translation of Rare Words with Subword Units.
  4. OpenAI Help Center. Understanding and counting tokens.
  5. Hu et al. (2021). LoRA: Low-Rank Adaptation of Large Language Models.
  6. Ouyang et al. (2022). Training language models to follow instructions with human feedback.
  7. Schulman et al. (2017). Proximal Policy Optimization Algorithms.
  8. Rafailov et al. (2023). Direct Preference Optimization: Your Language Model is Secretly a Reward Model.
  9. Holtzman et al. (2019). The Curious Case of Neural Text Degeneration.
  10. Hinton, Vinyals & Dean (2015). Distilling the Knowledge in a Neural Network.