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 11p\frac{1}{1-p}to maintain the same expected value.

For each element in the input array:

outputi={0with probability pxi11pwith probability (1p)\text{output}_i = \begin{cases} 0 & \text{with probability } p \\ x_i \cdot \frac{1}{1-p} & \text{with probability } (1-p) \end{cases}

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 11p\frac{1}{1-p} 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