# 1.6 dropout 正则化（Dropout Regularization）

Dropout是指在深度学习网络的训练过程中，对于每层的神经元，按照一定的概率将其暂时从网络中丢弃。即每次训练时，每一层都有部分神经元不工作，起到简化复杂网络模型的效果，从而避免发生过拟合

![](/files/-Le0cvS4M7rvL3T-zxq9)

## Inverted dropout（反向随机失活）

假设对于第$$l$$层神经元，设定保留神经元比例概率keep\_prob=0.8，即该层有20%的神经元停止工作。$$dl$$为dropout向量，设置$$dl$$为随机vector，其中80%的元素为1，20%的元素为0。

生成dropout vector：

```python
dl = np.random.rand(al.shape[0],al.shape[1])<keep_prob
```

第$$l$$层经过dropout，随机删减20%的神经元，只保留80%的神经元，其输出为：

```python
al = np.multiply(al,dl)
```

最后，对$$al$$进行scale up处理，即：

```python
al /= keep_prob
```

对$$al$$进行scale up是为了保证在经过dropout后，$$al$$作为下一层神经元的输入值尽量保持不变,尽可能保持$$al$$的期望值相比之前没有大的变化

Inverted dropout的另外一个好处就是在对该dropout后的神经网络进行测试时能够减少scaling问题。因为在训练时，使用scale up保证$$al$$的期望值没有大的变化，测试时就不需要再对样本数据进行类似的尺度伸缩操作

对于$$m$$个样本，单次迭代训练时，随机删除掉隐藏层一定数量的神经元；然后，在删除后的剩下的神经元上正向和反向更新权重$$w$$和常数项$$b$$；接着，下一次迭代中，再恢复之前删除的神经元，重新随机删除一定数量的神经元，进行正向和反向更新$$w$$和$$b$$。不断重复上述过程，直至迭代训练完成

使用dropout训练结束后，在测试和实际应用模型时，不需要进行dropout和随机删减神经元，所有的神经元都在工作


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://baozoulin.gitbook.io/neural-networks-and-deep-learning/di-er-men-ke-gai-shan-shen-ceng-shen-jing-wang-luo-chao-can-shu-tiao-shi-zheng-ze-hua-yi-ji-you-hua/improving-deep-neural-networks/practical-aspects-of-deep-learning/16-dropout-zheng-ze-hua-ff08-dropout-regularization.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
