溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么使用Python繪制常見的激活函數(shù)曲線

發(fā)布時(shí)間:2023-04-26 13:51:05 來(lái)源:億速云 閱讀:101 作者:zzz 欄目:編程語(yǔ)言

本文小編為大家詳細(xì)介紹“怎么使用Python繪制常見的激活函數(shù)曲線”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“怎么使用Python繪制常見的激活函數(shù)曲線”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來(lái)學(xué)習(xí)新知識(shí)吧。

準(zhǔn)備工作:下載numpy、matplotlib、sympy

pip install numpy matplotlib sympy

查找對(duì)應(yīng)庫(kù)的文檔:

numpy文檔 matplotlib文檔 sympy文檔

寫代碼的時(shí)候發(fā)現(xiàn)vscode不會(huì)格式化我的python?查了一下原來(lái)還要安裝flake8和yapf,一個(gè)是檢查代碼規(guī)范工具一個(gè)是格式化工具,接著進(jìn)行配置setting.json

"python.linting.flake8Enabled": true, // 規(guī)范檢查工具
"python.formatting.provider": "yapf", // 格式化工具
"python.linting.flake8Args": ["--max-line-length=248"], // 設(shè)置單行最長(zhǎng)字符限制
"python.linting.pylintEnabled": false, // 關(guān)閉pylint工具

準(zhǔn)備工作完成, 接下來(lái)就看看怎么寫代碼

第一步 新建一個(gè)py文件

先把激活函數(shù)的函數(shù)表達(dá)式寫出來(lái),這有兩種方式,如果只是單純的得出計(jì)算結(jié)果,其實(shí)用numpy就足夠了,但是還要自己去求導(dǎo),那就需要用sympy寫出函數(shù)式了。

sympy表達(dá)函數(shù)的方式是這樣的:

from sympy import symbols, evalf, diff
# 我們先要定義自變量是什么,這邊按需求來(lái),這是文檔的例子有兩個(gè)變量
x, y = symbols('x y')
# 然后我們寫出函數(shù)表達(dá)式
expr = x + 2*y
# 輸出看一下是什么東西
expr # x + 2*y
# 接著就要用我們定義的函數(shù)了
expr.evalf(subs={x: 10, y: 20}) # 50.000000
# 再對(duì)我們的函數(shù)求導(dǎo)
diff(expr, x, 1) # 對(duì)x進(jìn)行求導(dǎo)得出結(jié)果 1,這也是表達(dá)式

diff為sympy的求導(dǎo)函數(shù)

sympy.core.function.diff(f, *symbols, **kwargs)

接著我們定義激活函數(shù)的表達(dá)式

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)

然后再定義一個(gè)求導(dǎo)函數(shù)

def derivate(formula, len, variate):
    """
    定義函數(shù)求導(dǎo)
      formula:函數(shù)公式
      len:求導(dǎo)次數(shù)
      variate:自變量
    """
    return diff(formula, variate, len)

這邊有一個(gè)問題,為什么其他函數(shù)都是一個(gè),而softMax函數(shù)有兩個(gè),一個(gè)是softMax函數(shù)定義,一個(gè)是其導(dǎo)函數(shù)定義?

我們看一下softMax函數(shù)的樣子

怎么使用Python繪制常見的激活函數(shù)曲線

softMax函數(shù)分母需要寫累加的過(guò)程,使用numpy.sum無(wú)法通過(guò)sympy去求導(dǎo)(有人可以,我不知道為什么,可能是使用方式不同,知道的可以交流一下)而使用sympy.Sum或者sympy.summation又只能從i到n每次以1為單位累加

例如:假定有個(gè)表達(dá)式為 m**x (m的x次方)sympy.Sum(m**x, (x, 0, 100))則結(jié)果為m**100 + m**99 + m**98 &hellip; + m**1,而我定義的ndarray又是np.arange(-10, 10, 0.05),這就無(wú)法達(dá)到要求,就無(wú)法進(jìn)行求導(dǎo)。

所以就寫兩個(gè)函數(shù),一個(gè)是原函數(shù)定義,一個(gè)是導(dǎo)函數(shù)定義,并且之前也說(shuō)了,如果是求值的話,其實(shí)只用numpy就可以完成。

至此,所有函數(shù)以及導(dǎo)函數(shù)就被我們定義好了

第二步 使用matplotlib繪制曲線

首先,我們得知道m(xù)atplotlib有什么吧

matplotlib主要有Figure、Axes、Axis、Artist。我理解為figure就是畫布,我們?cè)诶L制圖形之前得準(zhǔn)備好畫布;axes和axis翻譯都是軸的意思,但是axes應(yīng)該是坐標(biāo)軸,axis是坐標(biāo)軸中的某一個(gè)軸;artist為其他可加入的元素

如果要繪制一張簡(jiǎn)單的圖可以這樣做

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.

然后我們準(zhǔn)備繪制我們的函數(shù)曲線了

plt.xlabel('x label') // 兩種方式加label,一種為ax.set_xlabel(面向?qū)ο螅环N就是這種(面向函數(shù))
plt.ylabel('y label')

加完laben之后 ,我考慮了兩種繪制方式,一是把所有曲線都繪制在一個(gè)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列

第二個(gè)是通過(guò)輸入函數(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繪制常見的激活函數(shù)曲線

讀到這里,這篇“怎么使用Python繪制常見的激活函數(shù)曲線”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過(guò)才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI