Dropout Regularization

Neural Networks

Difficulty: 4 | Problem written by zeyad_omar
Problem reported in interviews at


Dropout regularization is a technique that is used to avoid overfitting by dropping weights randomly. In this problem, you are required to implement dropout regularization given the weights (2D array) of a neural networkand the probability of dropping the weights out.


p: probability of dropping out

weights: a 2D matrix containg the weights of a neural net where each column represents the weights of a layer. For example, if we have a simple neural net which has two inputs X1 and X2 and two outputs Y1 and Y2, then:

Y1 would be X1 * W11 + X2 * W12

Y2 would be X2 * W21 + X2 * W22

The weights matrix will be:

[ [W11  W21],

[W12  W22]]

Make sure you use random.seed(10) to get the same output as the test cases.

To determine whether a weight will be dropped, only set a weight to 0 if its corresponding position in the below matrix is less than 1-p:

prob_matrix = np.random.random( (np.array(weights).shape) )

Sample Input:
<class 'list'>
weights: [[2.2, 5.7], [6.8, 0.8]]
<class 'float'>
p: 0.4

Expected Output:
<class 'numpy.ndarray'>
[[1.69690542 0. ] [4.308808 0.59904311]]

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Dolores soluta aliquid consectetur ipsam totam iure, porro illo qui ratione dolore aperiam vitae tempora. Molestiae vitae earum illo doloremque quae odit exercitationem laborum, assumenda placeat quisquam eligendi porro provident, odio ut magnam minima fugiat numquam corporis natus magni fugit optio quis, nobis ea distinctio eius accusantium quia placeat asperiores modi minus suscipit non, eaque labore eum itaque.

Id nobis tempore temporibus illo, consequuntur esse minus distinctio quibusdam placeat tempora laborum, vel reiciendis accusamus laborum delectus dolores asperiores? Quibusdam libero eligendi dolores dolorum quo?

Eum alias quasi magnam beatae culpa quidem nihil, voluptatem provident sunt explicabo, aliquid esse cumque eum consequatur odio distinctio similique ratione quia nesciunt facilis, nesciunt tenetur deleniti nulla consequuntur modi minus suscipit fugit tempora dolor repellendus? Similique nobis repudiandae optio doloremque velit, perferendis suscipit quisquam magnam ut soluta necessitatibus repellat eaque voluptatum?

This is a premium feature.
To access this and other such features, click on upgrade below.


Input Test Case

Please enter only one test case at a time
numpy has been already imported as np (import numpy as np)