Gradient Episodic Memory (GEM) treats forgetting as a constraint: the proposed update must not increase the loss on the stored memory of earlier tasks. Averaged GEM (A-GEM) checks a single constraint against the gradient of a batch drawn from all memory. Implement both the projection and the check.
agem_project(g, g_ref)
g is the current task's gradient, g_ref the gradient on the memory batch, both (P,) arrays.g unchanged. Otherwise return the projection , the closest vector to g whose inner product with g_ref is zero.g_ref is the zero vector return g unchanged.gem_violations(g, G_ref, tol=0.0)
G_ref is (K, P): one memory gradient per earlier task. Return the sorted list of task indices with , the constraints the update would violate.agem_sequence(g_list, g_ref_list, lr, theta0, decimals=4)
theta <- theta - lr * agem_project(g, g_ref) for each pair in the two lists (same length), starting from theta0. Return (theta, n_projected): the final parameters rounded to decimals as a list, and how many steps were projected.g along g_ref; after it, the inner product with g_ref is exactly zero up to rounding.g_ref was non-zero.