Search

Search papers and lessons

Gradient Episodic Memory for Continual Learning

David Lopez-Paz, Marc'Aurelio Ranzato · 2017

Gradient Episodic Memory (GEM) learns a stream of tasks without forgetting them: a small memory of past examples turns each update into a constraint that stops old losses from rising.

On this page

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

TL;DR

Gradient Episodic Memory (GEM) trains one network on a stream of tasks, one example at a time, without forgetting the earlier tasks. It keeps a small memory of examples from each past task. Before each update it checks whether the new gradient would raise the loss on any remembered task. If it would, GEM changes the gradient by the smallest amount that stops every old loss from rising.

Builds on: Continual Learning, Quadratic Programming, Lagrangian Duality · Leads to: Experience Replay (the same memory idea, used in reinforcement learning)

Why it mattered

The paper observes that most continual-learning work reports one number, the average performance across tasks, after training on a few large tasks with many passes over the data. GEM's authors instead study the one-pass setting and report three numbers: the final average accuracy (ACC), backward transfer (BWT, how much learning later tasks changed earlier ones) and forward transfer (FWT, how much earlier learning helped tasks not yet seen). This paper defines these three measures in the form used here, and the rest of this lesson uses them.

The second idea is the memory. Replaying stored examples is a standard move in reinforcement learning, where an agent keeps a buffer of past experience and trains on random samples from it (Experience Replay). GEM uses a similar buffer, but keeps one memory per task and uses its gradients as constraints rather than as extra training data.

The third idea is the constraint itself. Earlier methods either penalised changes to the weights (Overcoming catastrophic forgetting in neural networks) or kept old predictions fixed by distillation (Knowledge Distillation). The paper points out that fixing old predictions rules out positive backward transfer, an update that also helps an old task. GEM only forbids old losses from rising, so an old loss is free to fall.

The problem

Standard supervised learning assumes examples are independent and identically distributed (iid) draws from one fixed distribution, and it trains by Empirical Risk Minimization over a dataset that can be revisited many times. A continual learner breaks both assumptions. Examples arrive in order, each is seen once, and the distribution switches when a new task starts. The paper names three challenges that follow:

  1. Non-iid input data. Once tasks switch, a long run of examples from the new task arrives, so the stream is not iid under any single distribution.
  2. Catastrophic forgetting. Training on the new task can hurt performance on old tasks. This is the failure described in Catastrophic Forgetting.
  3. Transfer. If tasks are related, what was learned earlier should make new tasks faster to learn, and can improve old ones.

Each example carries a task descriptor tt, an integer naming its task. In its simplest case the paper uses integer task descriptors, and it leaves structured descriptors for zero-shot transfer (Zero-Shot Learning) to future work.

How it works

Here is one update on a tiny network with two parameters, so every gradient is a pair of numbers. The network has already learned task 1, and its memory holds task 1 examples. A new example from task 2 arrives.

  1. Compute the new gradient. The new example gives g=(−1,1)g = (-1, 1), the direction that would reduce the loss on this example. Check: one entry per parameter, two parameters.
  2. Compute one gradient per past task from memory. The task 1 memory gives g1=(1,0)g_1 = (1, 0). Check: it is computed from stored task 1 examples only, not from the new one.
  3. Check the constraint. The update is safe for task 1 if ⟨g,g1⟩≥0\langle g, g_1 \rangle \geq 0. Here ⟨g,g1⟩=(−1)(1)+(1)(0)=−1\langle g, g_1 \rangle = (-1)(1) + (1)(0) = -1, which is negative. A small step along gg would raise the task 1 loss, so the constraint is violated.
  4. Project the gradient. Find the vector g~\tilde g closest to gg that satisfies the constraint. Removing the negative part along g1g_1 gives g~=(0,1)\tilde g = (0, 1). Check: ⟨g~,g1⟩=0≥0\langle \tilde g, g_1 \rangle = 0 \geq 0, and the distance from gg is ∥g−g~∥=∥(−1,0)∥=1\|g - \tilde g\| = \|(-1, 0)\| = 1.
  5. Take the step. With learning rate α=0.1\alpha = 0.1 the parameters move by −αg~=(0,−0.1)-\alpha \tilde g = (0, -0.1), not by −αg=(0.1,−0.1)-\alpha g = (0.1, -0.1). Check: the first parameter does not move, and ⟨g1,−αg~⟩=0\langle g_1, -\alpha \tilde g \rangle = 0, so the task 1 loss does not rise to first order.
  6. Store and evaluate. The new example joins the task 2 memory. At the end of each task the model is tested on every task, and those accuracies fill one row of the results matrix.
