Inverted dropout
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.
dropout_forward(x, p, rng, training=True)returns(out, mask).dropout_backward(grad_output, mask, p)returns the gradient of the loss with respect tox.
In training mode, build the mask with exactly one call:
True in the mask means kept. Each entry is dropped with probability p. The output is
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
| Name | Type | Shape | Meaning |
|---|---|---|---|
x | float array | any shape | the activations |
p | float | the probability to drop an entry, 0 <= p < 1 | |
rng | np.random.Generator | the random generator for the mask | |
training | bool | True for training mode, False for inference mode | |
grad_output | float array | same as x | the gradient of the loss with respect to out |
mask | bool array | same as x | the mask from dropout_forward |
dropout_forwardreturns a tuple(out, mask).outis a float array andmaskis a bool array. Both have the shape ofx.dropout_backwardreturns a float array with the shape ofx.
Constraints
xhas 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 hintsSolution and notes
LockedPass 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 --examplesStart my_answer.py from starter.py.
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.