Python實(shí)現(xiàn)之激活函數(shù)
本文轉(zhuǎn)載自微信公眾號(hào)「python與大數(shù)據(jù)分析」,作者一只小小鳥(niǎo)鳥(niǎo)。轉(zhuǎn)載本文請(qǐng)聯(lián)系python與大數(shù)據(jù)分析公眾號(hào)。
激活函數(shù)(Activation Function),就是在人工神經(jīng)網(wǎng)絡(luò)的神經(jīng)元上運(yùn)行的函數(shù),負(fù)責(zé)將神經(jīng)元的輸入映射到輸出端。
如果不用激活函數(shù),每一層輸出都是上層輸入的線(xiàn)性函數(shù),無(wú)論神經(jīng)網(wǎng)絡(luò)有多少層,輸出都是輸入的線(xiàn)性組合,這種情況就是最原始的感知機(jī)。
如果使用的話(huà),激活函數(shù)給神經(jīng)元引入了非線(xiàn)性因素,使得神經(jīng)網(wǎng)絡(luò)可以任意逼近任何非線(xiàn)性函數(shù),這樣神經(jīng)網(wǎng)絡(luò)就可以應(yīng)用到眾多的非線(xiàn)性模型中。
- #!/usr/bin/env python
- # -*- coding: UTF-8 -*-
- # _ooOoo_
- # o8888888o
- # 88" . "88
- # ( | - _ - | )
- # O\ = /O
- # ____/`---'\____
- # .' \\| |// `.
- # / \\|||:|||// \
- # / _|||||-:- |||||- \
- # | | \\\ - /// | |
- # | \_| ''\---/'' | _/ |
- # \ .-\__ `-` ___/-. /
- # ___`. .' /--.--\ `. . __
- # ."" '< `.___\_<|>_/___.' >'"".
- # | | : `- \`.;`\ _ /`;.`/ - ` : | |
- # \ \ `-. \_ __\ /__ _/ .-` / /
- # ==`-.____`-.___\_____/___.-`____.-'==
- # `=---='
- '''
- @Project :pythonalgorithms
- @File :Activationfunction.py
- @Author :不勝人生一場(chǎng)醉@Date :2021/8/11 0:14
- '''
- import numpy as np
- from matplotlib import pyplot as plt
- def drawpic(x, y, label=' ', title=' '):
- plt.figure(figsize=(10, 8))
- ax = plt.gca() # 通過(guò)gca:get current axis得到當(dāng)前軸
- plt.rcParams['font.sans-serif'] = ['SimHei'] # 繪圖中文
- plt.rcParams['axes.unicode_minus'] = False # 繪圖負(fù)號(hào)
- plt.plot(x, y, label=label)
- # 設(shè)置圖片的右邊框和上邊框?yàn)椴伙@示
- ax.spines['right'].set_color('none')
- ax.spines['top'].set_color('none')
- # 挪動(dòng)x,y軸的位置,也就是圖片下邊框和左邊框的位置
- # data表示通過(guò)值來(lái)設(shè)置x軸的位置,將x軸綁定在y=0的位置
- ax.spines['bottom'].set_position(('data', 0))
- # axes表示以百分比的形式設(shè)置軸的位置,即將y軸綁定在x軸50%的位置
- ax.spines['left'].set_position(('axes', 0.5))
- # ax.spines['left'].set_position(('data', 0))
- plt.title(title)
- plt.legend(loc='upper right')
- plt.show()
- if __name__ == '__main__':
- std = 0.1 # 標(biāo)準(zhǔn)差為0.1
- avg = 1 # 平均值為1
- x = np.linspace(avg - 5 * std, avg + 5 * std, 100)
- y = normaldistribution(x, avg, std)
- drawpic(x, y, 'normaldistribution', 'normal distribution function')
- x = np.linspace(-5, 5, 100)
- y = sigmoid(x)
- drawpic(x, y, 'sigmoid', 'sigmoid Activation function')
- y = tanh(x)
- drawpic(x, y, 'tanh', 'tanh Activation function')
- y = stepfunction(x)
- drawpic(x, y, 'tanh', 'step Activation function')
- y = relu(x)
- drawpic(x, y, 'relu', 'relu Activation function')
- y = leakyrelu(x)
- drawpic(x, y, 'leakyrelu', 'leakyrelu Activation function')
- y = softmax(x)
- drawpic(x, y, 'softmax', 'softmax Activation function')
- # 求正態(tài)分布值,avg表示期望值,std表示標(biāo)準(zhǔn)差
- def normaldistribution(x, avg=0, std=1):
- return np.exp(-(x - avg) ** 2 / (2 * std ** 2)) / (np.sqrt(2 * np.pi) * std)
- # return np.exp(-(x - avg) ** 2 / (2 * std ** 2)) / (math.sqrt(2 * math.pi) *
- # Sigmoid函數(shù)
- # Sigmoid函數(shù)是一個(gè)在生物學(xué)中常見(jiàn)的S型函數(shù),也稱(chēng)為S型生長(zhǎng)曲線(xiàn)。
- # 在信息科學(xué)中,由于其單增以及反函數(shù)單增等性質(zhì),Sigmoid函數(shù)常被用作神經(jīng)網(wǎng)絡(luò)的閾值函數(shù),將變量映射到0,1之間
- def sigmoid(x):
- return 1 / (1 + np.power(np.e, -x))
- # Tanh函數(shù)
- # Tanh是雙曲函數(shù)中的一個(gè),Tanh()為雙曲正切。
- # 在數(shù)學(xué)中,雙曲正切“Tanh”是由基本雙曲函數(shù)雙曲正弦和雙曲余弦推導(dǎo)而來(lái)。
- # 函數(shù)tanh(藍(lán)色)和函數(shù)sigmoid(橙色)一樣,在其飽和區(qū)的接近于0,都容易產(chǎn)生后續(xù)梯度消失、計(jì)算量大的問(wèn)題
- def tanh(x):
- return (np.exp(x) - np.exp(-x)) / (np.exp(x) + np.exp(-x))
- # 階躍函數(shù)
- def stepfunction(x):
- return np.array(x > 0, dtype=np.int32)
- # ReLU函數(shù)
- # Relu激活函數(shù)(The Rectified Linear Unit),用于隱層神經(jīng)元輸出。
- # Relu會(huì)使一部分神經(jīng)元的輸出為0,這樣就造成了網(wǎng)絡(luò)的稀疏性,并且減少了參數(shù)的相互依存關(guān)系,緩解了過(guò)擬合問(wèn)題的發(fā)生。
- def relu(x):
- return np.maximum(0, x)
- # leaky ReLU函數(shù)
- def leakyrelu(x):
- return np.maximum(0.01 * x, x)
- # softmax函數(shù)
- # softmax函數(shù)可以看做是Sigmoid函數(shù)的一般化,用于多分類(lèi)神經(jīng)網(wǎng)絡(luò)輸出。
- def softmax(x):
- return np.exp(x) / np.sum(np.exp(x))