flowchart LR
  A["New example: gradient g"] --> B["Gradients g_k from each task memory"]
  B --> C{"Is every inner product of g with g_k nonnegative?"}
  C -- yes --> E["Update with g"]
  C -- no --> D["Project g to the closest g~ that satisfies all constraints"]
  D --> E
  E --> F["Store the example in its task memory"]

The math

Definition: GEM update

Let Mk\mathcal{M}_k be the memory for past task kk, let fθf_\theta be the network with parameters θ\theta, and let ℓ\ell be the loss. For a new example (x,t,y)(x, t, y), GEM computes the gradient gg of its loss, the gradient gkg_k of the average loss over each memory Mk\mathcal{M}_k with k<tk \lt t, and then the vector g~\tilde g closest to gg that keeps every ⟨g~,gk⟩≥0\langle \tilde g, g_k \rangle \geq 0. The parameters move by θ←θ−αg~\theta \leftarrow \theta - \alpha \tilde g.

Step 1 and 2 in symbols

The gradients are

g=∇θ ℓ(fθ(x,t),y),gk=∇θ ℓ(fθ,Mk)=∇θ1∣Mk∣∑(xi,k,yi)∈Mkℓ(fθ(xi,k),yi).g = \nabla_\theta\, \ell(f_\theta(x, t), y), \qquad g_k = \nabla_\theta\, \ell(f_\theta, \mathcal{M}_k) = \nabla_\theta \frac{1}{|\mathcal{M}_k|} \sum_{(x_i, k, y_i) \in \mathcal{M}_k} \ell(f_\theta(x_i, k), y_i).

Here ∇θ\nabla_\theta is the gradient with respect to the parameters, a vector with one entry per parameter. The number of parameters is pp.

Step 3 in symbols: why the inner product is the test

Stepping to θ′=θ−αg~\theta' = \theta - \alpha \tilde g changes the loss on memory kk to first order by −α⟨gk,g~⟩-\alpha \langle g_k, \tilde g \rangle. So that loss does not rise when

⟨g~,gk⟩≥0for all k<t.\langle \tilde g, g_k \rangle \geq 0 \quad \text{for all } k \lt t.

This is an approximation. It assumes the loss is locally linear for a small step and that the memory is a fair sample of the old task.

Step 4 in symbols: the projection is a small quadratic program

The closest feasible gradient solves

min⁡g~ 12∥g−g~∥22subject toG⊤g~≥0,\min_{\tilde g} \ \tfrac{1}{2} \|g - \tilde g\|_2^2 \quad \text{subject to} \quad G^\top \tilde g \geq 0,

where G=(g1,…,gt−1)G = (g_1, \ldots, g_{t-1}) is a p×(t−1)p \times (t-1) matrix whose columns are the memory gradients. A direct solve has pp variables, which can number in the millions. Its dual has one variable per past task instead. Introduce multipliers v≥0v \geq 0 and set the derivative of the Lagrangian with respect to g~\tilde g to zero, which gives g~=g+Gv\tilde g = g + Gv. Substituting back gives the dual

min⁡v≥0 12v⊤G⊤G v+g⊤G v,g~=g+Gv⋆.\min_{v \geq 0} \ \tfrac{1}{2} v^\top G^\top G\, v + g^\top G\, v, \qquad \tilde g = g + G v^\star.

This is a quadratic program on t−1t-1 variables, the number of tasks seen so far. The paper states the dual in a more general form with matrices CC, AA and bb (its equations 9 and 10); the version here is the same problem written with GEM's memory gradients. The matrix G⊤GG^\top G holds the inner products between the memory gradients.

Step 5 in symbols

The parameter update is θ←θ−αg~\theta \leftarrow \theta - \alpha \tilde g, where α\alpha is the learning rate. The paper sets its learning rates by grid search (Appendix A).

Step 6 in symbols: the three metrics

Let Ri,jR_{i,j} be the test accuracy on task jj after training on task ii, and let bˉi\bar b_i be the accuracy on task ii before training starts. With TT tasks,

ACC=1T∑i=1TRT,i,BWT=1T−1∑i=1T−1(RT,i−Ri,i),FWT=1T−1∑i=2T(Ri−1,i−bˉi).\mathrm{ACC} = \frac{1}{T} \sum_{i=1}^{T} R_{T,i}, \qquad \mathrm{BWT} = \frac{1}{T-1} \sum_{i=1}^{T-1} \big(R_{T,i} - R_{i,i}\big), \qquad \mathrm{FWT} = \frac{1}{T-1} \sum_{i=2}^{T} \big(R_{i-1,i} - \bar b_i\big).

