TensorFlow(八)激活函数
TensorFlow提供了多種激活函數
1. sigmoid函數
tf.sigmoid(x, name = None) == tf.nn.sigmoid(x, name = None) # y = 1 / (1 + exp(-x))Computes sigmoid of x element-wise.
Specifically, y = 1 / (1 + exp(-x)).
x: A Tensor with type float, double, int32, complex64, int64, or qint32.
name: A name for the operation (optional).
y=1/(1+exp(?x))y=1/(1+exp(?x))
- sigmoid函數優缺點:
- 優點:
可以把輸入映射到(0, 1)區間,可以用來表示概率(eg:logistic regression)
在物理意義上最為接近生物神經元 - 缺點:
- 梯度消失問題
- 首先明確一點:誤差反向傳播時,梯度包含了f′(zl)和上一層的誤差項(又包含了f′(zl+1):z 為權重加權和)兩個乘法因子,反向傳播推導
- 由于 sigmoid 的導數f′(zl)區間為(0, 0.25],所以其極易落入飽和區,導致梯度非常小,權重接近不變,無法正常更新
- 誤差不斷向底層傳遞的過程中,f′(zl)會呈指數倍增加,而其值域為(0, 0.25],所以梯度越往后傳遞值越小,最終導致權重無法正常更新
- sigmoid的輸出并不是均值為0的,所有輸出數據的大于0,會增加梯度的不穩定性
- 當輸出接近飽和或劇烈變化時,對輸出范圍的這種縮減往往會帶來一些不利影響
2. tanh函數
tf.tanh(x, name = None) == tf.nn.tanh(x, name = None) # y = (exp(x) - exp(-x)) / (exp(x) + exp(-x))Computes hyperbolic tangent of x element-wise. x: A Tensor with type
float, double, int32, complex64, int64, or qint32. name: A name for
the operation (optional).
tanh(x)=sinh(x)/cosh(x)=(exp(x)?exp(?x))/(exp(x)+exp(?x))tanh(x)=sinh(x)/cosh(x)=(exp(x)?exp(?x))/(exp(x)+exp(?x))
- tanh函數的優缺點:
- 優點:
Tanh outputs are zero-centered,把輸入映射到(-1, 1)區間 - 缺點:
雖然 tanh 的導數f′(zl)區間為(0, 1],但仍然會導致梯度消失問題!
3. ReLU函數
tf.nn.relu(features, name=None) # y = max(features, 0)Computes rectified linear: max(features, 0).
features: A Tensor. Must be one of the following types: float32, float64, int32, int64,uint8, int16, int8.
name: A name for the operation (optional).
ReLU(x)=max(0,x)ReLU(x)=max(0,x)
- ReLU函數的優缺點:
- 優點:
- 比 sigmoid/tanh 收斂的更快(6x),creating sparse representations with true zeros( more likely to be linearly separable)
- 其導數在其權重和(z) 大于 0 的時候為 1,從而誤差可以很好的傳播,權重可以正常更新
- 缺點:
- 其導數在其權重和(z) 小于 0 的時候為 0,會導致梯度值為0,從而權重無法正常更新
- 輸出具有偏移現象,即輸出均值恒大于零
- 當使用了較大的學習速率時,易受到飽和的神經元的影響。
4. ReLU函數的演變形式
- Leaky-ReLU
- ReLU6
Computes Rectified Linear 6: min(max(features, 0), 6).
features(x): A Tensor with type float, double, int32, int64, uint8, int16, or int8.
name: A name for the operation (optional).
relu6(x)=min(max(x,0),6)relu6(x)=min(max(x,0),6)
- ELU
5. softplus函數
tf.nn.softplus(features, name=None) # y = log(exp(features) + 1)Computes softplus: log(exp(features) + 1).
features: A Tensor. Must be one of the following types: float32, float64, int32, int64,uint8, int16, int8.
name: A name for the operation (optional).
softplus(x)=log(exp(feature)+1)softplus(x)=log(exp(feature)+1)
6. softsign函數
tf.nn.softsign(features, name=None) # y = features / (abs(features) + 1)
softsign(x)=x/(abs(x)+1)softsign(x)=x/(abs(x)+1)
總結
以上是生活随笔為你收集整理的TensorFlow(八)激活函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: TensorFlow(七)tf.nn库
- 下一篇: TensorFlow(九)eval函数