Implement inverted dropout forward and backward passes in NumPy

Quick Overview

Implement inverted dropout in NumPy, with a seeded forward pass that returns the output and its keep mask and a backward pass that returns the input gradient. Tests expectation-preserving scaling, the edge cases where p is 0 or 1, eval-mode behavior, and reuse of the mask in the gradient.

Implement inverted dropout forward and backward passes in NumPy

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `dropout_forward(x, p, seed, training=True)` returning `(out, mask)` for inverted dropout with drop probability `p`, and `dropout_backward(dout, mask, p)` returning the gradient with respect to `x`. Requirements: - Training: draw `u = np.random.default_rng(seed).random(x.shape)` (one call) and keep a unit when `u < 1 - p`. `mask` is a boolean array of shape `x.shape`. - Kept units are scaled by `1 / (1 - p)`, and dropped units are `0`. - With `training=False` or `p == 0`, the output equals `x` and `mask` is all `True`. - With `p == 1`, the output, mask and gradient are all zeros or `False`, with no division by zero. - A `p` outside $[0, 1]$ raises `ValueError`. - Backward returns `dout / (1 - p)` where `mask` is `True`, and `0` elsewhere. Example: ```python x = np.array([2.0, -4.0]) out, mask = dropout_forward(x, 0.5, seed=seed, training=True) # seed: any integer # If the draw gives mask == [True, False], then out == [4.0, 0.0] dropout_backward(np.array([1.0, 1.0]), mask, 0.5) # -> [2.0, 0.0] dropout_forward(x, 0.5, seed=seed, training=False) # -> (array([ 2., -4.]), array([ True, True])) ``` ```hint Where the gradient comes from Fix the mask and write one unit's output as a function of its input. Its slope on a kept unit and on a dropped unit tells you what backward needs from forward. ``` ```hint Order of the checks The scale factor has $1 - p$ in its denominator. Decide which cases return early before that factor is ever computed. ``` ### What a Strong Answer Covers - One seeded draw of shape `x.shape` with the keep test `u < 1 - p`, so the mask is reproducible and each unit survives with probability ${1 - p}$. - Scaling kept units by ${1 / (1 - p)}$ so the expected training output equals `x`, with eval mode and `p == 0` as the identity. - Handling `p == 1` before any division, and rejecting values of `p` outside $[0, 1]$. - A backward pass that reuses the forward mask and scale, and why that is the exact gradient. ### Follow-up Questions - Why scale at training time instead of multiplying by `1 - p` at inference? - How would you avoid storing the full boolean mask for very large activations? - How does dropout interact with batch normalization?

Overview: Implement inverted dropout in NumPy, with a seeded forward pass that returns the output and its keep mask and a backward pass that returns the input gradient. Tests expectation-preserving scaling, the edge cases where p is 0 or 1, eval-mode behavior, and reuse of the mask in the gradient.

PracHub logo
General
Nov 25, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Implement dropout_forward(x, p, seed, training=True) returning (out, mask) for inverted dropout with drop probability p, and dropout_backward(dout, mask, p) returning the gradient with respect to x.

Requirements:

  • Training: draw u = np.random.default_rng(seed).random(x.shape) (one call) and keep a unit when u < 1 - p . mask is a boolean array of shape x.shape .
  • Kept units are scaled by 1 / (1 - p) , and dropped units are 0 .
  • With training=False or p == 0 , the output equals x and mask is all True .
  • With p == 1 , the output, mask and gradient are all zeros or False , with no division by zero.
  • A p outside [0,1][0, 1] raises ValueError .
  • Backward returns dout / (1 - p) where mask is True , and 0 elsewhere.

Example:

x = np.array([2.0, -4.0])
out, mask = dropout_forward(x, 0.5, seed=seed, training=True)  # seed: any integer
# If the draw gives mask == [True, False], then out == [4.0, 0.0]
dropout_backward(np.array([1.0, 1.0]), mask, 0.5)  # -> [2.0, 0.0]

dropout_forward(x, 0.5, seed=seed, training=False)
# -> (array([ 2., -4.]), array([ True,  True]))

What a Strong Answer Covers Guidance

  • One seeded draw of shape x.shape with the keep test u < 1 - p , so the mask is reproducible and each unit survives with probability 1−p{1 - p} .
  • Scaling kept units by 1/(1−p){1 / (1 - p)} so the expected training output equals x , with eval mode and p == 0 as the identity.
  • Handling p == 1 before any division, and rejecting values of p outside [0,1][0, 1] .
  • A backward pass that reuses the forward mask and scale, and why that is the exact gradient.

Follow-up Questions Guidance

  • Why scale at training time instead of multiplying by 1 - p at inference?
  • How would you avoid storing the full boolean mask for very large activations?
  • How does dropout interact with batch normalization?