Python實現(xiàn)繪制多種激活函數(shù)曲線詳解
利用numpy、matplotlib、sympy繪制sigmoid、tanh、ReLU、leaky ReLU、softMax函數(shù)
起因:深度學(xué)習(xí)途中,老師留一作業(yè),繪制激活函數(shù)及其導(dǎo)數(shù),耗時挺久,記錄學(xué)習(xí)過程
準備工作:下載numpy、matplotlib、sympy
pip install numpy matplotlib sympy
查找對應(yīng)庫的文檔:
寫代碼的時候發(fā)現(xiàn)vscode不會格式化我的python?查了一下原來還要安裝flake8和yapf,一個是檢查代碼規(guī)范工具一個是格式化工具,接著進行配置setting.json
"python.linting.flake8Enabled": true, // 規(guī)范檢查工具 "python.formatting.provider": "yapf", // 格式化工具 "python.linting.flake8Args": ["--max-line-length=248"], // 設(shè)置單行最長字符限制 "python.linting.pylintEnabled": false, // 關(guān)閉pylint工具
準備工作完成, 接下來就看看怎么寫代碼
第一步 新建一個py文件
先把激活函數(shù)的函數(shù)表達式寫出來,這有兩種方式,如果只是單純的得出計算結(jié)果,其實用numpy就足夠了,但是還要自己去求導(dǎo),那就需要用sympy寫出函數(shù)式了。
sympy表達函數(shù)的方式是這樣的:
from sympy import symbols, evalf, diff
# 我們先要定義自變量是什么,這邊按需求來,這是文檔的例子有兩個變量
x, y = symbols('x y')
# 然后我們寫出函數(shù)表達式
expr = x + 2*y
# 輸出看一下是什么東西
expr # x + 2*y
# 接著就要用我們定義的函數(shù)了
expr.evalf(subs={x: 10, y: 20}) # 50.000000
# 再對我們的函數(shù)求導(dǎo)
diff(expr, x, 1) # 對x進行求導(dǎo)得出結(jié)果 1,這也是表達式
diff為sympy的求導(dǎo)函數(shù)
sympy.core.function.diff(f, *symbols, **kwargs)
接著我們定義激活函數(shù)的表達式
def sigmoid():
"""
定義sigmoid函數(shù)
"""
x = symbols('x')
return 1. / (1 + exp(-x))
def tanh():
"""
定義tanh函數(shù)
"""
x = symbols('x')
return (exp(x) - exp(-x)) / (exp(x) + exp(-x))
def relu():
"""
定義ReLU函數(shù)
"""
x = symbols('x')
return Piecewise((0, x < 0), (x, x >= 0))
def leakyRelu():
"""
定義Leaky ReLu函數(shù)
"""
x = symbols('x')
return Piecewise((0.1 * x, x < 0), (x, x >= 0))
def softMax(x: np.ndarray):
"""
定義SoftMax函數(shù)\n
"""
exp_x = np.exp(x)
print(exp_x, np.sum(exp_x))
return exp_x / np.sum(exp_x)
def softmax_derivative(x):
"""
定義SoftMax導(dǎo)數(shù)函數(shù)\n
x - 輸入x向量
"""
s = softMax(x)
return s * (1 - s)
然后再定義一個求導(dǎo)函數(shù)
def derivate(formula, len, variate):
"""
定義函數(shù)求導(dǎo)
formula:函數(shù)公式
len:求導(dǎo)次數(shù)
variate:自變量
"""
return diff(formula, variate, len)
這邊有一個問題,為什么其他函數(shù)都是一個,而softMax函數(shù)有兩個,一個是softMax函數(shù)定義,一個是其導(dǎo)函數(shù)定義?
我們看一下softMax函數(shù)的樣子

