BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / Neural networks9 of 18 in topic

265. Implement inverted dropout

MediumNeural networks
Asked atByteDanceGoogleNVIDIA

Dropout randomly zeros activations during training. Inverted dropout also scales the survivors by 1 / (1 - p) so the expected value stays the same and nothing changes at inference time.

Write dropout(x, p, training, rng). In training, build the keep mask with rng.random(x.shape) >= p, return x * mask / (1 - p). Not training, or p == 0: return x unchanged (as a float array).

Draw exactly one rng.random call of shape x.shape so results match.

Inputs can be Python lists or NumPy arrays, so convert them with np.asarray first.

Functiondef dropout(x, p, training, rng)

Example 1

Input: x = np.ones((2, 4)), p = 0.5, training = True, rng = np.random.default_rng(0)

Output: [[2.0, 0.0, 0.0, 0.0], [2.0, 2.0, 2.0, 2.0]]

Example 2

Input: x = [1.0, 2.0], p = 0.5, training = False, rng = np.random.default_rng(0)

Output: [1.0, 2.0]

Example 3

Input: round(float((np.asarray(dropout(np.ones(20000), 0.3, True, np.random.default_rng(1))) == 0).mean()), 1)

Output: 0.3

Constraints

  • Python 3 with NumPy. Code runs in your browser.
  • Your code must pass all 4 tests to be accepted.
Hint 1

mask = rng.random(x.shape) >= p.

Hint 2

Multiply and divide by 1 - p.

Common mistakes
  • The buggy version forgets the scaling, which shrinks activations during training only.
Understand the idea

Dropout randomly switches off activations during training so the network cannot rely on any single unit, like a team practising with random players missing. Inverted dropout divides the survivors by (1 - p), so the expected value of each activation stays the same and nothing needs to change at test time.

Worked example

x = [2, 4, 6, 8], p = 0.5, and the random draws are [0.7, 0.2, 0.9, 0.4]. Keep where draw >= 0.5, so the mask is [1, 0, 1, 0]. Output = [2, 0, 6, 0] / 0.5 = [4, 0, 12, 0]. On average the kept values make up for the dropped ones. In evaluation mode, or when p = 0, return x unchanged as floats.

mask = rng.random(x.shape) >= p; out = x x mask / (1 - p) in training only

Call rng.random exactly once with the full shape so results are reproducible. Dropout is used in many networks and in some LLM fine-tuning setups.

Topics
Neural networks
Companies3
  • ByteDance the topic appears in interview reports ByteDance Machine Learning Engineer Interview Process
  • Google the topic appears in interview reports Google AI/ML, RAG and agentic AI interview (AI Engineering Insider)
  • NVIDIA the topic appears in interview reports NVIDIA Interview Prep: Questions, Process & Study Plan

From public interview reports and guides. Not affiliated with or endorsed by these companies.

Similar questions
  • Forward pass of a pre-norm residual MLP blockMedium
  • Backpropagate through an embedding lookupMedium
  • Backpropagate through a 2-layer tanh net for regression and binary classificationMedium
  • Trace activation spread through deep ReLU layers for Xavier and He initMedium
All Neural networks problems
Report
265 / 903
Python 3solution.pyPython idle
1
2
3
4
5
6
import numpy as np
​
def dropout(x, p, training, rng):
# your code here
pass
​

Run checks these cases in your browser, no sign-in needed. Submit checks all 4 tests.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS