TorchedUp
LearnBetaProblemsSystem DesignSoonPremium
TorchedUp
LearnBetaProblemsSystem DesignSoonPremium
←

249. Dropout Forward (PyTorch)

Medium

Implement inverted dropout in PyTorch using primitive tensor ops only.

Signature: def dropout_forward(x: torch.Tensor, p: float, seed: int = 0) -> torch.Tensor

The rule: you may NOT call nn.Dropout or F.dropout. Build the Bernoulli mask yourself.

Requirements:

  • Each element zeroed independently with probability p
  • Surviving elements scaled by 1/(1-p) (inverted dropout — no train/inference mismatch)
  • For p == 0.0, return x unchanged (no scaling, no mask)
  • Determinism: use a torch.Generator seeded with seed, and torch.rand(shape, generator=g) to draw uniforms

PyTorch idioms vs NumPy:

  • torch.rand(shape, generator=g) is the rough equivalent of np.random.default_rng(seed).random(shape) — but the underlying RNG (philox/MT19937 vs PCG64) is different, so the masks differ between the two implementations even with the same seed. Tests target the PyTorch RNG; expected values won't match the NumPy version.
  • Build the mask with (torch.rand(...) >= p).to(x.dtype) so it broadcasts and divides cleanly. Don't use bool mask in arithmetic without casting.

Math

y=1−px⊙mask​,maski​∼Bernoulli(1−p)

Related problems

  • Dropout ForwardmediumNumPy

Asked at

NumPy

import numpy as np

 

def dropout_forward(...):

    pass

🔒

Premium problem

Free accounts include problems #1–20. Upgrade to unlock the editor, hidden test cases, and reference solutions for every problem.

Upgrade to PremiumBack to problems

Already premium?