国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

【學習經(jīng)驗分享NO.16】超全代碼-python畫Sigmoid,ReLU,Tanh等十多種激活函數(shù)曲線及其梯度曲線(持續(xù)更新)

這篇具有很好參考價值的文章主要介紹了【學習經(jīng)驗分享NO.16】超全代碼-python畫Sigmoid,ReLU,Tanh等十多種激活函數(shù)曲線及其梯度曲線(持續(xù)更新)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。


前言

激活函數(shù)是一種特殊的非線性函數(shù),它能夠在神經(jīng)網(wǎng)絡中使用,其作用是將輸入信號轉化成輸出信號。它將神經(jīng)元中的輸入信號轉換為一個有意義的輸出,從而使得神經(jīng)網(wǎng)絡能夠學習和識別復雜的模式。常用的激活函數(shù)有 Sigmoid、ReLU、Leaky ReLU 和 ELU 等。大論文理論部分需要介紹激活函數(shù),需要自己貼圖,用python畫圖比matlab好很多,推薦,可以根據(jù)自己的需要對代碼進行注釋得到相應的激活函數(shù)曲線,暫時編寫了三種論文常見的激活函數(shù)代碼,后續(xù)會進行更新其他激活函數(shù)畫圖的代碼。如果有幫助,請收藏關注一下我吧,如果有更多的需求歡迎私信我


1. Sigmoid

sigmoid 是使用范圍最廣的一類激活函數(shù),具有指數(shù)函數(shù)形狀,它在物理意義上最為接近生物神經(jīng)元,是一個在生物學中常見的S型的函數(shù),也稱為S型生長曲線。此外,(0, 1) 的輸出還可以被表示作概率,或用于輸入的歸一化,代表性的如Sigmoid交叉熵損失函數(shù)。
【學習經(jīng)驗分享NO.16】超全代碼-python畫Sigmoid,ReLU,Tanh等十多種激活函數(shù)曲線及其梯度曲線(持續(xù)更新)

		import math
import numpy as np
import matplotlib.pyplot as plt

# set x's range
x = np.arange(-10, 10, 0.1)
y1 = 1 / (1 + math.e ** (-x))  # sigmoid
# y11=math.e**(-x)/((1+math.e**(-x))**2)
y11 = 1 / (2 + math.e ** (-x)+ math.e ** (x))  # sigmoid的導數(shù)
y2 = (math.e ** (x) - math.e ** (-x)) / (math.e ** (x) + math.e ** (-x))  # tanh
y22 = 1-y2*y2  # tanh函數(shù)的導數(shù)
y3 = np.where(x < 0, 0, x)  # relu
y33 = np.where(x < 0, 0, 1)  # ReLU函數(shù)導數(shù)
plt.xlim(-4, 4)
plt.ylim(-1, 1.2)
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.spines['bottom'].set_position(('data', 0))
ax.spines['left'].set_position(('data', 0))

# Draw pic
plt.plot(x, y1, label='Sigmoid', linestyle="-", color="black")
plt.plot(x, y11, label='Sigmoid derivative', linestyle="-", color="blue")
#plt.plot(x, y2, label='Tanh', linestyle="-", color="black")
#plt.plot(x, y22, label='Tanh derivative', linestyle="-", color="blue")
#plt.plot(x, y3, label='Tanh', linestyle="-", color="black")
#plt.plot(x, y33, label='Tanh derivative', linestyle="-", color="blue")
# Title
plt.legend(['Sigmoid', 'Tanh', 'Relu'])
plt.legend(['Sigmoid', 'Sigmoid derivative'])  # y1 y11
#plt.legend(['Tanh', 'Tanh derivative'])  # y2 y22
#plt.legend(['Relu', 'Relu derivative'])  # y3 y33

#plt.legend(['Sigmoid', 'Sigmoid derivative', 'Relu', 'Relu derivative', 'Tanh', 'Tanh derivative'])  # y3 y33
# plt.legend(loc='upper left')  # 將圖例放在左上角

