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:
- 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.
- Catastrophic forgetting. Training on the new task can hurt performance on old tasks. This is the failure described in Catastrophic Forgetting.
- 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 , 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.
- Compute the new gradient. The new example gives , the direction that would reduce the loss on this example. Check: one entry per parameter, two parameters.
- Compute one gradient per past task from memory. The task 1 memory gives . Check: it is computed from stored task 1 examples only, not from the new one.
- Check the constraint. The update is safe for task 1 if . Here , which is negative. A small step along would raise the task 1 loss, so the constraint is violated.
- Project the gradient. Find the vector closest to that satisfies the constraint. Removing the negative part along gives . Check: , and the distance from is .
- Take the step. With learning rate the parameters move by , not by . Check: the first parameter does not move, and , so the task 1 loss does not rise to first order.
- 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
Let be the memory for past task , let be the network with parameters , and let be the loss. For a new example , GEM computes the gradient of its loss, the gradient of the average loss over each memory with , and then the vector closest to that keeps every . The parameters move by .
Step 1 and 2 in symbols
The gradients are
Here is the gradient with respect to the parameters, a vector with one entry per parameter. The number of parameters is .
Step 3 in symbols: why the inner product is the test
Stepping to changes the loss on memory to first order by . So that loss does not rise when
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
where is a matrix whose columns are the memory gradients. A direct solve has variables, which can number in the millions. Its dual has one variable per past task instead. Introduce multipliers and set the derivative of the Lagrangian with respect to to zero, which gives . Substituting back gives the dual
This is a quadratic program on variables, the number of tasks seen so far. The paper states the dual in a more general form with matrices , and (its equations 9 and 10); the version here is the same problem written with GEM's memory gradients. The matrix holds the inner products between the memory gradients.
Step 5 in symbols
The parameter update is , where is the learning rate. The paper sets its learning rates by grid search (Appendix A).
Step 6 in symbols: the three metrics
Let be the test accuracy on task after training on task , and let be the accuracy on task before training starts. With tasks,
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
A network has one past task with memory gradient . The new example gives .
- Check the constraint: .
- The result is , so nothing needs projecting. .
- Take the step with : .
Interpretation: when the new gradient already agrees with the old task's gradient, GEM behaves like ordinary stochastic gradient descent.
Two past tasks have memory gradients and . The new example gives .
- Both constraints fail: and .
- Here , so is the identity and .
- Solve the dual . Each coordinate is minimised at and , which are both nonnegative, so .
- Recover the projected gradient: .
- Check: both inner products are , 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.
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.
- At 200 memories, GEM leads iCaRL by .
- At 5,120 memories, GEM leads iCaRL by .
- The gap grows by while the memory grows by a factor of .
- GEM's accuracy rises by over that range, while iCaRL's rises by .
- 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 . iCaRL rises by . GEM gains about nine times as much over that step.
Results
Setup. Each experiment has 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 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:
| method | 1 epoch | 2 epochs | 5 epochs |
|---|---|---|---|
| single, shuffled data | 0.83/-0.00 | 0.87/-0.00 | 0.89/-0.00 |
| single | 0.53/-0.08 | 0.49/-0.25 | 0.43/-0.40 |
| independent | 0.56/-0.00 | 0.64/-0.00 | 0.67/-0.00 |
| multimodal | 0.76/-0.02 | 0.72/-0.11 | 0.59/-0.28 |
| EWC | 0.55/-0.19 | 0.59/-0.17 | 0.61/-0.11 |
| GEM | 0.86/+0.05 | 0.88/+0.02 | 0.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
- Section 1, Introduction (needs Continual Learning, Empirical Risk Minimization, Catastrophic Forgetting): the setting, the three challenges and why iid training breaks.
- Section 2, A Framework for Continual Learning: task descriptors and the ACC, BWT and FWT definitions. The matrix is the only new object. Transfer Learning explains why forward transfer is worth measuring.
- Section 3, Gradient of Episodic Memory: the core. It needs Dot Product for the constraint and Gradient Descent for the update, and Quadratic Programming and Lagrangian Duality for the projection. Read Algorithm 1 alongside the walkthrough above. The paper notes that quadratic programs are at the heart of Support Vector Machines.
- Section 4, Experiments: the networks use Rectified Linear Unit (ReLU) units and a small residual network. Training uses Stochastic Gradient Descent on mini-batches of 10 samples. The baselines include Overcoming catastrophic forgetting in neural networks, which uses a penalty, and iCaRL, which uses distillation (Knowledge Distillation). The loss in each case is a Loss Function.
- Section 5, Related work: regularisation methods, memory-based methods and multitask learning. Skim it for the landscape.
- Section 6, Conclusion and Appendices: the three limitations, the hyper-parameter grid in Appendix A, and the full accuracy matrices in Appendix B.
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 . To first order, a step along changes the loss on task by a term proportional to . 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 , so the constraint fails. The projection removes the negative second component, giving . Check with the dual: and , so and . The distance is , and one half of its square is .
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 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
- Lopez-Paz, D. and Ranzato, M. A. (2017). Gradient Episodic Memory for Continual Learning. arXiv:1706.08840. https://arxiv.org/abs/1706.08840
- Authors' implementation, linked from the paper: https://github.com/facebookresearch/GradientEpisodicMemory