PackNet assigns each task a disjoint subset of a network's weights. After training a task, the weights it does not need are pruned (set to zero and released) and the survivors are frozen; the next task trains only the released weights. Implement the bookkeeping on a single flat weight vector.
packnet_prune(W, free_mask, prune_frac)
W is a (P,) weight vector after training the current task, free_mask a boolean (P,) marking the weights that were available to it (not owned by earlier tasks), prune_frac in .n_prune = int(prune_frac * n_free) with the smallest magnitude (ties by lower index first, stable sort on |W|). Return (task_mask, new_free_mask, W_pruned): task_mask is boolean and marks the free weights that survived (now owned by this task), new_free_mask marks the pruned weights (available to the next task), and W_pruned equals W with pruned entries set to zero. Weights outside free_mask are untouched and belong to neither mask.packnet_inference_mask(task_masks, task_id)
task_masks is a list of boolean masks in task order. Return the boolean union of the masks of tasks 0..task_id inclusive: inference for task t uses the weights of task t and of every earlier task, never later ones.packnet_train_grad(grad, free_mask)
grad with entries outside free_mask set to zero, the gradient actually applied while training a new task.capacity_schedule(P, prune_frac, n_tasks)
prune_frac of what it was given, starting with all P weights free; integer arithmetic with int() truncation at every step as in packnet_prune.np.argsort(np.abs(W[free_idx]), kind="stable") ranks the free weights only; map back to global indices before building masks.