# save pic
# plt.savefig('plot_test.png', dpi=100)
plt.savefig(r"./")

# show it!!
plt.show()

【學習經(jīng)驗分享NO.16】超全代碼-python畫Sigmoid,ReLU,Tanh等十多種激活函數(shù)曲線及其梯度曲線(持續(xù)更新)

2. tanh

tanh在sigmoid基礎上做出改進,與sigmoid相比,tanh輸出均值為0,能夠加快網(wǎng)絡的收斂速度。然而,tanh同樣存在梯度消失現(xiàn)象。
【學習經(jīng)驗分享NO.16】超全代碼-python畫Sigmoid,ReLU,Tanh等十多種激活函數(shù)曲線及其梯度曲線(持續(xù)更新)

		import math
import numpy as np
import matplotlib.pyplot as plt

# set x's range
x = np.arange(-10, 10, 0.1)
y1 = 1 / (1 + math.e ** (-x))  # sigmoid
# y11=math.e**(-x)/((1+math.e**(-x))**2)
y11 = 1 / (2 + math.e ** (-x)+ math.e ** (x))  # sigmoid的導數(shù)
y2 = (math.e ** (x) - math.e ** (-x)) / (math.e ** (x) + math.e ** (-x))  # tanh
y22 = 1-y2*y2  # tanh函數(shù)的導數(shù)
y3 = np.where(x < 0, 0, x)  # relu
y33 = np.where(x < 0, 0, 1)  # ReLU函數(shù)導數(shù)
plt.xlim(-4, 4)
plt.ylim(-1, 1.2)
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.spines['bottom'].set_position(('data', 0))
ax.spines['left'].set_position(('data', 0))

# Draw pic
#plt.plot(x, y1, label='Sigmoid', linestyle="-", color="black")
#plt.plot(x, y11, label='Sigmoid derivative', linestyle="-", color="blue")

#plt.plot(x, y2, label='Tanh', linestyle="-", color="black")
#plt.plot(x, y22, label='Tanh derivative', linestyle="-", color="blue")

plt.plot(x, y3, label='Tanh', linestyle="-", color="black")
plt.plot(x, y33, label='Tanh derivative', linestyle="-", color="blue")

# Title
plt.legend(['Sigmoid', 'Tanh', 'Relu'])
#plt.legend(['Sigmoid', 'Sigmoid derivative'])  # y1 y11
#plt.legend(['Tanh', 'Tanh derivative'])  # y2 y22
plt.legend(['Relu', 'Relu derivative'])  # y3 y33

#plt.legend(['Sigmoid', 'Sigmoid derivative', 'Relu', 'Relu derivative', 'Tanh', 'Tanh derivative'])  # y3 y33
# plt.legend(loc='upper left')  # 將圖例放在左上角

# save pic
# plt.savefig('plot_test.png', dpi=100)
plt.savefig(r"./")

# show it!!
plt.show()

【學習經(jīng)驗分享NO.16】超全代碼-python畫Sigmoid,ReLU,Tanh等十多種激活函數(shù)曲線及其梯度曲線(持續(xù)更新)

3. ReLU

ReLU是針對sigmoid與tanh中存在的梯度消失問題提出的激活活函數(shù)。由于ReLU在x大于0時梯度保持不變,從而解決了tanh中存在的梯度消失現(xiàn)象。但是,在訓練過程中,當x進入到小于0的范圍時,激活函數(shù)輸出一直為0,使得網(wǎng)絡無法更新權重,出現(xiàn)了“神經(jīng)元死亡”現(xiàn)象。同時,與sigmoid相似,ReLU輸出均值大于0,同樣存在偏移現(xiàn)象。
【學習經(jīng)驗分享NO.16】超全代碼-python畫Sigmoid,ReLU,Tanh等十多種激活函數(shù)曲線及其梯度曲線(持續(xù)更新)

		import math
