Search

Search papers and lessons

Meta-Learning

Level 4 · 11 min read

Meta-learning trains a model on many tasks so that a few gradient steps on a new task, with few examples, give good results on it.

On this page

  • Colour = topic mix
  • Calculus
  • Discrete math
  • Learning basics
  • Probability
  • Linear algebra
  • Losses & regularization
  • ReadReady to readNot yet

TL;DR

Meta-learning trains a model on many different tasks so that it becomes easy to adapt. Ordinary training learns one answer for one task. Meta-learning instead learns a starting point from which a few gradient steps, on a few labelled examples, give good results on a task the model has never seen. The learning happens at two levels: an inner loop adapts to one task, and an outer loop improves the starting point across tasks.

Builds on: Episodic Training · Leads to: MAML, Few-Shot Learning

Where it's used

Few-shot image classification. A model is shown a new set of object categories with only a handful of labelled photos per category. Meta-learned starting points were tested on image benchmarks such as miniImageNet, where the task is to classify new classes from a few examples each.

Robot control. A robot arm trained on many objects should pick up a new object after a few practice attempts, not after thousands. The MAML paper tests this idea in simulation: policies trained with policy gradient methods adapt to new goals and to simulated robots such as a cheetah and an ant after a few gradient steps.

Learned optimisers. Some systems replace the hand-written update rule of Gradient Descent with a rule learned across many training runs, so the optimiser itself adapts to the kind of problem it is given.

How it works

Take a toy family of tasks. The model has one number θ\theta. Each task ii has a target cic_i, and its loss is Li(θ)=(θ−ci)2L_i(\theta) = (\theta - c_i)^2. The gradient of that loss is 2(θ−ci)2(\theta - c_i). We use two tasks, with targets c1=2c_1 = 2 and c2=6c_2 = 6, and start with θ=0\theta = 0. The inner step size is α=0.25\alpha = 0.25, and the outer step size is β=0.1\beta = 0.1.

  1. Adapt to task 1 with one inner step. The gradient at θ=0\theta = 0 is 2(0−2)=−42(0 - 2) = -4. The adapted value is θ1′=0−0.25⋅(−4)=1\theta_1' = 0 - 0.25 \cdot (-4) = 1. Check: 1 is halfway between the start 0 and the target 2.

  2. Adapt to task 2 with one inner step. The gradient is 2(0−6)=−122(0 - 6) = -12, so θ2′=0−0.25⋅(−12)=3\theta_2' = 0 - 0.25 \cdot (-12) = 3. Check: 3 is halfway between 0 and 6, as expected for this loss.

  3. Score the adapted models on their own tasks. Task 1 gives (1−2)2=1(1 - 2)^2 = 1, and task 2 gives (3−6)2=9(3 - 6)^2 = 9. The meta-loss, which is the sum over tasks, is 1+9=101 + 9 = 10. Check: the meta-loss is large because one starting value cannot be close to both targets.

  4. Update the starting point across tasks. Each adapted value moves by half the distance from θ\theta to its target. The meta-gradient for task ii works out to θi′−ci\theta_i' - c_i, which is 1−2=−11 - 2 = -1 for task 1 and 3−6=−33 - 6 = -3 for task 2. Their sum is −4-4. One outer step gives θ←0−0.1⋅(−4)=0.4\theta \leftarrow 0 - 0.1 \cdot (-4) = 0.4. Check: the starting point moved from 0 toward 4, the average of the two targets.

flowchart TD
  A["Starting point theta"] --> B["Sample a batch of tasks"]
  B --> C["Inner loop: a few gradient steps on each task's support examples"]
  C --> D["Score each adapted model on that task's query examples"]
  D --> E["Outer loop: change theta to lower the total query loss"]
  E --> A

The math

Definition: Meta-learning

Let p(T)p(\mathcal{T}) be a distribution over tasks. Each task Ti\mathcal{T}_i has a loss LTi\mathcal{L}_{\mathcal{T}_i}. The model has parameters θ\theta, step size α\alpha for adaptation, and step size β\beta for meta-training. Meta-learning chooses θ\theta to minimise the expected loss of the adapted parameters over tasks:

