Neural Mastery
← Practice
Inverted Dropout Forward Passeasy

Inverted Dropout Forward Pass

easy Neural Networks by Hand 15 min

NumPy allowed · plain Python earns 10 bonus points

Apply inverted dropout regularization during training to prevent co-adaptation of feature representations.

Task

Implement dropout_forward(x, p_drop, mask): given activations x, dropout probability p_drop, and binary keep-mask mask (where 1 means keep, 0 means drop), compute out[i][j] = (x[i][j] * mask[i][j]) / (1.0 - p_drop). Scaling during training ensures that no scaling is required at test time. Round each element to 4 decimal places.

Function Signature

dropout_forward(x: list[list[float]], p_drop: float, mask: list[list[int]]) -> list[list[float]]

Examples

Example 1: Dropout p=0.5 with alternating mask
Input: x = [[1, 2], [3, 4]] p_drop = 0.5 mask = [[1, 0], [0, 1]]
Output: [[2, 0], [0, 8]]
Example 2: No drop p=0.0
Input: x = [[1.5, -2]] p_drop = 0 mask = [[1, 1]]
Output: [[1.5, -2]]
Example 3: High drop rate p=0.8
Input: x = [[10, 20, 30]] p_drop = 0.8 mask = [[0, 1, 0]]
Output: [[0, 100, 0]]

Constraints

  • 0.0 <= p_drop < 1.0
  • x and mask have identical 2D dimensions (N, D)
  • 1 <= N, D <= 100
Python3Saved ✓
def dropout_forward(x: list[list[float]], p_drop: float, mask: list[list[int]]) -> list[list[float]]:
"""Execute inverted dropout forward pass using a provided binary mask.

Args:
x: 2D input activations of shape (N, D).
p_drop: Probability of dropping a unit (0.0 <= p_drop < 1.0).
mask: 2D binary integer mask (1 = keep, 0 = drop) matching shape of x.

Returns:
Scaled activations matrix of shape (N, D) rounded to 4 decimals.
"""
pass

Case 1: Dropout p=0.5 with alternating mask
Input: x = [[1, 2], [3, 4]] p_drop = 0.5 mask = [[1, 0], [0, 1]]
Expected: [[2, 0], [0, 8]]
Case 2: No drop p=0.0
Input: x = [[1.5, -2]] p_drop = 0 mask = [[1, 1]]
Expected: [[1.5, -2]]
Case 3: High drop rate p=0.8
Input: x = [[10, 20, 30]] p_drop = 0.8 mask = [[0, 1, 0]]
Expected: [[0, 100, 0]]