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:
p1/(1-p) (inverted dropout — no train/inference mismatch)p == 0.0, return x unchanged (no scaling, no mask)torch.Generator seeded with seed, and torch.rand(shape, generator=g) to draw uniformsPyTorch 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.(torch.rand(...) >= p).to(x.dtype) so it broadcasts and divides cleanly. Don't use bool mask in arithmetic without casting.Math
Related problems
Asked at
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.
Already premium?