ACC is the average final accuracy. A negative BWT is forgetting, and a positive BWT means later tasks helped earlier ones. FWT measures how well the model does on a task before it has trained on that task.

Worked examples

Example
1. One constraint that already holds

A network has one past task with memory gradient g1=(1,0)g_1 = (1, 0). The new example gives g=(2,−1)g = (2, -1).

  1. Check the constraint: ⟨g,g1⟩=2⋅1+(−1)⋅0=2\langle g, g_1 \rangle = 2 \cdot 1 + (-1) \cdot 0 = 2.
  2. The result is 2≥02 \geq 0, so nothing needs projecting. g~=g=(2,−1)\tilde g = g = (2, -1).
  3. Take the step with α=0.1\alpha = 0.1: θ←θ−0.1⋅(2,−1)=θ+(−0.2,0.1)\theta \leftarrow \theta - 0.1 \cdot (2, -1) = \theta + (-0.2, 0.1).

Interpretation: when the new gradient already agrees with the old task's gradient, GEM behaves like ordinary stochastic gradient descent.

Example
2. Two past tasks that both conflict

Two past tasks have memory gradients g1=(1,0)g_1 = (1, 0) and g2=(0,1)g_2 = (0, 1). The new example gives g=(−2,−1)g = (-2, -1).

  1. Both constraints fail: ⟨g,g1⟩=−2\langle g, g_1 \rangle = -2 and ⟨g,g2⟩=−1\langle g, g_2 \rangle = -1.
  2. Here G=(1001)G = \begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix}, so G⊤GG^\top G is the identity and g⊤G=(−2,−1)g^\top G = (-2, -1).
  3. Solve the dual min⁡v≥0 12(v12+v22)−2v1−v2\min_{v \geq 0} \ \tfrac{1}{2}(v_1^2 + v_2^2) - 2 v_1 - v_2. Each coordinate is minimised at v1=2v_1 = 2 and v2=1v_2 = 1, which are both nonnegative, so v⋆=(2,1)v^\star = (2, 1).
  4. Recover the projected gradient: g~=g+Gv⋆=(−2+2,−1+1)=(0,0)\tilde g = g + G v^\star = (-2 + 2, -1 + 1) = (0, 0).
  5. Check: both inner products are 0≥00 \geq 0, so the constraints hold, and the update is zero.

Interpretation: GEM does not learn the new example at this step. It only keeps the old losses from rising, so the new example waits until a later update points somewhere else.

Example
3. Reading the memory-size results

In Table 2 of the paper, on CIFAR-100, the final average accuracy (ACC) depends on the episodic memory size. The reported values are GEM 0.487 (200 memories), 0.579 (1,280), 0.633 (2,560) and 0.654 (5,120), and iCaRL 0.436, 0.494, 0.500 and 0.508 at the same sizes.

  1. At 200 memories, GEM leads iCaRL by 0.487−0.436=0.0510.487 - 0.436 = 0.051.
  2. At 5,120 memories, GEM leads iCaRL by 0.654−0.508=0.1460.654 - 0.508 = 0.146.
  3. The gap grows by 0.146−0.051=0.0950.146 - 0.051 = 0.095 while the memory grows by a factor of 5120/200=25.65120 / 200 = 25.6.
  4. GEM's accuracy rises by 0.654−0.487=0.1670.654 - 0.487 = 0.167 over that range, while iCaRL's rises by 0.508−0.436=0.0720.508 - 0.436 = 0.072.
  5. So GEM gains more from extra memory than iCaRL does.

Try it: between 1,280 and 2,560 memories, how much does GEM's accuracy rise, and how much does iCaRL's rise?

Answer: GEM rises by 0.633−0.579=0.0540.633 - 0.579 = 0.054. iCaRL rises by 0.500−0.494=0.0060.500 - 0.494 = 0.006. GEM gains about nine times as much over that step.

Results

Setup. Each experiment has T=20T = 20 tasks, and each example is seen once. On MNIST each task has 1,000 examples from 10 classes. On CIFAR-100 each task has 2,500 examples from 5 classes. The MNIST networks have two hidden layers of 100 ReLU units. The CIFAR-100 network is a smaller ResNet-18 (three times fewer feature maps) with a final linear classifier per task, and it has p=1,109,240p = 1{,}109{,}240 parameters. Training uses plain SGD on mini-batches of 10 samples. The GEM memory holds 5,120 examples.

