Inverted Dropout Forward Pass

~18 mincode completion

Implement dropout_forward(x, mask, p, training).

  • x is the activation array (any shape).
  • is a same-shaped array of 1 (keep) and 0 (drop), passed in rather than sampled so the result is reproducible.
  • p is the drop probability, 0 <= p < 1.
  • training is a bool. When , return x unchanged, 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 unchanged

  • Return Array of the same shape as x.

Constraints

  • Allowed library: NumPy only

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~18 min

8 employers weight this skill

4 frontier labs, 2 big tech firms, 1 autonomy company, 1 enterprise vendor. Top match scores 78.

Python
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
Loading docs…

The AI Mentor needs an account

It reads your code and the failing tests and nudges you toward the fix without handing you the answer. Free accounts get it on every problem you're working on today.