Inverted Dropout Forward
Problem Statement
Implement the forward pass of Inverted Dropout.
Background
Dropout is a regularization technique that randomly "drops" neurons during training to prevent overfitting. In Inverted Dropout:
- Each neuron is dropped with probability (drop_prob)
- Remaining neurons are scaled by to keep expected values the same
- During inference (test mode), no dropout is applied
The scaling ensures that expected output values are consistent between training and testing.
Your Task
Write a function dropout_forward(activations, drop_prob, mask, train_mode=True) that:
- If train_mode=False: return activations unchanged
- If train_mode=True: apply the given mask (0s and 1s) and scale by 1/(1-drop_prob)
The mask is provided as input (list of 0s and 1s) to make testing deterministic.
Output Format
Return a list of values after applying dropout. Round each value to 4 decimal places.
Example
activations=[10, 20, 30], drop_prob=0.5, mask=[1, 0, 1], train_mode=True
[20.0, 0, 60.0]
Scale = 1/(1-0.5) = 2. First: ×2=20. Second: =0. Third: ×2=60.
Constraints
- 0 < drop_prob < 1
- len(activations) == len(mask)
- mask contains only 0s and 1s
- List length: 1 to 100 elements