Model-Agnostic Meta-Learning (MAML) in RL
MAML learns a starting policy that sits close to many tasks at once, so one or two gradient steps on a new task land near good behavior.
Why Does This Exist?
Deep RL is hungry. A new friction, speed, or mass can force retraining from scratch for millions of steps. Meta-RL asks instead for fast learning across a task family , so a new task needs only a few trials.
Early meta methods hid adaptation inside recurrent memory. That ties learning to one architecture and struggles outside the training range. MAML grounds adaptation in gradient descent itself. It learns an init that is not best anywhere but is easy to adjust everywhere, so one policy gradient step per task does the work. Any differentiable policy fits. For the policy gradient used inside see policy gradient. For memory based meta learning see RL squared.
The one idea you need first
Inner loop adapts to one task. Outer loop improves the starting point across tasks. Call the inner rate and the outer rate. Inner data comes from the start policy, outer scores come from adapted policies.
Think of It Like This
Base camp on the saddle
You must guide climbs on several peaks. Camp in the east valley and peak 1 is quick but peak 2 takes days to reach. Camp on the central saddle and no summit is at your door, yet each summit is one short ridge sprint away.
MAML picks the saddle. Zero shot views are modest. One step sprints and reach each peak fast. Each week you shift camp by the average miss, which is the outer update with rate .
Where it stops: mountains stay put. In RL moving camp changes the scouting paths you collect, so the start also shapes exploration, not just distance.
How It Actually Works
Policy has weights . For task , collect rollouts from , form return , and take one ascent step:
Then sample fresh rollouts from adapted and score post performance. The meta objective sums it:
Outer update with rate :
Chain rule gives a Hessian term , so full MAML is second order. First order MAML drops the Hessian and treats as identity, which cuts variance at small cost in speed.
Worked example
Linear policy with , reward , targets and , , , start .
Task 1: . Gradient . Adapt . New .
Task 2: . Gradient . Adapt . New .
Totals: zero shot , adapted . Meta slopes with : task 1 gives , task 2 gives , sum . New start:
Start shifts toward midpoint . The diagram draws , , , and on one line.
Code
This replays the inner and outer arithmetic with the same numbers.
from typing import List, Tuple
def reward(theta: float, k: float) -> float: """Quadratic task reward.""" return -((theta - k) ** 2)
def grad(theta: float, k: float) -> float: """d reward / d theta.""" return -2.0 * (theta - k)
if __name__ == "__main__": theta, alpha, beta = 2.50, 0.10, 0.10 t1, t2 = 2.0, 4.0 th1 = theta + alpha * grad(theta, t1) th2 = theta + alpha * grad(theta, t2) print(f"th1 = {th1:.4f}") # -> th1 = 2.4000 print(f"th2 = {th2:.4f}") # -> th2 = 2.8000 print(f"adapt = {reward(th1, t1) + reward(th2, t2):.4f}") # -> adapt = -1.6000 mg = (-2.0 * (th1 - t1) * 0.8) + (-2.0 * (th2 - t2) * 0.8) print(f"meta grad = {mg:.4f}") # -> meta grad = 1.2800 print(f"theta new = {theta + beta * mg:.4f}") # -> theta new = 2.6280Watch Out For
Noisy Hessians shake the outer loop
Full MAML multiplies two sampled gradients through the Hessian, so variance compounds. Symptom: outer loss swings, entropy collapses, and exploration dies early.
Use first order MAML for most RL runs, normalize advantages per task so large reward scales do not dominate, and keep inner steps to one or two until the start stabilizes. Add trust region clipping when tasks fight each other.
The Quick Version
- MAML learns an init so one gradient step per task reaches high return, with inner rate and outer rate .
- Adaptation is plain gradient ascent, so any differentiable policy works with no special memory.
- In the 1D check start adapts to and , lifting total from to .
- Meta gradient moves start to , toward the task midpoint.
- In RL the start also shapes exploration, and first order updates tame Hessian noise.