Implement Dropout (Training Mode)
Implement Dropout (Training Mode)
Implement dropout for training. Randomly set elements to zero with probability p, then scale the remaining elements by 1−p1to maintain the same expected value.
For each element in the input array:
outputi={0xi⋅1−p1with 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.
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 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
Log in to take notes on this problem
Accepts: array
Accepts: number
Accepts: number
Implement Dropout (Training Mode)
Implement Dropout (Training Mode)
Implement dropout for training. Randomly set elements to zero with probability p, then scale the remaining elements by 1−p1to maintain the same expected value.
For each element in the input array:
outputi={0xi⋅1−p1with 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.
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 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
Log in to take notes on this problem
Accepts: array
Accepts: number
Accepts: number