Inverted dropout

ImplementEasy15 min14 checks

dropoutregularizationgradients

Problem

Dropout sets a random part of the activations to 0 during training. Inverted dropout also divides the kept activations by 1 - p, so the expected value of each activation does not change. At inference time the layer then does nothing. You write the forward pass for both modes and the backward pass.

Task

Write two functions.

  1. dropout_forward(x, p, rng, training=True) returns (out, mask).
  2. dropout_backward(grad_output, mask, p) returns the gradient of the loss with respect to x.

In training mode, build the mask with exactly one call:

\text{mask} = (\texttt{rng.random(x.shape)} \geq p)

True in the mask means kept. Each entry is dropped with probability p. The output is

\text{out} = x \cdot \text{mask} / (1 - p)

In inference mode (training=False), return a copy of x and a mask that is True everywhere. Do not draw from rng in inference mode.

The backward pass uses the mask from the forward pass: \partial L / \partial x = \text{grad\_output} \cdot \text{mask} / (1 - p).

Input and output

NameTypeShapeMeaning
xfloat arrayany shapethe activations
pfloatthe probability to drop an entry, 0 <= p < 1
rngnp.random.Generatorthe random generator for the mask
trainingboolTrue for training mode, False for inference mode
grad_outputfloat arraysame as xthe gradient of the loss with respect to out
maskbool arraysame as xthe mask from dropout_forward
  • dropout_forward returns a tuple (out, mask). out is a float array and mask is a bool array. Both have the shape of x.
  • dropout_backward returns a float array with the shape of x.

Constraints

  • x has 1 to 4 axes and at most 40000 entries.
  • 0 <= p < 1.
  • Do not change the input arrays.
  • Use numpy only.

Examples

pythonx = np.array([[1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0]])
out, mask = dropout_forward(x, 0.5, np.random.default_rng(0))
grad = dropout_backward(np.ones((2, 4)), mask, 0.5)
textout
[[ 2.  0.  0.  0.]
 [10. 12. 14. 16.]]
mask
[[ True False False False]
 [ True  True  True  True]]
grad
[[2. 0. 0. 0.]
 [2. 2. 2. 2.]]
pythonout, mask = dropout_forward(x, 0.5, np.random.default_rng(0), training=False)
textout
[[1. 2. 3. 4.]
 [5. 6. 7. 8.]]
mask
[[ True  True  True  True]
 [ True  True  True  True]]

Hints

4 hints

    Solution and notes

    Locked

    Pass every check with Submit to open the reference solution and the notes.

    Run it on your computer

    Clone the repository, install numpy and run the same checks in a terminal:

    python3 scripts/practice/run.py inverted-dropout my_answer.py
    python3 scripts/practice/run.py inverted-dropout my_answer.py --examples

    Start my_answer.py from starter.py.

    Your code

    Python loads on the first run

    Your code is saved in this browser as you type. On a phone you can read and edit the code, but the checks are easier to use on a desktop.

    Keyboard shortcuts
    Ctrl Enter
    Run the examples
    Ctrl Shift Enter
    Submit
    Tab / Shift Tab
    Indent or outdent the lines
    Esc then Tab
    Move the focus out of the editor
    Alt H
    Show the next hint

    On a Mac, use Cmd in place of Ctrl.

    Select Run to check the examples, or Submit to run every check.