import numpy as np
import matplotlib.pyplot as plt

# set x's range
x = np.arange(-10, 10, 0.1)
y1 = 1 / (1 + math.e ** (-x))  # sigmoid
# y11=math.e**(-x)/((1+math.e**(-x))**2)
y11 = 1 / (2 + math.e ** (-x)+ math.e ** (x))  # sigmoid的導數(shù)
y2 = (math.e ** (x) - math.e ** (-x)) / (math.e ** (x) + math.e ** (-x))  # tanh
y22 = 1-y2*y2  # tanh函數(shù)的導數(shù)
y3 = np.where(x < 0, 0, x)  # relu
y33 = np.where(x < 0, 0, 1)  # ReLU函數(shù)導數(shù)
plt.xlim(-4, 4)
plt.ylim(-1, 1.2)
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.spines['bottom'].set_position(('data', 0))
ax.spines['left'].set_position(('data', 0))

# Draw pic
#plt.plot(x, y1, label='Sigmoid', linestyle="-", color="black")
#plt.plot(x, y11, label='Sigmoid derivative', linestyle="-", color="blue")

#plt.plot(x, y2, label='Tanh', linestyle="-", color="black")
#plt.plot(x, y22, label='Tanh derivative', linestyle="-", color="blue")

plt.plot(x, y3, label='Tanh', linestyle="-", color="black")
plt.plot(x, y33, label='Tanh derivative', linestyle="-", color="blue")



# Title
plt.legend(['Sigmoid', 'Tanh', 'Relu'])
#plt.legend(['Sigmoid', 'Sigmoid derivative'])  # y1 y11
#plt.legend(['Tanh', 'Tanh derivative'])  # y2 y22
plt.legend(['Relu', 'Relu derivative'])  # y3 y33

#plt.legend(['Sigmoid', 'Sigmoid derivative', 'Relu', 'Relu derivative', 'Tanh', 'Tanh derivative'])  # y3 y33
# plt.legend(loc='upper left')  # 將圖例放在左上角

# save pic
# plt.savefig('plot_test.png', dpi=100)
plt.savefig(r"./")

# show it!!
plt.show()

【學習經(jīng)驗分享NO.16】超全代碼-python畫Sigmoid,ReLU,Tanh等十多種激活函數(shù)曲線及其梯度曲線(持續(xù)更新)

4. Leaky ReLU

Leaky ReLU 通過把 x 的非常小的線性分量給予負輸入(0.01x)來調整負值的零梯度(zero gradients)問題;
leak 有助于擴大 ReLU 函數(shù)的范圍,通常 a 的值為 0.01 左右;
Leaky ReLU 的函數(shù)范圍是(負無窮到正無窮)。
注意:從理論上講,Leaky ReLU 具有 ReLU 的所有優(yōu)點,而且 Dead ReLU 不會有任何問題,但在實際操作中,尚未完全證明 Leaky ReLU 總是比 ReLU 更好。

import numpy as np
import matplotlib.pyplot as plt 

# Generate data points 
x = np.arange(-5, 5, 0.1) 
  
# Compute Leaky ReLU for different values of alpha 
alpha = 0.3 # alpha is the slope of the negative part 
y_leaky_relu = np.maximum(x, x * alpha) 
  
# Plotting the Leaky ReLU graph  
plt.plot(x, y_leaky_relu) 
plt.title("Leaky ReLU Activation Function") 
plt.xlabel("X") 
plt.ylabel("Y") 

 # Show the plot  
 plt.show()

【學習經(jīng)驗分享NO.16】超全代碼-python畫Sigmoid,ReLU,Tanh等十多種激活函數(shù)曲線及其梯度曲線(持續(xù)更新)

5. ELU

