Part 5: Large language models Lesson 10 of 10 in this part
Learning paradigms beyond plain training
What you will learn. Describe in plain words few-shot and in-context learning, multi-task and continual learning, meta-learning, double descent, model merging and quantisation-aware training, and work out a weight merge and a 4-bit quantisation by hand.
Before this lesson: Fine-tuning: SFT, LoRA, QLoRA and preference tuning, Bias, variance and the limits of learning. Already know these? Carry on.
In plain words
The usual recipe is: collect labelled examples for one task, train a model, test it. Real systems stretch that recipe in several directions. Few-shot and in-context learning show a large language model a handful of examples inside the prompt and it follows the pattern without any change to its weights. Multi-task learning trains one model on several tasks at once so they share what they learn. Continual learning trains on tasks one after another, and the danger is catastrophic forgetting: learning task B overwrites what the model knew about task A. Meta-learning trains a model to learn new tasks quickly from few examples. Double descent is the observation that, as models grow past the size that fits the training data exactly, test error can fall again after having risen. Two practical techniques change the model after training: model merging averages the weights of models fine-tuned from the same start, and quantisation-aware training trains a model while pretending its weights are stored with few bits, so it loses less when really quantised.
Few-shot, in context
- Examples are put in the prompt
- No weights change
Multi-task
- One model, several tasks together
- Tasks share what they learn
Continual
- Tasks arrive one after another
- Old skills can be forgotten
Meta-learning
- Learns how to learn
- Adapts from few examples
Each one changes what is learned, when, or how many examples are needed.
- InputTrain on task AThe model does well on A
- RiskTrain only on task BThe weights move to suit B
- ProblemPerformance on A fallsThe old skill was overwritten
- OutputMix a sample of A into the training on BReplay keeps both skills
Replaying a small sample of old data while learning the new task is the simplest remedy.
Post-training quantisation
- Train normally, then round the weights
- Quick, may lose some accuracy
Quantisation-aware training
- Simulate the rounding during training
- Usually loses less accuracy
Training with the rounding in the loop lets the model adapt before the weights are really rounded.
- InputStart from one base modelSame weights to begin with
- ProcessFine-tune two copies on different tasksTwo sets of weights
- ModelAverage the weights number by numberOne merged set of weights
- OutputTest the merge on both tasksIt may keep both skills, or neither well
Works best when both models start from the same base model and the tasks are compatible.
Real-world example
A team has one base language model and two fine-tuned versions, one that writes in the company's tone and one that fills in support forms. They try merging by averaging the weights, to avoid running two models. The merged model keeps the tone well but fills in forms less accurately than the specialist. The team tests it on both tasks and finds that a weighted average (two-thirds tone, one-third forms) works better. For the model that goes on a laptop, they squeeze weights from 16 bits to 4 bits and check accuracy on their own questions. They know that a new fine-tune for a third task could make the model forget the first two, so they keep a small replay set of old examples.
How it works
- In context. Put a few worked examples in the prompt, then the new case. The model continues the pattern. Nothing is stored for later.
- Multi-task and continual. For several tasks at once, mix their data. For tasks in a sequence, mix in a replay of old data, or add small task-specific adapters, to limit forgetting.
- Merging. Average the weights of two models that came from the same base. A weighted average lets you favour one skill.
- Quantisation. Pick a scale from the largest weight. Divide each weight by the scale, round to a whole number in a small range, and store the whole numbers and the scale. To use them, multiply back.
- Quantisation-aware training. Run the rounding inside the training loop so the weights settle at values that survive it.
- Meta-learning and double descent. Meta-learning methods such as MAML train the starting weights so that a few gradient steps on a new task work well. Double descent reminds you that bigger is not always worse: test error can rise and then fall again as a model gets large enough.
Python example
Python: example run The example programme behind this lesson's quoted output ran on 2026-11-19 (Python 3.12.10). 1 code block in the lesson passed a syntax check, 1 also ran on their own.
Two toy models, each trained on one of two tasks, are merged by averaging weights, and five weights are quantised to 4 bits. No large model or framework is involved. The few-shot, multi-task, continual and meta-learning paradigms are described here but not run.
"""Two ideas from beyond plain supervised learning, in numbers: merging two models by averaging their weights,
and quantising weights to 4 bits.
Run: python paradigms.py (needs numpy)
"""
import numpy as np
g = (np.arange(10) + 0.5) / 5 - 1.0 # -0.9, -0.7, ... 0.9
X = np.array([(a, b + 0.013) for a in g for b in g]) # 100 points on a grid (the small offset keeps points off the decision boundary)
task_a = (X[:, 0] > 0).astype(float) # task A: is the first number positive?
task_b = (X[:, 1] > 0).astype(float) # task B: is the second number positive?
def train(y, steps=300, lr=1.0): # logistic regression by gradient descent: weights and a bias
w = np.zeros(3)
for _ in range(steps):
p = 1 / (1 + np.exp(-(X @ w[:2] + w[2])))
w = w - lr * np.append(X.T @ (p - y) / len(y), np.mean(p - y))
return w
def accuracy(w, y):
return float(np.mean(((X @ w[:2] + w[2]) > 0) == (y == 1)))
wa, wb = train(task_a), train(task_b)
merged = (wa + wb) / 2 # model merging: average the two weight vectors
z = lambda w: [round(float(v), 2) + 0.0 for v in w] # avoids printing -0.00
print("weights: model A [%.2f %.2f %.2f] model B [%.2f %.2f %.2f]" % (*z(wa), *z(wb)))
print(" task A task B")
for name, w in (("model A", wa), ("model B", wb), ("merged (average)", merged)):
print("%-16s %6.2f %6.2f" % (name, accuracy(w, task_a), accuracy(w, task_b)))
weights = np.array([0.83, -0.41, 0.07, -0.95, 0.30]) # quantise to 4 bits: 15 levels from -7 to 7 times a scale
scale = np.abs(weights).max() / 7
q = np.round(weights / scale).astype(int)
back = q * scale
print("scale %.4f, integers %s" % (scale, q.tolist()))
print("restored weights %s" % [round(float(v), 3) for v in back])
print("largest error %.4f, average error %.4f" % (np.abs(weights - back).max(), np.abs(weights - back).mean()))weights: model A [9.00 0.00 0.00] model B [0.00 9.00 -0.11]
task A task B
model A 1.00 0.50
model B 0.50 1.00
merged (average) 0.75 0.75
scale 0.1357, integers [6, -3, 1, -7, 2]
restored weights [0.814, -0.407, 0.136, -0.95, 0.271]
largest error 0.0657, average error 0.0226Model A scores 1.00 on task A and 0.50 on task B, and model B the reverse. The merged model scores 0.75 on both. The 4-bit scale is 0.1357 and the integers are 6, -3, 1, -7 and 2. The restored weights are close to the originals, with a largest error of 0.0657 and an average error of 0.0226.
.NET (C#) example
C#: compiled and run The C# sample compiled on 2026-11-19 with .NET SDK 10.0.400 (no extra packages). It also ran to the end without error.
Merging and 4-bit quantisation written from scratch in C#, with no packages, printing the same output. Real quantisation of large models is done by tools in Python, and the quantised file is run from .NET through ONNX Runtime or a local model server.
// Merging two models by averaging weights, and quantising weights to 4 bits, from scratch.
// Same output as paradigms.py. No packages.
using System.Globalization;
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
var X = new List<double[]>();
for (int i = 0; i < 10; i++) for (int j = 0; j < 10; j++) X.Add(new[] { (i + 0.5) / 5 - 1.0, (j + 0.5) / 5 - 1.0 + 0.013 }); // 100 points on a grid (the small offset keeps points off the decision boundary)
int n = X.Count;
int[] taskA = X.Select(p => p[0] > 0 ? 1 : 0).ToArray(); // task A: is the first number positive?
int[] taskB = X.Select(p => p[1] > 0 ? 1 : 0).ToArray(); // task B: is the second number positive?
double[] Train(int[] y, int steps = 300, double lr = 1.0) // logistic regression by gradient descent
{
var w = new double[3];
for (int s = 0; s < steps; s++)
{
var g = new double[3];
for (int k = 0; k < n; k++)
{
double p = 1 / (1 + Math.Exp(-(X[k][0] * w[0] + X[k][1] * w[1] + w[2])));
g[0] += X[k][0] * (p - y[k]) / n; g[1] += X[k][1] * (p - y[k]) / n; g[2] += (p - y[k]) / n;
}
for (int d = 0; d < 3; d++) w[d] -= lr * g[d];
}
return w;
}
double Accuracy(double[] w, int[] y) => Enumerable.Range(0, n).Average(k => ((X[k][0] * w[0] + X[k][1] * w[1] + w[2] > 0) == (y[k] == 1)) ? 1.0 : 0.0);
var wa = Train(taskA); var wb = Train(taskB);
var merged = wa.Zip(wb, (a, b) => (a + b) / 2).ToArray(); // model merging: average the two weight vectors
string Z(double[] w) => string.Join(" ", w.Select(v => (Math.Round(v, 2) + 0.0).ToString("F2"))); // avoids printing -0.00
Console.WriteLine($"weights: model A [{Z(wa)}] model B [{Z(wb)}]");
Console.WriteLine(" task A task B");
foreach (var (name, w) in new[] { ("model A", wa), ("model B", wb), ("merged (average)", merged) })
Console.WriteLine($"{name,-16} {Accuracy(w, taskA),6:F2} {Accuracy(w, taskB),6:F2}");
double[] weights = { 0.83, -0.41, 0.07, -0.95, 0.30 }; // quantise to 4 bits: 15 levels from -7 to 7 times a scale
double scale = weights.Max(Math.Abs) / 7;
int[] q = weights.Select(v => (int)Math.Round(v / scale, MidpointRounding.ToEven)).ToArray();
double[] back = q.Select(v => v * scale).ToArray();
Console.WriteLine($"scale {scale:F4}, integers [{string.Join(", ", q)}]");
Console.WriteLine("restored weights [" + string.Join(", ", back.Select(v => Math.Round(v, 3).ToString("0.0##"))) + "]");
var err = weights.Zip(back, (a, b) => Math.Abs(a - b)).ToArray();
Console.WriteLine($"largest error {err.Max():F4}, average error {err.Average():F4}");Maths behind it
- Averages of weights (arithmetic)
- Rounding to a grid with a scale (arithmetic)
- Percentage change and shares (arithmetic)
- Test error against model size (statistics)
Common mistakes
- Merging models that did not start from the same base model.
- Trusting a merged or quantised model without testing it on the real tasks.
- Fine-tuning on a new task and never re-testing the old ones.
- Treating in-context examples as training. They help only within that prompt.
- Assuming a bigger model will always overfit more.
Quick check
What is catastrophic forgetting?
Show answer
A model trained on a new task loses what it had learned about an earlier one, because its weights moved to suit the new task.
Why does merging work best with models from the same base?
Show answer
Their weights start from the same place, so averaging them stays in a region where the model still works.
How much smaller are weights stored with 4 bits instead of 32?
Show answer
Eight times smaller, because 32 ÷ 4 = 8.
What does in-context learning change in the model?
Show answer
Nothing. The examples in the prompt guide the answer for that prompt only.
Completion
Evidence of completion. A table of two models and their merge scored on two tasks, and a 4-bit quantisation of five weights with the largest and average error, with a sentence on when each technique is worth using.
Saved in this browser only. Reading and exercise are ticked separately because they are different achievements. A tick records your own work; it is not a test result. See all progress.