Python實現(xiàn)繪制多種激活函數(shù)曲線詳解
利用numpy、matplotlib、sympy繪制sigmoid、tanh、ReLU、leaky ReLU、softMax函數(shù)
起因:深度學習途中,老師留一作業(yè),繪制激活函數(shù)及其導數(shù),耗時挺久,記錄學習過程
準備工作:下載numpy、matplotlib、sympy
pip install numpy matplotlib sympy
查找對應庫的文檔:
寫代碼的時候發(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"], // 設置單行最長字符限制 "python.linting.pylintEnabled": false, // 關閉pylint工具
準備工作完成, 接下來就看看怎么寫代碼
第一步 新建一個py文件
先把激活函數(shù)的函數(shù)表達式寫出來,這有兩種方式,如果只是單純的得出計算結果,其實用numpy就足夠了,但是還要自己去求導,那就需要用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ù)求導 diff(expr, x, 1) # 對x進行求導得出結果 1,這也是表達式
diff為sympy的求導函數(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導數(shù)函數(shù)\n x - 輸入x向量 """ s = softMax(x) return s * (1 - s)
然后再定義一個求導函數(shù)
def derivate(formula, len, variate): """ 定義函數(shù)求導 formula:函數(shù)公式 len:求導次數(shù) variate:自變量 """ return diff(formula, variate, len)
這邊有一個問題,為什么其他函數(shù)都是一個,而softMax函數(shù)有兩個,一個是softMax函數(shù)定義,一個是其導函數(shù)定義?
我們看一下softMax函數(shù)的樣子
softMax函數(shù)分母需要寫累加的過程,使用numpy.sum無法通過sympy去求導(有人可以,我不知道為什么,可能是使用方式不同,知道的可以交流一下)而使用sympy.Sum或者sympy.summation又只能從i到n每次以1為單位累加
例如:假定有個表達式為 m**x (m的x次方)sympy.Sum(m**x, (x, 0, 100))則結果為m**100 + m**99 + m**98 … + m**1,而我定義的ndarray又是np.arange(-10, 10, 0.05),這就無法達到要求,就無法進行求導。
所以就寫兩個函數(shù),一個是原函數(shù)定義,一個是導函數(shù)定義,并且之前也說了,如果是求值的話,其實只用numpy就可以完成。
至此,所有函數(shù)以及導函數(shù)就被我們定義好了
第二步 使用matplotlib繪制曲線
首先,我們得知道m(xù)atplotlib有什么吧
matplotlib主要有Figure、Axes、Axis、Artist。我理解為figure就是畫布,我們在繪制圖形之前得準備好畫布;axes和axis翻譯都是軸的意思,但是axes應該是坐標軸,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ū)ο螅?,一種就是這種(面向函數(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)容,更多關于Python繪制激活函數(shù)曲線的資料請關注腳本之家其它相關文章!
相關文章
Python面試之os.system()和os.popen()的區(qū)別詳析
Python調(diào)用Shell,有兩種方法:os.system(cmd)或os.popen(cmd)腳本執(zhí)行過程中的輸出內(nèi)容,下面這篇文章主要給大家介紹了關于Python面試之os.system()和os.popen()區(qū)別的相關資料,需要的朋友可以參考下2022-06-06如何將yolo格式轉(zhuǎn)化為voc格式:txt轉(zhuǎn)xml(親測有效)
這篇文章主要介紹了如何將yolo格式轉(zhuǎn)化為voc格式:txt轉(zhuǎn)xml,親測有效,可以使用,本文通過圖文并茂的形式給大家介紹的非常詳細,感興趣的朋友參考下吧2023-12-12