注:本系列全部博客將持續更新併發布在github上,您能夠經過github下載本系列全部文章筆記文件。html
激活函數是深度學習,亦或者說人工神經網絡中一個十分重要的組成部分,它能夠對神經元的接收信息進行非線性變換,將變換後的信息輸出到下一層神經元。激活函數做用方式以下公式所示:python
其中,$Activation()$就是激活函數。git
爲何要使用激活函數呢?當咱們不用激活函數時,網絡中各層只會根據權重$w$和誤差$b$只會進行線性變換,就算有多層網絡,也只是至關於多個線性方程的組合,依然只是至關於一個線性迴歸模型,解決複雜問題的能力有限。咱們但願咱們的神經網絡可以處理複雜任務,如語言翻譯和圖像分類等,線性變換永遠沒法執行這樣的任務。激活函數得加入能對輸入進行非線性變換,使其可以學習和執行更復雜的任務。github
另外,激活函數使反向傳播成爲可能,由於激活函數的偏差梯度能夠用來調整權重和誤差。若是沒有可微的非線性函數,這就不可能實現。網絡
總之,激活函數的做用是可以給神經網絡加入一些非線性因素,使得神經網絡能夠更好地解決較爲複雜的問題。併發
sigmoid函數能夠將整個實數範圍的的任意值映射到[0,1]範圍內,噹噹輸入值較大時,sigmoid將返回一個接近於1的值,而當輸入值較小時,返回值將接近於0。sigmoid函數數學公式和函數圖像以下所示:app
感覺一下TensorFlow中的sigmoid函數:dom
import tensorflow as tf x = tf.linspace(-5., 5.,6) x
<tf.Tensor: id=3, shape=(6,), dtype=float32, numpy=array([-5., -3., -1., 1., 3., 5.], dtype=float32)>
有兩種方式能夠調用sigmoid函數:ide
tf.keras.activations.sigmoid(x)
<tf.Tensor: id=4, shape=(6,), dtype=float32, numpy= array([0.00669285, 0.04742587, 0.26894143, 0.7310586 , 0.95257413, 0.9933072 ], dtype=float32)>
tf.sigmoid(x)
<tf.Tensor: id=5, shape=(6,), dtype=float32, numpy= array([0.00669285, 0.04742587, 0.26894143, 0.7310586 , 0.95257413, 0.9933072 ], dtype=float32)>
看,$x$中全部值都映射到了[0,1]範圍內。函數
sigmoid優缺點總結:
優勢:輸出的映射區間(0,1)內單調連續,很是適合用做輸出層,而且比較容易求導。
缺點:具備軟飽和性,即當輸入x趨向於無窮的時候,它的導數會趨於0,致使很容易產生梯度消失。
Relu(Rectified Linear Units修正線性單元),是目前被使用最爲頻繁得激活函數,relu函數在x<0時,輸出始終爲0。因爲x>0時,relu函數的導數爲1,即保持輸出爲x,因此relu函數可以在x>0時保持梯度不斷衰減,從而緩解梯度消失的問題,還能加快收斂速度,還能是神經網絡具備稀疏性表達能力,這也是relu激活函數可以被使用在深層神經網絡中的緣由。因爲當x<0時,relu函數的導數爲0,致使對應的權重沒法更新,這樣的神經元被稱爲"神經元死亡"。
relu函數公式和圖像以下:
在TensorFlow中,relu函數的參數狀況比sigmoid複雜,咱們先來看一下:
tf.keras.activations.relu( x, alpha=0.0, max_value=None, threshold=0 )
x = tf.linspace(-5., 5.,6) x
<tf.Tensor: id=9, shape=(6,), dtype=float32, numpy=array([-5., -3., -1., 1., 3., 5.], dtype=float32)>
tf.keras.activations.relu(x)
<tf.Tensor: id=10, shape=(6,), dtype=float32, numpy=array([0., 0., 0., 1., 3., 5.], dtype=float32)>
tf.keras.activations.relu(x,alpha=2.)
<tf.Tensor: id=11, shape=(6,), dtype=float32, numpy=array([-10., -6., -2., 1., 3., 5.], dtype=float32)>
tf.keras.activations.relu(x,max_value=2.) # 大於2部分都將輸出爲2.
<tf.Tensor: id=16, shape=(6,), dtype=float32, numpy=array([0., 0., 0., 1., 2., 2.], dtype=float32)>
tf.keras.activations.relu(x,alpha=2., threshold=3.5) # 小於3.5的值按照alpha * (x - threshold)計算
<tf.Tensor: id=27, shape=(6,), dtype=float32, numpy=array([-17., -13., -9., -5., -1., 5.], dtype=float32)>
softmax函數是sigmoid函數的進化,在處理分類問題是很方便,它能夠將全部輸出映射到成機率的形式,即值在[0,1]範圍且總和爲1。例如輸出變量爲[1.5,4.4,2.0],通過softmax函數激活後,輸出爲[0.04802413, 0.87279755, 0.0791784 ],分別對應屬於一、二、3類的機率。softmax函數數學公式以下:
tf.nn.softmax(tf.constant([[1.5,4.4,2.0]]))
<tf.Tensor: id=29, shape=(1, 3), dtype=float32, numpy=array([[0.04802413, 0.87279755, 0.0791784 ]], dtype=float32)>
tf.keras.activations.softmax(tf.constant([[1.5,4.4,2.0]]))
<tf.Tensor: id=31, shape=(1, 3), dtype=float32, numpy=array([[0.04802413, 0.87279755, 0.0791784 ]], dtype=float32)>
x = tf.random.uniform([1,5],minval=-2,maxval=2) x
<tf.Tensor: id=38, shape=(1, 5), dtype=float32, numpy= array([[ 1.9715171 , 0.49954653, -0.37836075, 1.6178164 , 0.80509186]], dtype=float32)>
tf.keras.activations.softmax(x)
<tf.Tensor: id=39, shape=(1, 5), dtype=float32, numpy= array([[0.42763966, 0.09813169, 0.04078862, 0.30023944, 0.13320053]], dtype=float32)>
tanh函數不管是功能仍是函數圖像上鬥魚sigmoid函數十分類似,因此二者的優缺點也同樣,區別在於tanh函數將值映射到[-1,1]範圍,其數學公式和函數圖像以下:
<tf.Tensor: id=43, shape=(6,), dtype=float32, numpy=array([-5., -3., -1., 1., 3., 5.], dtype=float32)>
tf.keras.activations.tanh(x)
<tf.Tensor: id=44, shape=(6,), dtype=float32, numpy= array([-0.99990916, -0.9950547 , -0.7615942 , 0.7615942 , 0.9950547 , 0.99990916], dtype=float32)>
神經網絡中,隱藏層之間的輸出大多須要經過激活函數來映射(固然,也能夠不用,沒有使用激活函數的層通常稱爲logits層),在構建模型是,須要根據實際數據狀況選擇激活函數。TensorFlow中的激活函數可不止這4個,本文只是介紹最經常使用的4個,固然,其餘激活函數大可能是這幾個激活函數的變種。
原文出處:https://www.cnblogs.com/chenhuabin/p/11711379.html