ELU(Exponential Linear Unit)激活函數(shù)是一種添加了指數(shù)項的非線性激活函數(shù),它的表達式如下:
ELU(x) = {x, x > 0; α(e^x-1), x <= 0}
其中α是一個超參數(shù)。ELU 激活函數(shù)的特征在于輸入為負時會有正值的輸出,而不是 0。這樣可以有效地避免神經(jīng)元死亡問題。 ELU 激活函數(shù)有助于神經(jīng)元學習復雜的非線性函數(shù),可以幫助神經(jīng)元學習復雜的非線性特征。

import numpy as np
import matplotlib.pyplot as plt

# Generate data points
x = np.arange(-5, 5, 0.1)

# Compute ELU for different values of alpha and lambda
alpha = 0.3  # alpha is the slope of the negative part
lamda = 1  # lambda is the magnitude of the negative part
y_elu = np.maximum(x, alpha * (np.exp(x) - 1)) + lamda * (np.maximum(0, x) - x)  # Compute ELU

# Plotting the ELU graph
plt.plot(x, y_elu)
plt.title("ELU Activation Function")
plt.xlabel("X")
plt.ylabel("Y")

# Show the plot
plt.show()

【學習經(jīng)驗分享NO.16】超全代碼-python畫Sigmoid,ReLU,Tanh等十多種激活函數(shù)曲線及其梯度曲線(持續(xù)更新)

6.PReLU

PReLU 激活函數(shù)是一種可訓練的改進版本的 Leaky ReLU 激活函數(shù),它使用了可訓練的斜率 α 作為超參數(shù)。PReLU 的定義如下:
f(x) = max(αx, x)
其中α是可學習的斜率,通常初始化為 0.25 或者 0.01。PReLU 和 Leaky ReLU 很相似,但 PReLU 能夠根據(jù)輸入和預期而學習一個最佳的斜率 α。這能夠避免使用常量 α 時所帶來的問題。

import numpy as np
import matplotlib.pyplot as plt 

# Generate data points 
x = np.arange(-5, 5, 0.1) 
  
# Compute PReLU for different values of alpha and lambda 
alpha = 0.3 # alpha is the slope of the negative part  
lamda = 1 # lambda is the magnitude of the negative part  

 y_prelu = lamda * (np.maximum(0, x) - x) + alpha * np.minimum(0, x) # Compute PReLU  

    # Plotting the PReLU graph  
plt.plot(x, y_prelu) 
plt.title("PReLU Activation Function") 
plt.xlabel("X") 
plt.ylabel("Y")  

 # Show the plot  
 plt.show()

【學習經(jīng)驗分享NO.16】超全代碼-python畫Sigmoid,ReLU,Tanh等十多種激活函數(shù)曲線及其梯度曲線(持續(xù)更新)

7. Softmax

Softmax 激活函數(shù)用于多分類問題,它在神經(jīng)網(wǎng)絡的最后一層被使用。Softmax 函數(shù)能夠將輸入信號轉換為 0 到 1 之間的值,表示不同分類的可能性。Softmax 激活函數(shù)的定義如下:
Sj(z) = exp(zj) / Σk=1n exp(zk)
其中 zj 表示神經(jīng)元的輸入,n 表示神經(jīng)元的個數(shù),Sj(z) 表示神經(jīng)元 j 的輸出。Softmax 函數(shù)會將所有輸出值相加為 1,并將每個輸出值歸一化到 0 到 1 之間。

import numpy as np
import matplotlib.pyplot as plt 

# Generate data points 
x = np.arange(-5, 5, 0.1) 
  
# Compute Softmax for different values of beta and gamma 
beta = 0.3 # beta is the slope of the negative part  
gamma = 1 # gamma is the magnitude of the negative part  

 y_softmax = gamma * (np.exp(beta*x) / np.sum(np.exp(beta*x))) # Compute Softmax  

    # Plotting the Softmax graph  
plt.plot(x, y_softmax) 
plt.title("Softmax Activation Function") 
plt.xlabel("X") 
plt.ylabel("Y")  

 # Show the plot  
 plt.show()

