Machine Unlearning › Approximate Unlearning: Gradient Ascent on Forgets
Language AI HandbookAll chapters We copy the original model, then run full-batch SGD on the proxy loss. We use 400 steps, a learning rate of 0.02 and a displacement penalty of 0.005. The code requires the original weights and both sets of synthetic examples. The fresh retrain plays no part in the update.
PythonIn [10]Expand
def approximate_unlearn(
original, forget_features, forget_labels, retain_features, retain_labels
):
model = copy.deepcopy(original)
initial = [param.detach().clone() for param in model.parameters()]
optimizer = torch.optim.SGD(model.parameters(), lr=0.02)
X_f, y_f = (
torch.from_numpy(forget_features),
torch.from_numpy(forget_labels),
)
X_r, y_r = (
torch.from_numpy(retain_features),
torch.from_numpy(retain_labels),
)
for _ in range(400):
optimizer.zero_grad()
forget_loss = nn.functional.cross_entropy(model(X_f), y_f)
retain_loss = nn.functional.cross_entropy(model(X_r), y_r)
displacement = sum(
(param - start).square().sum()
for param, start in zip(model.parameters(), initial)
)
objective = -forget_loss + retain_loss + 5e-3 * displacement
objective.backward()
optimizer.step()
return model
model_unlearned = approximate_unlearn(
model_orig, X_forget, y_forget, X_ret, y_ret
)
models = {
"Original": model_orig,
"Retrain": model_retrain,
"Approximate": model_unlearned,
}
all_metrics = {
name: behavior_metrics(model, model_retrain)
for name, model in models.items()
}ConsoleOut [11]Expand
Original | forget acc 0.967 | retain acc 0.865 | disagreement 0.800
Retrain | forget acc 0.167 | retain acc 0.868 | disagreement 0.000
Approximate | forget acc 0.000 | retain acc 0.727 | disagreement 0.777The approximate update suppresses class 0 on the held-out forget points in this run, even more often than the retrain does. It disagrees with the retrain on roughly 78% of these points and also lowers retain accuracy from 86.5% to 72.7%. The predicted regions make that distinction visible.
PythonIn [12]Expand
grid_x = np.linspace(-5, 5, 201, dtype=np.float32)
grid_y = np.linspace(-4, 4, 161, dtype=np.float32)
grid_xx, grid_yy = np.meshgrid(grid_x, grid_y)
grid_points = np.column_stack([grid_xx.ravel(), grid_yy.ravel()])
regions = {
name: predict(model, grid_points).reshape(grid_xx.shape)
for name, model in models.items()
}VisualizationOut [13]Expand

A grid samples the hard-label decision regions. It does not show confidence, and it says nothing about how either model behaves outside the plotted rectangle. The same input coordinates and class markings let us compare where each model assigns classes 1 and 2.
A probe-level comparison is especially helpful when the model looks successful by a thresholded outcome. At fixed points, an approximate edit could leave the same argmax as the fresh retrain but assign sharply different scores to the classes. If a downstream system uses scores for confidence rejection or routing, the released behavior of that system can differ even when the class labels match. Add a score-level comparison if the downstream system consumes scores. A label agreement test only needs hard predictions.
The grid and probe table also use different weights over input locations. The grid spaces the two coordinates uniformly in the displayed rectangle. The probes concentrate around their Gaussian means. A large area of the grid can contribute little to probe disagreement when few probes land there. The map can suggest places to inspect, while the table gives the result under the stated sampling scheme.
Comments
Add to the conversation
A question, a correction, or an example from your own work.
No comments yet. Be the first to share your thoughts!