After task A is learned, Elastic Weight Consolidation (EWC) estimates how much each parameter matters to A, then penalises moving it while task B trains. Implement the estimate and the penalty.
diagonal_fisher(grads)
grads is an (N, P) array: row is the gradient of the log-likelihood of sample with respect to the parameters, at task A's solution.(P,) diagonal Fisher estimate, the mean over samples of the squared gradient.ewc_penalty(theta, theta_star, fisher, lam)
theta and theta_star are (P,) arrays (current parameters and task A's solution), fisher is (P,), lam a float.(penalty, grad): the scalar as a Python float and its (P,) gradient with respect to theta.ewc_quadratic_solution(h_B, theta_B, fisher, theta_star, lam)
h_B and optimum theta_B (both (P,)).(P,) minimiser of task B's loss plus the EWC penalty, in closed form (each coordinate is independent).