【學習經(jīng)驗分享NO.16】超全代碼-python畫Sigmoid,ReLU,Tanh等十多種激活函數(shù)曲線及其梯度曲線(持續(xù)更新)

8. Swish

Swish 激活函數(shù)是一種新型的激活函數(shù),它的表達式如下:
S(x) = x * Sigmoid(x)
其中 Sigmoid 函數(shù)為雙曲正切函數(shù),它的定義如下:
Sigmoid(x) = 1 / (1 + exp(-x))
Swish 函數(shù)可以將輸入信號轉化為 0 到 1 之間的值,表示不同分類的可能性。特別是,在神經(jīng)網(wǎng)絡中,Swish 激活函數(shù)可以幫助神經(jīng)元學習復雜的非線性函數(shù)。

import numpy as np
import matplotlib.pyplot as plt 

# Generate data points 
x = np.arange(-5, 5, 0.1) 

 # Compute Swish for different values of beta and gamma  
beta = 0.3 # beta is the slope of the negative part  
gamma = 1 # gamma is the magnitude of the negative part  

 y_swish = gamma * x * (np.exp(beta*x) / np.sum(np.exp(beta*x))) # Compute Softmax  

    # Plotting the Swish graph  
plt.plot(x, y_swish) 
plt.title("Swish Activation Function") 
plt.xlabel("X") 
plt

【學習經(jīng)驗分享NO.16】超全代碼-python畫Sigmoid,ReLU,Tanh等十多種激活函數(shù)曲線及其梯度曲線(持續(xù)更新)

9. Maxout

Maxout 激活函數(shù)是一種新型的激活函數(shù),它可以用來代替?zhèn)鹘y(tǒng)的 ReLU 和 Sigmoid 函數(shù)。Maxout 激活函數(shù)的表達式如下:
Maxout(x) = MAX{ f1(x), f2(x) }
其中 f1 和 f2 是 Maxout 的兩個參數(shù),它們分別對應于 Maxout 的兩個輸入。Maxout 的最大值就是它的輸出值。Maxout 激活函數(shù)可以幫助神經(jīng)元學習復雜的非線性函數(shù),特別是在神經(jīng)網(wǎng)絡中。

import numpy as np
import matplotlib.pyplot as plt

# Generate data points
x = np.arange(-5, 5, 0.1)

 # Compute Maxout for different values of alpha and beta
alpha = 1 # alpha is the slope of the linear part
beta = 2 # beta is the slope of the non-linear part

y_maxout = np.maximum(alpha*x, beta*x) # Compute Maxout

    # Plotting the Maxout graph
plt.plot(x, y_maxout)
plt.title("Maxout Activation Function")
plt.xlabel("X")
plt.show()

【學習經(jīng)驗分享NO.16】超全代碼-python畫Sigmoid,ReLU,Tanh等十多種激活函數(shù)曲線及其梯度曲線(持續(xù)更新)

10. Softplus

Softplus 激活函數(shù)是一種漸進型的非線性激活函數(shù),它的表達式如下:
Softplus(x) = ln (1 + e^x)
Softplus 激活函數(shù)在輸入為 0 時取值為 0,它的輸出隨著輸入增大而變大,但不會到負無窮大。Softplus 激活函數(shù)有助于神經(jīng)元學習復雜的非線性函數(shù),可以幫助神經(jīng)元學習復雜的非線性特征。

import numpy as np
import matplotlib.pyplot as plt

# Generate data points
x = np.arange(-10, 10, 0.1)

 # Compute Softplus for different values of alpha and beta
alpha = 1 # alpha is the slope of the linear part
beta = 2 # beta is the slope of the non-linear part

y_softplus = np.log(1 + np.exp(alpha*x + beta*x)) # Compute Softplus

    # Plotting the Softplus graph
plt.plot(x, y_softplus)
plt.title("Softplus Activation Function")
plt.xlabel("X")
plt.show()

