8個(gè)深度學(xué)習(xí)中常用的激活函數(shù)
激活函數(shù),又稱轉(zhuǎn)換函數(shù),是設(shè)計(jì)神經(jīng)網(wǎng)絡(luò)的關(guān)鍵。激活函數(shù)在某種意義上是重要的,因?yàn)樗挥脕泶_定神經(jīng)網(wǎng)絡(luò)的輸出。它將結(jié)果值映射為0到1或-1到1等(取決于函數(shù))。激活函數(shù)還有另一個(gè)名稱,稱為Squashing函數(shù),當(dāng)限制了激活函數(shù)的范圍時(shí)使用這個(gè)名稱。激活函數(shù)應(yīng)用于神經(jīng)網(wǎng)絡(luò)的每個(gè)節(jié)點(diǎn),并決定該神經(jīng)元是否應(yīng)該被“觸發(fā)”/“激活”。
為什么選擇激活函數(shù)是非常重要的。
當(dāng)在隱藏層和輸出層中實(shí)現(xiàn)時(shí),激活函數(shù)的選擇非常關(guān)鍵。模型的準(zhǔn)確性和損失很大程度上依賴于激活函數(shù)。此外,必須根據(jù)您對(duì)模型的期望來選擇它們。例如,在二值分類問題中,sigmoid函數(shù)是一種最優(yōu)選擇。
激活函數(shù)類型。大致可分為兩類:
線性激活函數(shù)。
非線性激活函數(shù)。
為了方便展示我們導(dǎo)入如下庫(kù):
- import math as m
- import matplotlib.pyplot as plt
- import numpy as np
- import tensorflow as tf
- from tensorflow import keras
- from tensorflow.keras import layers
Sigmoid
sigmoid激活函數(shù)也稱為logistic函數(shù)。Sigmoid函數(shù)在回歸分類問題中非常流行。sigmoid函數(shù)給出的值的范圍是0和1。
- def sigmoid(x):
- return 1 / (1 + m.exp(-x))values_of_sigmoid = []
- values_of_x = []
- for i in range(-500,500,1):
- i = i*0.01
- values_of_x.append(i)
- values_of_sigmoid.append(sigmoid(i))plt.plot( values_of_x ,values_of_sigmoid)
- plt.xlabel("values of x")
- plt.ylabel("value of sigmoid")

tanH
這個(gè)函數(shù)非常類似于sigmoid激活函數(shù)。這個(gè)函數(shù)在-1到1的范圍內(nèi)接受任何實(shí)值作為輸入和輸出值。輸入越大(越正),輸出值越接近1.0,而輸入越小(越負(fù)),輸出越接近-1.0。Tanh激活函數(shù)計(jì)算如下。
- def tanh(x):
- return (m.exp(x) - m.exp(-x)) / (m.exp(x) + m.exp(-x))values_of_tanh = []
- values_of_x = []
- for i in range(-500,500,1):
- i = i*0.001
- values_of_x.append(i)
- values_of_tanh.append(tanh(i))plt.plot( values_of_x ,values_of_tanh)
- plt.xlabel("values of x")
- plt.ylabel("value of tanh")

Softmax
Softmax激活函數(shù)輸出一個(gè)和為1.0的值向量,可以解釋為類隸屬度的概率。Softmax是argmax函數(shù)的“軟”版本,它允許一個(gè)“贏家通吃”函數(shù)的似然輸出。
- def softmax(x):
- e_x = np.exp(x - np.max(x))
- return e_x / e_x.sum()values_of_x = [i*0.01 for i in range(-500,500)]
- plt.plot(scores ,softmax(values_of_x))
- plt.xlabel("values of x")
- plt.ylabel("value of softmax")

