TensorTonicTensorTonic
Problems
Study PlansProjectsInterviewPricingFeedback
Problems
Loading...
1 / 1

Implement Dropout (Training Mode)

Neural Networks
Medium

Implement dropout for training. Randomly set elements to zero with probability p, then scale the remaining elements by 11−p\frac{1}{1-p}1−p1​to maintain the same expected value.

For each element in the input array:

outputi={0with probability pxi⋅11−pwith probability (1−p)\text{output}_i = \begin{cases} 0 & \text{with probability } p \\ x_i \cdot \frac{1}{1-p} & \text{with probability } (1-p) \end{cases}outputi​={0xi​⋅1−p1​​with probability pwith probability (1−p)​

The scaling ensures the expected value stays the same.

Use rng.random() if rng provided, else np.random.random() for randomness.

Return tuple (output, dropout_pattern) where dropout_pattern shows which elements were kept/dropped.

Loading visualization...

Examples

Input: x=[1., 2., 3.], p=0.0 (no dropout)

Output: ([1., 2., 3.], [1., 1., 1.])

Input: x=[2., 4.], p=0.5 (50% dropout, random)

Output: ([0., 8.], [0., 2.]) or ([4., 0.], [2., 0.])

Hint 1

Generate random values between 0 and 1. If random_value < (1-p), keep the element and scale it, otherwise set to 0.

Hint 2

Create a pattern array showing 0 for dropped elements and 11−p\frac{1}{1-p}1−p1​ for kept elements. Multiply input by this pattern.

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 dropout for training. Randomly set elements to zero with probability p, then scale the remaining elements by 11−p\frac{1}{1-p}1−p1​to maintain the same expected value.

For each element in the input array:

outputi={0with probability pxi⋅11−pwith probability (1−p)\text{output}_i = \begin{cases} 0 & \text{with probability } p \\ x_i \cdot \frac{1}{1-p} & \text{with probability } (1-p) \end{cases}outputi​={0xi​⋅1−p1​​with probability pwith probability (1−p)​

The scaling ensures the expected value stays the same.

Use rng.random() if rng provided, else np.random.random() for randomness.

Return tuple (output, dropout_pattern) where dropout_pattern shows which elements were kept/dropped.

Loading visualization...

Examples

Input: x=[1., 2., 3.], p=0.0 (no dropout)

Output: ([1., 2., 3.], [1., 1., 1.])

Input: x=[2., 4.], p=0.5 (50% dropout, random)

Output: ([0., 8.], [0., 2.]) or ([4., 0.], [2., 0.])

Hint 1

Generate random values between 0 and 1. If random_value < (1-p), keep the element and scale it, otherwise set to 0.

Hint 2

Create a pattern array showing 0 for dropped elements and 11−p\frac{1}{1-p}1−p1​ for kept elements. Multiply input by this pattern.

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.