【學習經(jīng)驗分享NO.16】超全代碼-python畫Sigmoid,ReLU,Tanh等十多種激活函數(shù)曲線及其梯度曲線(持續(xù)更新)

11. GELU

GELU 激活函數(shù)是一種基于 Gaussian 概率分布的非線性函數(shù),它可以將輸入信號轉換為大于 0 的值或小于 0 的值。GELU 激活函數(shù)的特點是,它的梯度隨輸入的大小而變化,這使得它能夠有效地學習復雜的非線性問題。此外,GELU 激活函數(shù)還具有快速優(yōu)化和泛化性能優(yōu)異的優(yōu)勢。


import numpy as np
import matplotlib.pyplot as plt


def gelu(x):
    return 0.5 * x * (1 + np.tanh(np.sqrt(2 / np.pi) * (x + 0.044715 * np.power(x, 3))))


# Generate values for x axis
x_axis = np.arange(-4, 4, 0.01)
# Calculate corresponding y axis values using gelu() function
y_axis = [gelu(i) for i in x_axis]
# Plot the points
plt.plot(x_axis, y_axis)
# Set the x and y axes labels and title of the graph
plt.title("GELU Activation Function")
plt.xlabel('Input')
plt.ylabel('Output')

# Display the graph
plt.show()

【學習經(jīng)驗分享NO.16】超全代碼-python畫Sigmoid,ReLU,Tanh等十多種激活函數(shù)曲線及其梯度曲線(持續(xù)更新)

12. SILU

SiLU激活函數(shù)(又稱Sigmoid-weighted Linear Unit)是一種新型的非線性激活函數(shù),它將sigmoid函數(shù)和線性單元相結合,以此來獲得在低數(shù)值區(qū)域中表現(xiàn)良好的非線性映射能力。SiLU激活函數(shù)的特征是它在低數(shù)值區(qū)域中表現(xiàn)較為平滑,而在高數(shù)值區(qū)域中表現(xiàn)起來十分“銳利”,從而能夠有效地避免過度學習的問題。

import matplotlib.pyplot as plt
import numpy as np
from scipy.special import expit


# 定義SiLU函數(shù)和導函數(shù)
def silu(x):
    return x * expit(x)


def dsilu(x):
    return expit(x) * (1 + x * (1 - expit(x)))

    # 生成-5到5之間的100個數(shù)據(jù)點   計算SiLU和導SiLU函數(shù)的值   繪圖顯示 SiLU 函數(shù)以及導函數(shù)的曲線


x = np.linspace(-5, 5, 100)  # 生成-5到5之間的100個數(shù)據(jù)點

y = silu(x)  # 計算sigmoid函數(shù)的值
y_prime = dsilu(x) #計算sigmoid 導函數(shù)的值

plt.plot(x, y, label="silu")  # 繪圖 顯示 SiLU 函數(shù)以及導函數(shù)的曲線。
plt.plot(x, y_prime, label="d/dx silu") 
plt.legend()
plt.show()

【學習經(jīng)驗分享NO.16】超全代碼-python畫Sigmoid,ReLU,Tanh等十多種激活函數(shù)曲線及其梯度曲線(持續(xù)更新)

總結

本文介紹Sigmoid,ReLU,Tanh這三種常見的激活函數(shù)及利用python畫圖代碼,后續(xù)會持續(xù)更新,有幫助也請免費點個小小的收藏吧,有更多的需求幫助可關注私信我。關注即免費獲取大量人工智能學習資料。文章來源地址http://www.zghlxwxcb.cn/news/detail-401544.html

到了這里,關于【學習經(jīng)驗分享NO.16】超全代碼-python畫Sigmoid,ReLU,Tanh等十多種激活函數(shù)曲線及其梯度曲線(持續(xù)更新)的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如若轉載,請注明出處: 如若內容造成侵權/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領支付寶紅包贊助服務器費用

相關文章

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包