What is Machine Learning?
The foundation of everything – understand before you train
Machine learning (ML) is a branch of artificial intelligence that enables computers to learn from experience. Instead of giving a computer exact rules, you show it example data – and it finds the patterns itself. The result is called a model.
Classical Programming vs. Machine Learning
ML: data + answers → rules (the model)
With ML, the computer figures out the rules itself.
The three fundamental learning paradigms
Supervised Learning
You give the model data WITH correct answers (labels). It learns the mapping from input to output.
Example: Email → "spam" or "not spam"
Use case: Classification, regression, NLP
Unsupervised Learning
The model only receives data WITHOUT labels. It searches on its own for patterns, clusters, and structures.
Example: Grouping customers by purchasing behavior
Use case: Clustering, dimensionality reduction
Reinforcement Learning
The model learns through reward and punishment within an environment. It optimizes its strategy.
Example: Playing chess, RLHF in ChatGPT
Use case: Robotics, games, RLHF
Key ML terms – the vocabulary
The "intelligence" – a mathematical function with millions of learned parameters that maps inputs to outputs.
The model's internal numbers. They are optimized during training and represent the model's "knowledge".
The process by which the model iteratively improves its parameters using example data.
Applying the fully trained model to new, unseen data.
The collection of training examples. Quality and quantity are both crucial.
One complete pass through ALL training data.
A small subset of the training data processed simultaneously.
A numeric value indicating how "wrong" the model currently is. Lower is better.
The mathematical direction in which parameters need to change to reduce the loss.
The model "memorizes" training data instead of generalizing – poor performance on new data.
The model is too simple or trained too briefly – poor performance on both training AND test data.
The ability to perform well on unseen data. The actual goal of training.
Why fine-tuning instead of training from scratch?
Training an LLM like GPT or LLaMA from scratch costs millions of euros and requires thousands of GPUs over weeks. With fine-tuning, you take an already pretrained model – one that already knows language, facts, and logic – and specialize it for your task. In hours instead of months, on your own GPU.
Training from scratch (pretraining)
- ⏱ Weeks to months of compute time
- 💰 Hundreds of thousands to millions of euros
- 📦 Billions of training samples needed
- 🖥 Hundreds to thousands of GPUs simultaneously
- 🧑💻 Realistic only for large organizations
Fine-tuning (with FrameTrain)
- ⏱ Minutes to a few hours
- 💰 Practically free (your own hardware)
- 📦 A few hundred to a thousand examples are enough
- 🖥 1 GPU – your own computer
- 🧑💻 Achievable for anyone
Transfer learning: the foundation of fine-tuning
Fine-tuning is based on transfer learning: the prior knowledge of a large pretrained model (grammar, facts, logic, code) is transferred to a new, specific task. Only a fraction of the parameters need to be adapted.
Base model
(7B parameters)
Specialized
model
The base model already "knows" language. Fine-tuning teaches it your specific use case.
Neural Networks
Understanding how modern AI models are built
An artificial neural network (ANN) is loosely inspired by the human brain. It consists of layers of neurons connected to one another. Each connection has a weight – the knowledge learned during training.
Structure of a simple neural network
How a single neuron works
Each neuron receives inputs from all neurons in the previous layer, multiplies them by weights, sums everything up, and applies an activation function:
Neuron computation (simplified): output = activation(w₁·x₁ + w₂·x₂ + ... + wₙ·xₙ + b) w₁...wₙ = weights (learned during training) x₁...xₙ = inputs from the previous layer b = bias (offset value) activation = non-linear function (ReLU, sigmoid, etc.) Example with ReLU: output = max(0, w₁·x₁ + w₂·x₂ + b) → everything < 0 becomes 0 → the network learns "fires / doesn't fire"
Activation functions
ReLU (Rectified Linear Unit)
f(x) = max(0, x)The simplest and most common activation. Everything below 0 → 0, otherwise linear. Fast, little vanishing gradient.
Used in: Standard in hidden layers of classic networks
GeLU (Gaussian Error Linear Unit)
f(x) ≈ x · Φ(x)A smoother version of ReLU, used by BERT and GPT. Slightly better for transformer architectures.
Used in: Transformer feed-forward layers (BERT, GPT, LLaMA)
SiLU / Swish
f(x) = x · σ(x)Self-gated, smooth and differentiable everywhere. Used in newer architectures like LLaMA 2/3.
Used in: LLaMA, Mistral, newer decoder models
Softmax
f(xᵢ) = e^xᵢ / Σe^xⱼConverts raw scores into probabilities that sum to 1. Used only in the output layer for classification.
Used in: Output layer for classification and LM head
Key layer types in modern networks
Dense / Fully Connected (Linear)
Every neuron connected to every neuron in the previous layer. Universal for classification and regression. In transformers: the FFN layers.
Embedding Layer
Converts discrete tokens (words/characters) into continuous vectors. Fundamental for language models – the "representation" of each word.
Multi-Head Attention
The core of transformers. Several parallel attention heads compute relationships between all token pairs in the sequence.
Layer Normalization
Normalizes activations within a layer. Essential for stable transformer training. Prevents exploding/vanishing gradient issues.
Dropout
Randomly disables neurons during training. Forces redundancy, acting as a strong regularizer against overfitting.
Residual / Skip Connections
A direct connection from the input to the output of a block (x + f(x)). Enables very deep networks without gradient degradation.
Depth vs. width: why depth is better
A deep network (many layers) learns hierarchical representations: lower layers learn simple patterns (edges, syllables), middle layers combine them (shapes, words), upper layers recognize complex concepts (faces, meanings). Width alone isn't enough.
Transformers & LLMs
The architecture behind GPT, LLaMA, BERT, and friends
Since the paper "Attention Is All You Need" (Vaswani et al., 2017), the transformer architecture has dominated the AI world. All modern LLMs – GPT-4, LLaMA, Mistral, BERT – are based on it. The key: the self-attention mechanism.
Revolutionary idea: only attention, no recurrence
Self-attention: the core idea
Self-attention lets every token in the text "look at" every other token to understand its context. Three projections (query, key, value) determine how strongly each token attends to others:
For each token i: Query_i = Token_i × W_Q (what am I looking for?) Key_j = Token_j × W_K (what do I offer?) Value_j = Token_j × W_V (what is my content?) Attention-Score(i,j) = softmax(Query_i · Key_j / √d_k) Output_i = Σ_j [Attention-Score(i,j) × Value_j] → Each token gets an output that is a weighted combination of all other tokens.
Multi-head attention
Instead of a single attention computation, h parallel heads are used. Each head learns to attend to a different type of relationship: head 1 → syntactic dependencies, head 2 → coreference, head 3 → semantic similarity, etc.
Anatomy of a transformer block
Modern LLMs at a glance
BERT / RoBERTa
EncoderClassification, NER, question answering
GPT-2 / GPT-J
DecoderText generation, completion
LLaMA 2/3
DecoderChat, instruction following
Mistral 7B
DecoderEfficient, strong (GQA, SWA)
T5 / Flan-T5
Enc-DecTranslation, summarization
Phi-3 / Gemma 2B
DecoderSmall, efficient models
VRAM requirements for LLM fine-tuning
Memory requirements for full fine-tuning (optimistic estimate with mixed precision):
The consequence: LoRA & QLoRA are game-changers
With QLoRA: 7B fine-tuning = ~4 GB VRAM (an RTX 3070 is enough!).
How AI "learns"
Fully understanding backpropagation and gradient descent
Behind every training run are two fundamental algorithms: backpropagation (computes how wrong the model is and why) and gradient descent (adjusts the parameters in the right direction).
The complete learning cycle
Forward Pass
Training data flows forward through all layers. Each layer transforms the input until a prediction emerges.
Compute the loss
The prediction is compared to the correct label. A loss function quantifies the error as a single number. The larger it is, the worse.
Backward Pass (Backpropagation)
Using the chain rule of calculus, it's computed how much each individual parameter contributed to the total error. Result: a gradient for every parameter.
Update the weights
The optimizer shifts each parameter against the gradient: W_new = W_old - LR × gradient. The step size is determined by the learning rate.
Reset gradients
Gradients are reset to 0. Important! Otherwise gradients accumulate across batches (unless gradient accumulation is intentional).
Repeat
This cycle repeats for every batch in every epoch. After thousands to millions of iterations, the model has "learned" the data.
Gradient descent: step by step toward the minimum
The learning rate: the slider of learning
The learning rate (LR) determines how large each step downhill is. Too large: you overshoot the minimum and bounce around wildly. Too small: you almost never get there.
Weight update: W_new = W_old - α × ∂L/∂W α (alpha) = learning rate (e.g. 0.00002 = 2e-5) ∂L/∂W = gradient (derivative of the loss w.r.t. W) Example: W_old = 0.5, gradient = 0.3, LR = 0.01 W_new = 0.5 - 0.01 × 0.3 = 0.5 - 0.003 = 0.497
Stochastic Gradient Descent (SGD) vs. Batch GD
Full Batch GD
Batch: All data
Speed: 🐌 Slow
Quality: 🎯 Stable
Memory: 💾 High
Mini-Batch GD ⭐
Batch: Small batches (8–64)
Speed: ⚡ Fast
Quality: 🎯 Good
Memory: 💾 Low
Stochastic GD
Batch: 1 example
Speed: ⚡ Very fast
Quality: 📉 Noisy
Memory: 💾 Minimal
Why local minima aren't a problem (for large LLMs)
In low-dimensional spaces (2D or 3D), there are many local minima you can get stuck in. In high-dimensional parameter spaces with billions of parameters, it's fundamentally different: almost all critical points are saddle points or flat regions, not true local minima. Moreover, all "good" local minima are about as good as the global minimum.
The key takeaway
Vanishing & exploding gradients
In very deep networks, gradients can vanish during the backward pass (→ weights barely change, training stagnates) or explode (→ weights become huge, training breaks down). Modern architectures address this through:
Against vanishing:
- ✓ Residual connections (skip connections)
- ✓ Layer normalization
- ✓ Better activations (ReLU, GeLU)
- ✓ Careful initialization
Against exploding:
- ✓ Gradient clipping (max_grad_norm = 1.0)
- ✓ Lower learning rate
- ✓ LR warmup at the start
- ✓ Adaptive optimizers (Adam, AdamW)