TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

Implement Dropout (Training Mode)

Neural Networks
Medium

Implement inverted dropout for a NumPy array. During training, independently drop each element with probability p. Scale every retained element by 1/(1−p)1 / (1 - p)1/(1−p) so the expected output magnitude is unchanged.

Use rng.random(x.shape) when an rng generator is provided. Otherwise, use np.random.random(x.shape). The test panel displays a seed; the runner creates np.random.default_rng(seed) and passes that generator as rng.

Return (output, dropout_pattern), where dropout_pattern is the scaled mask applied to the input. Its entries are 0 for dropped elements and 1/(1−p)1 / (1 - p)1/(1−p) for retained elements.

Loading visualization...

Examples

Input: x = [[1, 2], [3, 4]], p = 0.5, seed = 123

Output: ([[0.0, 4.0], [6.0, 8.0]], [[0.0, 2.0], [2.0, 2.0]])

Explanation: The seeded generator drops the first element. Retained elements are multiplied by 2 because p = 0.5.

Input: x = [[1, 2], [3, 4]], p = 0.0, seed = 7

Output: ([[1.0, 2.0], [3.0, 4.0]], [[1.0, 1.0], [1.0, 1.0]])

Hint 1

Generate one random value per input element and retain positions below 1 - p.

Hint 2

Build the pattern with 1 / (1 - p) at retained positions and zero elsewhere.

Requirements

  • Convert x to a NumPy array without modifying the input
  • Generate one random value for every input element
  • Drop elements with probability p and scale retained elements by 1 / (1 - p)
  • Return (output, dropout_pattern) as two NumPy arrays with the same shape as x

Constraints

  • Fully vectorized (no Python loops)
  • Input array up to shape (1000, 1000)
  • 0.0 ≤ p < 1.0
  • Use NumPy only
  • Time limit: 200 ms
Try Similar Problems
Batch NormalizationHe InitializationXavier InitializationLinear Layer ForwardGradient Clipping

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: number

Accepts: number

You must run your code first.
PrevNext

Implement Dropout (Training Mode)

Neural Networks
Medium

Implement inverted dropout for a NumPy array. During training, independently drop each element with probability p. Scale every retained element by 1/(1−p)1 / (1 - p)1/(1−p) so the expected output magnitude is unchanged.

Use rng.random(x.shape) when an rng generator is provided. Otherwise, use np.random.random(x.shape). The test panel displays a seed; the runner creates np.random.default_rng(seed) and passes that generator as rng.

Return (output, dropout_pattern), where dropout_pattern is the scaled mask applied to the input. Its entries are 0 for dropped elements and 1/(1−p)1 / (1 - p)1/(1−p) for retained elements.

Loading visualization...

Examples

Input: x = [[1, 2], [3, 4]], p = 0.5, seed = 123

Output: ([[0.0, 4.0], [6.0, 8.0]], [[0.0, 2.0], [2.0, 2.0]])

Explanation: The seeded generator drops the first element. Retained elements are multiplied by 2 because p = 0.5.

Input: x = [[1, 2], [3, 4]], p = 0.0, seed = 7

Output: ([[1.0, 2.0], [3.0, 4.0]], [[1.0, 1.0], [1.0, 1.0]])

Hint 1

Generate one random value per input element and retain positions below 1 - p.

Hint 2

Build the pattern with 1 / (1 - p) at retained positions and zero elsewhere.

Requirements

  • Convert x to a NumPy array without modifying the input
  • Generate one random value for every input element
  • Drop elements with probability p and scale retained elements by 1 / (1 - p)
  • Return (output, dropout_pattern) as two NumPy arrays with the same shape as x

Constraints

  • Fully vectorized (no Python loops)
  • Input array up to shape (1000, 1000)
  • 0.0 ≤ p < 1.0
  • Use NumPy only
  • Time limit: 200 ms
Try Similar Problems
Batch NormalizationHe InitializationXavier InitializationLinear Layer ForwardGradient Clipping

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: number

Accepts: number

You must run your code first.