Find the fraction of dead ReLU units

Why it matters Training dashboards track dead units to spot a learning rate that is too high.

A ReLU unit that outputs 0 for every input in a batch gets no gradient and may never recover.

Write dead_fraction(pre) where pre is a (batch, units) array of pre-activations. Return the fraction of units whose pre-activation is <= 0 for every row.

Signaturedef dead_fraction(pre)

Example

dead_fraction([[1, -1, -2], [0.5, -3, 4]]) is 1/3, since only unit 1 is never positive.

Understand the idea

A ReLU unit is dead on a batch if its input is zero or negative for every example. Its output is always 0, so it passes no gradient back and its weights stop learning. Measuring the share of dead units is a quick health check for a network. Worked example: pre = [[-1, 2, 0], [-0.5, -1, 0], [-2, 3, -1]], three rows and three units. Unit 0 sees -1, -0.5, -2: never positive, so dead. Unit 1 sees 2, -1, 3: positive at least once, so alive. Unit 2 sees 0, 0, -1: never strictly positive, so dead. Dead fraction = 2 / 3 = about 0.667. Rule: for each unit (column), check whether pre <= 0 holds for all rows (reduce over the batch axis, axis 0), then return the mean of those booleans over units. Reducing over the wrong axis measures dead examples, not dead units. Zero counts as dead because ReLU outputs 0 there. Used in: debugging training with a learning rate that is too high, and comparing ReLU with leaky or GELU variants.

solution.pyPython idle
Ctrl/Cmd + Enter runs sample tests

Run checks the first 2 tests. Submit checks all 3.

  • Sample 1: exampledead_fraction([[1, -1, -2], [0.5, -3, 4]])
  • Sample 2: all deaddead_fraction([[-1, 0], [-2, -3], [0, -1]])
  • Test 3: randomdead_fraction(_P)