PigeonColumba compressa

Lesson 8 of 10

Structured pruning: removing neurons for real

Zeroing weights doesn't make a network faster on ordinary hardware; the matrix multiply is the same size. Structured pruning deletes whole neurons, so the matrices shrink.

Deleting hidden neuron j between two layers means three edits that must agree:

  1. remove column j of W1 (its incoming weights),
  2. remove entry j of b1,
  3. remove row j of W2 (its outgoing weights).

Miss one and the shapes no longer match. In a CNN with skip connections the same idea needs a dependency group of layers, which is what torch-pruning solves for the cockpit's YOLOv8n.

Implement keep_neurons(W1, b1, W2, keep) returning the smaller (W1, b1, W2) that keeps only the hidden neurons listed in keep, and l1_scores(W1) returning one importance score per hidden neuron (the L1 norm of its incoming weights).

Your code

Where this is used