Inverted Dropout Forward Pass
Implement dropout_forward(x, mask, p, training).
xis the activation array (any shape).- is a same-shaped array of
1(keep) and0(drop), passed in rather than sampled so the result is reproducible. pis the drop probability,0 <= p < 1.trainingis a bool. When , returnxunchanged, ignoring the mask entirely.- Return an array of the same shape as
x.
Note p = 0: the scale factor is 1/(1-0) = 1, so training and eval agree. That is the sanity check that your scaling is on the right side of the fraction.
Examples
Training with p=0.5: kept units are doubled, dropped units zeroed
- Input
- dropout_forward([1, 2, 3, 4], [1, 0, 1, 0], 0.5, True)
- Output
- [2, 0, 6, 0]
Eval mode returns x unchanged and ignores the mask
- Input
- dropout_forward([1, 2, 3, 4], [1, 0, 1, 0], 0.5, False)
- Output
- [1, 2, 3, 4]
p=0.2 scales kept units by 1/0.8 = 1.25
- Input
- dropout_forward([10, -4, 0, 8], [1, 1, 0, 1], 0.2, True)
- Output
- [12.5, -5, 0, 10]
Hints
Hint 1
Convert the input with before doing elementwise work.
Hint 2
A common slip here: multiplied by one minus p instead of dividing.
Requirements
x: activation array: same-shaped array of 1 (keep) / 0 (drop)
p: drop probability in [0, 1)training: if False, return x unchangedReturn Array of the same shape as x.
Constraints
Allowed library: NumPy only
Time limit: 200 ms, Memory: 64 MB
Where this shows up
8 employers weight this skill
4 frontier labs, 2 big tech firms, 1 autonomy company, 1 enterprise vendor. Top match scores 78.
import numpy as np
def dropout_forward(x: np.ndarray, mask: np.ndarray, p: float,
training: bool) -> np.ndarray:
"""
Apply inverted dropout.
Args:
x: activation array
mask: same-shaped array of 1 (keep) / 0 (drop)
p: drop probability in [0, 1)
training: if False, return x unchanged
Returns:
Array of the same shape as x.
"""
# YOUR CODE HERE
pass