Inverted Dropout: Scaling the Survivors

Neural NetworksMedium+20 XP+15

Inverted Dropout: Scaling the Survivors

Dropout deletes a random slice of a layer's activations on every training step, so the network can't lean on any one unit. At test time nothing is dropped — which creates a problem: the layer downstream suddenly sees a much larger sum than it did during training.

Inverted dropout solves it during training instead of after. Surviving activations are scaled up by 1 / (1 - drop_prob), so the expected total stays the same and inference needs no adjustment at all.

Task: write apply_dropout(activations, mask, drop_prob), where mask holds 1 for a unit that survives and 0 for one that's dropped. Return the resulting activations, rounded to 4 decimal places.

  • The mask is handed to you, so this is fully deterministic — no random numbers.
  • drop_prob = 0.0 scales by 1 and changes nothing.
  • drop_prob = 1.0 drops everything; return all zeros rather than dividing by zero.

Because the scaling lives in the training path, the deployed model is just the plain network. That's the entire reason inverted dropout won over the original formulation, which rescaled at test time and made inference depend on a training hyperparameter.

Dropout & Stochastic Regularization

Keep Probability (q):0.5

Layer Node States

Node 1:ACTIVE
Node 2:ACTIVE
Node 3:ACTIVE
Node 4:ACTIVE
Node 5:ACTIVE
Node 6:ACTIVE

Scaled Output Activations

Node 1:2.00
Node 2:1.60
Node 3:2.40
Node 4:3.00
Node 5:1.80
Node 6:2.20

Note how active nodes are scaled up by 1/q to maintain the expected sum of activations, avoiding a scaling mismatch at test time.

Examples

activations = [1.0, 2.0, 3.0, 4.0]
mask = [1, 0, 1, 1]
drop_prob = 0.5
returns [2.0, 0.0, 6.0, 8.0]
activations = [5.0, -2.0]
mask = [1, 1]
drop_prob = 0.0
returns [5.0, -2.0]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run