Find inactive hidden units
A unit is another word for a neuron. A ReLU unit outputs 0 whenever the value going into it is 0 or below, and when that happens no gradient passes through it. If this is true for every example, the unit contributes nothing and learns nothing: its weights get no signal to change. Such a unit is called dead. Finding how many units are inactive on a batch is a quick health check for a network.
Learn it
Words to know
- neuron
- The basic unit of a network: a weighted sum of its inputs plus a bias, passed through an activation function.
- preactivation
- The value that goes into an activation function: the neuron's weighted sum plus bias, before the bend.
- ReLU
- The function
max(0, x). It keeps positive numbers and turns everything else into 0. - batch
- A group of examples that the model processes together in one step.
How it works
- The table has one row per example and one column per unit. Each number is the value going into that unit's ReLU.
- Look at one column at a time.
- If every value in the column is 0 or below, the unit was inactive for the whole batch. Record its column index.
- A single value above 0 is enough to keep the unit alive.
Worked example
Input preactivations = [[0, 1, -1], [-2, 2, 0]] Output [0, 2]
| Unit | Its values | Any above 0? | Inactive? |
|---|---|---|---|
| 0 | 0 and -2 | No | Yes |
| 1 | 1 and 2 | Yes | No |
| 2 | -1 and 0 | No | Yes |
| unit 0 | unit 1 | unit 2 | |
|---|---|---|---|
| example 0 | 0 | 1 | -1 |
| example 1 | -2 | 2 | 0 |
- alive
- inactive on this batch
Exactly 0 counts as inactive: ReLU outputs 0 there, and this course treats its slope as 0 too.
Why it matters
A layer where most units are inactive is wasting its size. A learning rate that is too high and badly scaled starting weights are the usual causes.
Your turn
Find the units that were inactive on every example of a batch.
preactivations- a table with at least one row: one row per example, one column per unit, holding the value that went into each ReLU
- Returns
- The column indexes whose values are all 0 or below, in increasing order.
Example 1
Input preactivations = [[0, 1, -1], [-2, 2, 0]] Output [0, 2]
Units 0 and 2 never get a value above 0. Unit 1 does, twice.
Example 2
Input preactivations = [[1, 2]] Output []
Both units get a positive value, so none is inactive.
Example 3
Input preactivations = [[2, -1], [-5, -1]] Output [1]
Unit 0 is at 2 for the first example, so it is alive, although its last value is -5. Unit 1 never gets above 0.
Example 4
Input preactivations = [[-1, -1], [-1, 3]] Output [0]
Unit 1 is at -1 for the first example but at 3 for the second. One positive value keeps it alive.
Hints
Each hint gives away a little more. Open one only when you need it.
Hint 1: Where to start
The table is stored row by row, but the question is about columns. Loop over column indexes.
Hint 2: The key step
For column
j, look atrow[j]in every row. The unit is inactive when all of those are<= 0.Hint 3: The outline
Set
units = len(preactivations[0]). For eachjinrange(units): ifall(row[j] <= 0 for row in preactivations), appendjto the result.
Solution
From here on the page gives the method away. If you are still solving, go back to the hints.
Step by step
- Unit j's value in every example. Taking
row[j]from each row reads the table downwards, one column at a time. - Inactive when none of them is above 0.
allstops at the first value above 0. Going throughjin order keeps the answer sorted.
The same numbered steps are the comments in the solution.
Python
def solve(preactivations):
units = len(preactivations[0])
inactive = []
for j in range(units):
# 1. Unit j's value in every example
column = [row[j] for row in preactivations]
# 2. Inactive when none of them is above 0
if all(value <= 0 for value in column):
inactive.append(j)
return inactiveWatch out
- Use
<= 0, not< 0. A value of exactly 0 gives an output of 0 and no gradient. - Inactive on one batch is not proof that a unit is dead for good. Another batch might switch it on.
Check yourself
Why do the weights of a dead ReLU unit stop changing?
No gradient passes through a unit whose output is flat at 0, so its weights get no signal to change.
Is a unit with one positive value in the batch inactive?
No. One example where it gets a value above 0 is enough for gradient to reach its weights.