softMax函數(shù)分母需要寫累加的過程,使用numpy.sum無法通過sympy去求導(dǎo)(有人可以,我不知道為什么,可能是使用方式不同,知道的可以交流一下)而使用sympy.Sum或者sympy.summation又只能從i到n每次以1為單位累加
例如:假定有個表達式為 m**x (m的x次方)sympy.Sum(m**x, (x, 0, 100))則結(jié)果為m**100 + m**99 + m**98 … + m**1,而我定義的ndarray又是np.arange(-10, 10, 0.05),這就無法達到要求,就無法進行求導(dǎo)。
所以就寫兩個函數(shù),一個是原函數(shù)定義,一個是導(dǎo)函數(shù)定義,并且之前也說了,如果是求值的話,其實只用numpy就可以完成。
至此,所有函數(shù)以及導(dǎo)函數(shù)就被我們定義好了
第二步 使用matplotlib繪制曲線
首先,我們得知道m(xù)atplotlib有什么吧
matplotlib主要有Figure、Axes、Axis、Artist。我理解為figure就是畫布,我們在繪制圖形之前得準備好畫布;axes和axis翻譯都是軸的意思,但是axes應(yīng)該是坐標軸,axis是坐標軸中的某一個軸;artist為其他可加入的元素
如果要繪制一張簡單的圖可以這樣做
x = np.linspace(0, 2, 100) # Sample data.
# Note that even in the OO-style, we use `.pyplot.figure` to create the Figure.
fig, ax = plt.subplots(figsize=(5, 2.7), layout='constrained')
ax.plot(x, x, label='linear') # Plot some data on the axes.
ax.plot(x, x**2, label='quadratic') # Plot more data on the axes...
ax.plot(x, x**3, label='cubic') # ... and some more.
ax.set_xlabel('x label') # Add an x-label to the axes.
ax.set_ylabel('y label') # Add a y-label to the axes.
ax.set_title("Simple Plot") # Add a title to the axes.
ax.legend() # Add a legend.
然后我們準備繪制我們的函數(shù)曲線了
plt.xlabel('x label') // 兩種方式加label,一種為ax.set_xlabel(面向?qū)ο螅环N就是這種(面向函數(shù))
plt.ylabel('y label')
加完laben之后 ,我考慮了兩種繪制方式,一是把所有曲線都繪制在一個figure里面,但是分為不同的axes
使用subplot函數(shù)可以把figure分為2行2列的axes
plt.subplot(2, 2, 1, adjustable='box') # 1行1列 plt.subplot(2, 2, 2, adjustable='box') # 1行2列
第二個是通過輸入函數(shù)名繪制指定的函數(shù)
do = input( 'input function expression what you want draw(sigmoid, tanh, relu, leakyRelu, softMax)\n' )
得到輸入之后
try:
plt.xlabel('x label')
plt.ylabel('y label')
plt.title(do)
if (do == 'softMax'):
plt.plot(num, softMax(num), label='Softmax')
plt.plot(num, softmax_derivative(num), label='Softmax Derivative')
else:
plt.plot(
num,
[eval(f'{do}()').evalf(subs={symbols("x"): i}) for i in num])
plt.plot(num, [
derivate(eval(f'{do}()'), 1, 'x').evalf(subs={symbols('x'): i})
for i in num
])
plt.tight_layout()
plt.show()
except TypeError:
print(
'input function expression is wrong or the funciton is not configured'
)
這就完活了,附一張賣家秀

以上就是Python實現(xiàn)繪制多種激活函數(shù)曲線詳解的詳細內(nèi)容,更多關(guān)于Python繪制激活函數(shù)曲線的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python使用post及get方式提交數(shù)據(jù)的實例
今天小編就為大家分享一篇關(guān)于Python使用post及get方式提交數(shù)據(jù)的實例,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-01-01
詳解python ThreadPoolExecutor異常捕獲
本文主要介紹了詳解python ThreadPoolExecutor異常捕獲,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
Python過濾函數(shù)filter()使用自定義函數(shù)過濾序列實例
這篇文章主要介紹了Python過濾函數(shù)filter()使用自定義函數(shù)過濾序列實例,配合自定義函數(shù)可以實現(xiàn)許多強大的功能,需要的朋友可以參考下2014-08-08
Python處理字節(jié)串:struct.pack和struct.unpack使用
這篇文章主要介紹了Python處理字節(jié)串:struct.pack和struct.unpack使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01
python訪問mysql數(shù)據(jù)庫的實現(xiàn)方法(2則示例)
這篇文章主要介紹了python訪問mysql數(shù)據(jù)庫的實現(xiàn)方法,結(jié)合實例形式分析了兩種Python操作MySQL數(shù)據(jù)庫的相關(guān)技巧,需要的朋友可以參考下2016-01-01

