寫在前面:

  1. 這僅僅是自己的學習筆記,如果侵權,還請告知;
  2. 講義是參照杜客等人對cs231n的中文翻譯。

對於神經網路的正則化懲罰,方法主要有L2正則化、L1正則化、最大範式約束(Max norm constraints)以及隨機失活(Dropout)。

首先簡要介紹其他正則化方法:

(1)L2正則化:可能是最常用的正則化方法。對於每個權重 W_{i} ,在目標函數中加入 frac{1}{2}lambda W_i^{2} ,則關於梯度則是 lambda W_{i}

(2)L1正則化:對於每個 W_{i} ,在目標函數中加入 lambda left| W_{i} 
ight| ,L2正則化與L1正則化可以混用。

一般來說L2正則化效果比L1正則化效果好。

(3)最大範式約束(Max norm constraints)

(4)隨機失活(Dropout):簡單又及其有效。

與L1正則化,L2正則化和最大範式約束等方法互為補充。在訓練的時候,隨機失活的實現方法是讓神經元以超參數p的概率被激活或者被設置為0。

過擬合是深度神經網路(DNN)的常見問題:模型只學會在訓練數據集上進行分類。

dropout的思想是訓練整體DNN,並平均整個集合的結果。而不是訓練單個的DNN。DNNs是以概率p捨棄部分神經元,其他神經元以概率q=1-p被保留,捨棄的神經元的輸出都被設置為0。(詳見深度學習網路大殺器之dropout——深入解析dropout)

Dropout在實踐中能很好工作是因為其在訓練階段阻止神經元的共適應。

在實際應用中,一般採用反向隨機失活(Inverted Dropout)。即在訓練階段對數值進行所放,測試階段保持不變。

詳細代碼如下:

前向傳播:

def dropout_forward(x, dropout_param):
"""
Performs the forward pass for (inverted) dropout.

Inputs:
- x: Input data, of any shape
- dropout_param: A dictionary with the following keys:
- p: Dropout parameter. We drop each neuron output with probability p.
- mode: test or train. If the mode is train, then perform dropout;
if the mode is test, then just return the input.
- seed: Seed for the random number generator. Passing seed makes this
function deterministic, which is needed for gradient checking but not
in real networks.

Outputs:
- out: Array of the same shape as x.
- cache: tuple (dropout_param, mask). In training mode, mask is the dropout
mask that was used to multiply the input; in test mode, mask is None.
"""
p, mode = dropout_param[p], dropout_param[mode]
if seed in dropout_param:
np.random.seed(dropout_param[seed])

mask = None
out = None

if mode == train:
#######################################################################
# TODO: Implement training phase forward pass for inverted dropout. #
# Store the dropout mask in the mask variable. #
#######################################################################
pass
mask=(np.random.rand(*x.shape)<p)/p ##以某一概率隨機失活
out=x * mask
#######################################################################
# END OF YOUR CODE #
#######################################################################
elif mode == test:
#######################################################################
# TODO: Implement the test phase forward pass for inverted dropout. #
#######################################################################
pass
out=x
#######################################################################
# END OF YOUR CODE #
#######################################################################
cache = (dropout_param, mask)
out = out.astype(x.dtype, copy=False)
return out, cache

反向傳播時代碼:

def dropout_backward(dout, cache):
"""
Perform the backward pass for (inverted) dropout.

Inputs:
- dout: Upstream derivatives, of any shape
- cache: (dropout_param, mask) from dropout_forward.
"""
dropout_param, mask = cache
mode = dropout_param[mode]

dx = None
if mode == train:
#######################################################################
# TODO: Implement training phase backward pass for inverted dropout #
#######################################################################
pass
dx=dout*mask
#######################################################################
# END OF YOUR CODE #
#######################################################################
elif mode == test:
dx = dout
return dx

推薦閱讀:

相關文章