Problems
Loading...
Step 3 of 6
Dropout Regularization
Easy

Implement inverted dropout using a supplied binary mask. During training, retained values are divided by 1−p1-p so their expected scale is unchanged:

y=x⊙m1−p.y=\frac{x\odot m}{1-p}.

Here, pp is the drop probability and mm is the supplied mask. During evaluation, return x unchanged. Return a new float64 NumPy array.

Loading visualization...

Examples

Input: x = [1,2,3,4], p = 0.5, training = true, mask = [0,1,1,0]

Output: [0,4,6,0]

Explanation: The retained values are multiplied by two because 1 / (1 - 0.5) = 2.

Input: x = [1,-2,3], p = 0.5, training = false, mask = [0,0,0]

Output: [1,-2,3]

Input: x = [[1,2],[3,4]], p = 0.25, training = true, mask = [[1,0],[1,1]]

Output: [[1.333333,0],[4,5.333333]]

Hint 1

Training mode uses x * mask / (1 - p).

Hint 2

Evaluation mode should return x.copy().

Requirements

  • In training mode, multiply x by mask and divide by 1−p1-p.
  • In evaluation mode, return an unchanged copy of x.
  • Do not modify the supplied arrays.
  • Return a float64 NumPy array with the same shape as x.

Constraints

  • x and mask have the same nonempty shape.
  • mask contains only 0 and 1.
  • 0≤p<10\le p<1.
  • training is a Boolean.