Main findings. On the MNIST and CIFAR-100 benchmarks, GEM performs similarly to or better than the multimodal baseline, which is well suited to MNIST. The paper reports that GEM minimises backward transfer while showing negligible or positive forward transfer. On CIFAR-100, GEM shows minimal forgetting and positive backward transfer.

Memory and passes (MNIST rotations, Table 3). Accuracy and BWT are given as ACC/BWT for 1, 2 and 5 epochs per task:

method1 epoch2 epochs5 epochs
single, shuffled data0.83/-0.000.87/-0.000.89/-0.00
single0.53/-0.080.49/-0.250.43/-0.40
independent0.56/-0.000.64/-0.000.67/-0.00
multimodal0.76/-0.020.72/-0.110.59/-0.28
EWC0.55/-0.190.59/-0.170.61/-0.11
GEM0.86/+0.050.88/+0.020.89/-0.02

Memory-less methods lose more as the passes increase. GEM's ACC matches the shuffled-data oracle at 5 epochs (0.89 for both), and at 1 epoch it is above that oracle (0.86 against 0.83).

Cost (Table 1, CPU training time in seconds). On MNIST permutations: single 11, independent 11, multimodal 14, EWC 179, GEM 77. On MNIST rotations: single 11, independent 16, multimodal 13, EWC 169, GEM 135. GEM is cheaper than EWC in both rows, but much more expensive than the memory-less models. Its cost is one backward pass per past task at each step.

Limits. The benchmarks are small, with 20 tasks, a small network and a few thousand examples per task. The paper's own list of limitations is that GEM does not use structured task descriptors, which could give positive forward transfer through zero-shot learning; that it does not investigate advanced memory management, such as building coresets of tasks; and that each iteration needs one backward pass per past task, which increases computation time. The paper reports GEM's forward transfer as negligible or positive, and it does not claim large gains.

Common misreadings. It is tempting to think GEM replays old examples as training data. In fact the memory is used only as constraints. The paper says that adding the memory loss to the objective would make the model overfit the few stored examples. It is also tempting to think that a non-increasing old loss means no forgetting at all. The guarantee is first-order and approximate, and it relies on a memory that represents the old task.

Reading guide

Check yourself

QuestionWhat does the GEM constraint for a past task k require of the new gradient g and the memory gradient g_k?Show answer

It requires the inner product ⟨g,gk⟩≥0\langle g, g_k \rangle \geq 0. To first order, a step along gg changes the loss on task kk by a term proportional to −⟨g,gk⟩-\langle g, g_k \rangle. A nonnegative inner product means that loss does not rise.

QuestionOne past task has memory gradient g_1 = (0, 1), and the new gradient is g = (3, -4). What is one half of the squared distance between g and the projected gradient?Show answer

The inner product is ⟨g,g1⟩=−4<0\langle g, g_1 \rangle = -4 \lt 0, so the constraint fails. The projection removes the negative second component, giving g~=(3,0)\tilde g = (3, 0). Check with the dual: G⊤G=1G^\top G = 1 and g⊤G=−4g^\top G = -4, so v⋆=4v^\star = 4 and g~=g+Gv⋆=(3,−4)+(0,4)=(3,0)\tilde g = g + G v^\star = (3, -4) + (0, 4) = (3, 0). The distance is ∥g−g~∥=∥(0,−4)∥=4\|g - \tilde g\| = \|(0, -4)\| = 4, and one half of its square is 12⋅16=8\tfrac{1}{2} \cdot 16 = 8.

QuestionWhy does GEM use the memory as constraints rather than adding the memory loss to the training objective?Show answer

Adding the memory loss to the objective makes the model fit the few stored examples, which overfits them. A constraint only stops an old loss from rising. Since it allows an old loss to fall, a single update can help an old task, which the paper calls positive backward transfer. Distillation fixes old predictions and rules that out.

QuestionA team must learn a stream of tasks but may not store any past examples for privacy reasons. Which part of GEM stops working, and what does that leave them?Show answer

GEM needs a gradient gkg_k computed from stored examples of each past task, so without a memory it cannot form the constraints. The team must use a method that needs no stored examples, such as the penalty approach of Overcoming catastrophic forgetting in neural networks, which keeps weight statistics instead of data. They should also expect more forgetting than GEM achieves with a memory, since the constraint that protects old tasks is gone.

Sources

Read the full paper

David Lopez-Paz, Marc'Aurelio Ranzato, 2017. arXiv:1706.08840