min⁡θ  ETi∼p(T)[LTi(θi′)],θi′=θ−α∇θLTi(θ)\min_{\theta} \; \mathbb{E}_{\mathcal{T}_i \sim p(\mathcal{T})} \left[ \mathcal{L}_{\mathcal{T}_i}\left(\theta_i'\right) \right], \qquad \theta_i' = \theta - \alpha \nabla_{\theta} \mathcal{L}_{\mathcal{T}_i}(\theta)

Here θi′\theta_i' is the parameter vector after one inner step on task ii. The outer variable is θ\theta, and the objective is evaluated at θi′\theta_i'.

Step 1 in symbols: the tasks

Each task Ti\mathcal{T}_i is a small problem drawn from p(T)p(\mathcal{T}), such as a classification problem with a few classes. Each task has two sets of examples. The support set is used for the inner step, and the query set is used to score the adapted model. One sampled task with its two sets is called an episode, and a group of episodes used for one outer update is a meta-batch. In the toy example, the task is the target cic_i, and LTi(θ)=(θ−ci)2\mathcal{L}_{\mathcal{T}_i}(\theta) = (\theta - c_i)^2.

Step 2 in symbols: the inner update

θi′=θ−α∇θLTi(θ)\theta_i' = \theta - \alpha \nabla_{\theta} \mathcal{L}_{\mathcal{T}_i}(\theta)

In the toy example, ∇θLTi(θ)=2(θ−ci)\nabla_{\theta} \mathcal{L}_{\mathcal{T}_i}(\theta) = 2(\theta - c_i), so θi′=θ−2α(θ−ci)\theta_i' = \theta - 2\alpha(\theta - c_i). With α=0.25\alpha = 0.25 this is θi′=0.5 θ+0.5 ci\theta_i' = 0.5\,\theta + 0.5\,c_i, the midpoint between the start and the target.

Step 3 in symbols: the meta-objective

The meta-loss sums the loss of the adapted parameters over the tasks in a batch:

J(θ)=∑iLTi(θi′)=∑iLTi(θ−α∇θLTi(θ))\mathcal{J}(\theta) = \sum_i \mathcal{L}_{\mathcal{T}_i}\left(\theta_i'\right) = \sum_i \mathcal{L}_{\mathcal{T}_i}\left(\theta - \alpha \nabla_{\theta} \mathcal{L}_{\mathcal{T}_i}(\theta)\right)

This is the same objective as the displayed definition, with the inner update substituted in. In the toy example, J(θ)=∑i(0.5 θ−0.5 ci)2\mathcal{J}(\theta) = \sum_i \left(0.5\,\theta - 0.5\,c_i\right)^2, which is smallest at θ\theta equal to the average of the cic_i.

Step 4 in symbols: the meta-gradient

The outer gradient differentiates through the inner update with the Chain Rule:

∇θLTi(θi′)=(I−α∇θ2LTi(θ))∇θLTi(θi′)\nabla_{\theta} \mathcal{L}_{\mathcal{T}_i}\left(\theta_i'\right) = \left(I - \alpha \nabla_{\theta}^{2} \mathcal{L}_{\mathcal{T}_i}(\theta)\right) \nabla_{\theta} \mathcal{L}_{\mathcal{T}_i}\left(\theta_i'\right)

Here ∇θ2\nabla_{\theta}^{2} is the Hessian of the task loss. The Hessian term is the second derivative that the inner step creates. In the toy example it is 22, so the factor is 1−0.25⋅2=0.51 - 0.25 \cdot 2 = 0.5, which matches dθi′/dθ=0.5d\theta_i'/d\theta = 0.5. The MAML paper notes that computing this term needs Hessian-vector products. Its first-order approximation drops the term, and on MiniImagenet that version performs nearly the same as the full method.

Step 5 in symbols: the outer update

θ←θ−β∇θJ(θ)\theta \leftarrow \theta - \beta \nabla_{\theta} \mathcal{J}(\theta)

In the toy example the meta-gradient summed over tasks is −4-4, so θ\theta moves by 0.1⋅4=0.40.1 \cdot 4 = 0.4 toward the average target.

Worked examples

Example
1. One task, one inner step

A task has target c=2c = 2. Start with θ=0\theta = 0 and use α=0.5\alpha = 0.5.

  1. Gradient of the loss at θ=0\theta = 0: 2(0−2)=−42(0 - 2) = -4.
  2. Inner step: θ′=0−0.5⋅(−4)=2\theta' = 0 - 0.5 \cdot (-4) = 2.
  3. Loss after the step: (2−2)2=0(2 - 2)^2 = 0.

Interpretation: with this step size the single gradient step lands exactly on the target, which is special to this quadratic loss. For a real network one step rarely reaches the answer, which is why meta-learning trains the starting point so that the few steps it does take are useful.

Example
2. A full meta-update with two tasks

This is the walkthrough at a larger step, with a second starting point to compare.

  1. Two tasks, targets c1=2c_1 = 2 and c2=6c_2 = 6. Start at θ=0\theta = 0, with α=0.25\alpha = 0.25 and β=0.1\beta = 0.1.
  2. Inner results: θ1′=1\theta_1' = 1 and θ2′=3\theta_2' = 3.
  3. Query losses after adaptation: (1−2)2=1(1 - 2)^2 = 1 and (3−6)2=9(3 - 6)^2 = 9. Total 10.
  4. Meta-gradients: θ1′−c1=−1\theta_1' - c_1 = -1 and θ2′−c2=−3\theta_2' - c_2 = -3. Total −4-4.
  5. Outer update: 0−0.1⋅(−4)=0.40 - 0.1 \cdot (-4) = 0.4.
  6. Now try a starting point of θ=4\theta = 4. The inner results are θ1′=0.5⋅4+0.5⋅2=3\theta_1' = 0.5 \cdot 4 + 0.5 \cdot 2 = 3 and θ2′=0.5⋅4+0.5⋅6=5\theta_2' = 0.5 \cdot 4 + 0.5 \cdot 6 = 5. The query losses are (3−2)2=1(3 - 2)^2 = 1 and (5−6)2=1(5 - 6)^2 = 1, so the total is 2, down from 10.

Interpretation: a starting point near the middle of the tasks lets one inner step reach both targets. The outer loop finds that middle by looking at how well the adapted models do, not by fitting either task directly.

Example
3. Realistic: few-shot image classification

A common setup is 5-way 5-shot. Each episode has 5 classes. For each class the model gets 5 labelled support images, and it is then tested on 15 query images per class.

  1. Support images per episode: 5 classes×5 images=255 \text{ classes} \times 5 \text{ images} = 25. The inner loop adapts to these 25 images only.
  2. Query images per episode: 5 classes×15 images=755 \text{ classes} \times 15 \text{ images} = 75. The query loss, computed after adaptation, supplies the meta-gradient.
  3. Images per episode in total: 25+75=10025 + 75 = 100.

Try it: a meta-batch holds 4 episodes, each set up as above. How many images are used in one outer update, and how many of them are support images?

Answer: each episode has 100 images, so the meta-batch uses 4×100=4004 \times 100 = 400 images. Of these, 4×25=1004 \times 25 = 100 are support images, and the other 300 are query images.

Common mistakes and tradeoffs

It is tempting to think that meta-learning is the same as transfer learning. In fact transfer learning reuses one trained model on one new task, while meta-learning trains explicitly for fast adaptation across many tasks. The two can be combined, and a meta-learned starting point is one way to make transfer work with very little data.

It is tempting to think that the learning happens when the model sees the new task. In fact the outer loop does the learning, across many tasks, and the inner loop only adapts with a few steps. The new task contributes the adaptation, not the lesson.

It is tempting to think that a meta-learner just overfits its training tasks. In fact the test is on held-out tasks it has never seen, which is why the task distribution must be split so that the meta-learner is judged on tasks it did not train on. Without that split, the scores look good and mean little.

When to use it: when you have many related tasks and only a few examples for each new one, as in few-shot classification and fast robot adaptation. When not to use it: when there is one task with plenty of data, where ordinary training is cheaper and simpler. Full meta-learning is also costly, because each meta-step needs second derivatives or many inner steps. Alternatives: fine-tuning a pretrained model (Transfer Learning), metric-based methods that compare a new example with class averages such as prototypical networks, and In-Context Learning in large language models.

History

Researchers used the phrase learning to learn in the 1990s, for example in a 1998 book edited by Sebastian Thrun and Lorien Pratt. The idea was that a learner could gain experience across tasks, but the methods of the time were hard to apply to deep networks.

In 2017 Chelsea Finn, Pieter Abbeel and Sergey Levine published MAML. Its key move was to train the starting parameters so that a few ordinary gradient steps on a new task work well, and to differentiate through those steps during training. The paper states that the method is model-agnostic in the sense that any model trained with gradient descent can use it.

The method caught on because it needs no special architecture. That made it easy to try on image classification, regression and reinforcement learning, and the few-shot benchmarks gave a common way to compare methods.

Recap

  • Meta-learning trains on many tasks so that a model can adapt to a new task with a few examples.
  • The inner loop takes a few gradient steps on one task. The outer loop changes the starting point to lower the loss after those steps.
  • The meta-gradient differentiates through the inner update, which brings in a second derivative of the task loss.
  • Meta-learning is judged on held-out tasks, and it differs from transfer learning in how it is trained.

Read next: Few-Shot Learning for the setting it is most often used in, and MAML for the method in full.

Check yourself

QuestionWhat does the inner loop do, and what does the outer loop change?Show answer

The inner loop takes a few gradient steps on one task's support examples to produce adapted parameters. The outer loop changes the shared starting parameters θ\theta so that the adapted models score well on the query examples across many tasks.

QuestionWith loss (θ−c)2(\theta - c)^2, θ=1\theta = 1, c=5c = 5 and α=0.25\alpha = 0.25, what is the adapted value θ′\theta'?Show answer

The gradient is 2(1−5)=−82(1 - 5) = -8. Then θ′=1−0.25⋅(−8)=1+2=3\theta' = 1 - 0.25 \cdot (-8) = 1 + 2 = 3. This is the midpoint between 1 and 5.

QuestionWhy must meta-learning be evaluated on held-out tasks?Show answer

A meta-learner trained and tested on the same tasks can look good just by fitting those tasks. Testing on held-out tasks checks whether the learned starting point helps with tasks it has not seen, which is what the method is meant to do.

QuestionA team has 10,000 labelled photos of one product category and wants to recognise a new category from 5 photos. Should it use full meta-learning or fine-tuning a pretrained model? Explain briefly.Show answer

Fine-tuning a pretrained model is the simpler first choice. Meta-learning needs many related tasks to train on, and the team has only one large dataset and one new category. A pretrained model fine-tuned on the 5 photos, possibly with the large dataset, is cheaper and easier to evaluate, and it may be enough.

Sources