Relu或線性整流激活函數是深度學習世界中最常見的激活函數選擇。Relu同時提供了最先進的結果,並且在計算上非常高效。
Relu激活函數的基本概念如下:
Return 0 if the input is negative otherwise return the input as it is.
我們可以用數學方式來表示它:

Relu的偽代碼如下:
if input > 0:
return input
else:
return 0
在本教程中,我們將學習如何實現我們自己的ReLu函數,了解一些它的缺點,並學習關於ReLu的更好版本。
建議閱讀:機器學習的線性代數[第1/2部分]
讓我們開始吧!
在Python中實現ReLu函數
讓我們在Python中寫出我們自己的Relu實現。我們將使用內置的max函數來實現它。
ReLu的代碼如下:
def relu(x):
return max(0.0, x)
為了測試這個函數,讓我們在一些輸入上運行它。
x = 1.0
print('Applying Relu on (%.1f) gives %.1f' % (x, relu(x)))
x = -10.0
print('Applying Relu on (%.1f) gives %.1f' % (x, relu(x)))
x = 0.0
print('Applying Relu on (%.1f) gives %.1f' % (x, relu(x)))
x = 15.0
print('Applying Relu on (%.1f) gives %.1f' % (x, relu(x)))
x = -20.0
print('Applying Relu on (%.1f) gives %.1f' % (x, relu(x)))
完整代碼
完整代碼如下:
def relu(x):
return max(0.0, x)
x = 1.0
print('Applying Relu on (%.1f) gives %.1f' % (x, relu(x)))
x = -10.0
print('Applying Relu on (%.1f) gives %.1f' % (x, relu(x)))
x = 0.0
print('Applying Relu on (%.1f) gives %.1f' % (x, relu(x)))
x = 15.0
print('Applying Relu on (%.1f) gives %.1f' % (x, relu(x)))
x = -20.0
print('Applying Relu on (%.1f) gives %.1f' % (x, relu(x)))
輸出:
Applying Relu on (1.0) gives 1.0
Applying Relu on (-10.0) gives 0.0
Applying Relu on (0.0) gives 0.0
Applying Relu on (15.0) gives 15.0
Applying Relu on (-20.0) gives 0.0
ReLu函數的梯度
讓我們看看ReLu函數的梯度(導數)會是什麼。進行微分後,我們得到以下函數:
f'(x) = 1, x>=0
= 0, x<0
我們可以看到,對於小於零的x值,梯度為0。這意味著某些神經元的權重和偏差不會被更新。這可能是訓練過程中的問題。
為了克服這個問題,我們有Leaky ReLu函數。接下來讓我們了解一下。
Leaky ReLu函數
Leaky ReLu函數是對常規ReLu函數的改進。為了解決負值梯度為零的問題,Leaky ReLu給負輸入提供一個極小的線性成分。
從數學上我們可以表示Leaky ReLu為:
f(x)= 0.01x, x<0
= x, x>=0
數學上:
- f(x)=1 (x<0)
- (αx)+1 (x>=0)(x)
在這裡,a 是一個像我們上面取的0.01這樣的小常數。
圖形上可以表示為:

Leaky ReLu的梯度
讓我們計算Leaky ReLu函數的梯度。梯度可能會是:
f'(x) = 1, x>=0
= 0.01, x<0
在這種情況下,負輸入的梯度是非零的。這意味著所有的神經元都會被更新。
在Python中實現Leaky ReLu
Leaky ReLu的實現如下:
def relu(x):
if x>0 :
return x
else :
return 0.01*x
讓我們嘗試使用一些輸入值。
x = 1.0
print('Applying Leaky Relu on (%.1f) gives %.1f' % (x, leaky_relu(x)))
x = -10.0
print('Applying Leaky Relu on (%.1f) gives %.1f' % (x, leaky_relu(x)))
x = 0.0
print('Applying Leaky Relu on (%.1f) gives %.1f' % (x, leaky_relu(x)))
x = 15.0
print('Applying Leaky Relu on (%.1f) gives %.1f' % (x, leaky_relu(x)))
x = -20.0
print('Applying Leaky Relu on (%.1f) gives %.1f' % (x, leaky_relu(x)))
完整代碼
Leaky ReLu的完整代碼如下:
def leaky_relu(x):
if x>0 :
return x
else :
return 0.01*x
x = 1.0
print('Applying Leaky Relu on (%.1f) gives %.1f' % (x, leaky_relu(x)))
x = -10.0
print('Applying Leaky Relu on (%.1f) gives %.1f' % (x, leaky_relu(x)))
x = 0.0
print('Applying Leaky Relu on (%.1f) gives %.1f' % (x, leaky_relu(x)))
x = 15.0
print('Applying Leaky Relu on (%.1f) gives %.1f' % (x, leaky_relu(x)))
x = -20.0
print('Applying Leaky Relu on (%.1f) gives %.1f' % (x, leaky_relu(x)))
輸出:
Applying Leaky Relu on (1.0) gives 1.0
Applying Leaky Relu on (-10.0) gives -0.1
Applying Leaky Relu on (0.0) gives 0.0
Applying Leaky Relu on (15.0) gives 15.0
Applying Leaky Relu on (-20.0) gives -0.2
結論
{
“error”: “Upstream error…”
}
Source:
https://www.digitalocean.com/community/tutorials/relu-function-in-python