# Week1 - Programming Assignment: Regularization - dropout code

**URL:** <https://community.deeplearning.ai/t/week1-programming-assignment-regularization-dropout-code/5829>\
**Category:** Improving Deep Neural Networks: Hyperparameter tun\
**Tags:** coursera-platform\
**Created:** [May 14, 2021, 12:49pm UTC](https://community.deeplearning.ai/t/week1-programming-assignment-regularization-dropout-code/5829 "2021-05-14T12:49:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![manish\_tiwari](https://avatars.discourse-cdn.com/v4/letter/m/eb8c5e/32.png) [@manish\_tiwari](https://community.deeplearning.ai/u/manish_tiwari)\
**Post date:** [May 14, 2021, 12:49pm UTC](https://community.deeplearning.ai/t/week1-programming-assignment-regularization-dropout-code/5829/1 "2021-05-14T12:49:53Z")

</div>

The dropout is supposed to drop nodes in the hidden layer randomly for each iteration. Are we not supposed to drop the same node for all m inputs in the training set?

The programming exercise required me to initialized the dropout matrix for the first hidden layer as follows:  
D1 = np.random.rand(A1.shape[0],A1.shape[1])  
D1 = (D1 \< keep\_prob).astype(int)

This would cause different nodes to be dropped for the “m” inputs. If we should drop the same node for all m examples, we should initialize D1 as a Vector of shape (A1.shape[0], 1) and use broadcasting to multiple A1 and D1.

I tried to run my code by initializing D1 as a vector and using broadcasting, but the test cases were failing.

Please clarify.

Thanks,  
Manish

---

<div class="post-metadata">

**Author:** ![javier](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/javier/32/47_2.png) [@javier](https://community.deeplearning.ai/u/javier)\
**Post date:** [May 14, 2021, 1:23pm UTC](https://community.deeplearning.ai/t/week1-programming-assignment-regularization-dropout-code/5829/2 "2021-05-14T13:23:19Z")

</div>

Hi @manish_tiwari , the idea in dropout is turn off nodes randomly for each sample.

If you turn of the same nodes for all m inputs, you will be optimizing a different NN, a smaller one. So your NN performance will decrease significantly.

Even if you change the dropout nodes once per epoch, the end result will be not as good as selecting the random nodes for each example.

---

<div class="post-metadata">

**Author:** ![taohaoxiong](https://avatars.discourse-cdn.com/v4/letter/t/7feea3/32.png) [@taohaoxiong](https://community.deeplearning.ai/u/taohaoxiong)\
**Post date:** [November 21, 2021, 1:46pm UTC](https://community.deeplearning.ai/t/week1-programming-assignment-regularization-dropout-code/5829/3 "2021-11-21T13:46:03Z")

</div>

I have have the same question as @manish_tiwari, since it mentioned “When you shut some neurons down, you actually modify your model” in the “Regularization” assignment description.

Regarding the performance, I tried to implement using:  
D1 = np.random.rand(A1.shape[0],1)  
D2 = np.random.rand(A2.shape[0],1)

by using keep\_prob = 0.9, learning\_rate = 0.3, the result is actually not worse with:  
On the train set:  
Accuracy: 0.933649289099526  
On the test set:  
Accuracy: 0.955

---

<div class="post-metadata">

**Author:** ![paulinpaloalto](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/paulinpaloalto/32/187_2.png) [@paulinpaloalto](https://community.deeplearning.ai/u/paulinpaloalto)\
**Post date:** [April 1, 2022, 7:40pm UTC](https://community.deeplearning.ai/t/week1-programming-assignment-regularization-dropout-code/5829/4 "2022-04-01T19:40:54Z")

</div>

Yes, this issue has been noticed and discussed a number of times. [Here’s a thread](https://community.deeplearning.ai/t/inverted-dropout-killing-nodes-or-stabbing-training-examples/49593/2) which shows more investigations which come to the same conclusion that you show: it probably doesn’t make all that much difference if you do the dropout differently per sample or consistently per sample in a given minibatch.