RELU 線性整流單元
ReLU可能是用于隱藏層的最常見的函數(shù)。它還可以有效地克服其他以前流行的激活函數(shù)(如Sigmoid和Tanh)的限制。具體來說,它不太容易受到阻止深度模型被訓(xùn)練的梯度下降消失問題的影響,盡管它可能會(huì)遇到諸如飽和單元等其他問題。
- def ReLU(x):
- return max(0,x)values_of_relu = []
- values_of_x = []
- for i in range(-500,500,1):
- i = i*0.01
- values_of_x.append(i)
- values_of_relu.append(ReLU(i))plt.plot(values_of_x,values_of_relu)

Leaky ReLU
ReLU的問題:當(dāng)給ReLU一個(gè)負(fù)值時(shí),它立即變成零,這降低了模型合適地?cái)M合或從數(shù)據(jù)訓(xùn)練的能力。這意味著ReLU激活函數(shù)的任何負(fù)輸入都會(huì)在圖中立即將該值轉(zhuǎn)換為零,這反過來又會(huì)通過不適當(dāng)?shù)赜成湄?fù)值而影響結(jié)果圖。
為了克服這個(gè)問題,Leaky ReLU被引入。
- def leaky_ReLU(x):
- return max(0.1*x,x)values_of_L_relu = []
- values_of_x = []
- for i in range(-500,500,1):
- i = i*0.01
- values_of_x.append(i)
- values_of_L_relu.append(leaky_ReLU(i))plt.plot(values_of_x,values_of_L_relu)

下面幾個(gè)函數(shù)都是RELU的變體基本上都是與Leaky 類似優(yōu)化了激活函數(shù)負(fù)值時(shí)的返回
ELU
- activation_elu = layers.Activation(‘elu’)x = tf.linspace(-3.0, 3.0, 100)
- y = activation_elu(x) # once created, a layer is callable just like a functionplt.figure(dpi=100)
- plt.plot(x, y)
- plt.xlim(-3, 3)
- plt.xlabel(“Input”)
- plt.ylabel(“Output”)
- plt.show()

SELU
- activation_selu = layers.Activation('selu')x = tf.linspace(-3.0, 3.0, 100)
- y = activation_selu(x) # once created, a layer is callable just like a functionplt.figure(dpi=100)
- plt.plot(x, y)
- plt.xlim(-3, 3)
- plt.xlabel("Input")
- plt.ylabel("Output")
- plt.show()

Swish
- activation_swish = layers.Activation(‘swish’)x = tf.linspace(-3.0, 3.0, 100)
- y = activation_swish(x) # once created, a layer is callable just like a functionplt.figure(dpi=100)
- plt.plot(x, y)
- plt.xlim(-3, 3)
- plt.xlabel(“Input”)
- plt.ylabel(“Output”)
- plt.show()

總結(jié)
常用于隱藏層激活函數(shù):
一般遞歸神經(jīng)網(wǎng)絡(luò)使用Tanh或sigmoid激活函數(shù),甚至兩者都使用。例如,LSTM通常對(duì)循環(huán)連接使用Sigmoid激活,對(duì)輸出使用Tanh激活。
1.多層感知器(MLP): ReLU激活函數(shù)。
2.卷積神經(jīng)網(wǎng)絡(luò)(CNN): ReLU激活函數(shù)。
3.遞歸神經(jīng)網(wǎng)絡(luò):Tanh和/或Sigmoid激活函數(shù)。
如果你不確定使用哪個(gè)激活函數(shù),你肯定可以嘗試不同的組合,并尋找最適合的,但是可以從RELU開始
輸出層激活功能:
輸出層激活函數(shù)必須根據(jù)你要解決的問題類型來選擇。例如,如果你有一個(gè)線性回歸問題,那么線性激活函數(shù)將是有用的。下面是您可能面臨的一些常見問題和使用的激活函數(shù)。
二進(jìn)制分類:一個(gè)節(jié)點(diǎn),sigmoid激活。
多類分類:每個(gè)類一個(gè)節(jié)點(diǎn),softmax激活。
多標(biāo)簽分類:每個(gè)類一個(gè)節(jié)點(diǎn),sigmoid激活。
以下是一些常用激活函數(shù)的公式和可視化顯示,希望對(duì)你有幫助
