Python實現(xiàn)之激活函數(shù)
本文轉(zhuǎn)載自微信公眾號「python與大數(shù)據(jù)分析」,作者一只小小鳥鳥。轉(zhuǎn)載本文請聯(lián)系python與大數(shù)據(jù)分析公眾號。
激活函數(shù)(Activation Function),就是在人工神經(jīng)網(wǎng)絡(luò)的神經(jīng)元上運行的函數(shù),負責將神經(jīng)元的輸入映射到輸出端。
如果不用激活函數(shù),每一層輸出都是上層輸入的線性函數(shù),無論神經(jīng)網(wǎng)絡(luò)有多少層,輸出都是輸入的線性組合,這種情況就是最原始的感知機。
如果使用的話,激活函數(shù)給神經(jīng)元引入了非線性因素,使得神經(jīng)網(wǎng)絡(luò)可以任意逼近任何非線性函數(shù),這樣神經(jīng)網(wǎng)絡(luò)就可以應(yīng)用到眾多的非線性模型中。
- #!/usr/bin/env python
 - # -*- coding: UTF-8 -*-
 - # _ooOoo_
 - # o8888888o
 - # 88" . "88
 - # ( | - _ - | )
 - # O\ = /O
 - # ____/`---'\____
 - # .' \\| |// `.
 - # / \\|||:|||// \
 - # / _|||||-:- |||||- \
 - # | | \\\ - /// | |
 - # | \_| ''\---/'' | _/ |
 - # \ .-\__ `-` ___/-. /
 - # ___`. .' /--.--\ `. . __
 - # ."" '< `.___\_<|>_/___.' >'"".
 - # | | : `- \`.;`\ _ /`;.`/ - ` : | |
 - # \ \ `-. \_ __\ /__ _/ .-` / /
 - # ==`-.____`-.___\_____/___.-`____.-'==
 - # `=---='
 - '''
 - @Project :pythonalgorithms
 - @File :Activationfunction.py
 - @Author :不勝人生一場醉@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() # 通過gca:get current axis得到當前軸
 - plt.rcParams['font.sans-serif'] = ['SimHei'] # 繪圖中文
 - plt.rcParams['axes.unicode_minus'] = False # 繪圖負號
 - plt.plot(x, y, label=label)
 - # 設(shè)置圖片的右邊框和上邊框為不顯示
 - ax.spines['right'].set_color('none')
 - ax.spines['top'].set_color('none')
 - # 挪動x,y軸的位置,也就是圖片下邊框和左邊框的位置
 - # data表示通過值來設(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 # 標準差為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表示標準差
 - 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ù)是一個在生物學(xué)中常見的S型函數(shù),也稱為S型生長曲線。
 - # 在信息科學(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ù)中的一個,Tanh()為雙曲正切。
 - # 在數(shù)學(xué)中,雙曲正切“Tanh”是由基本雙曲函數(shù)雙曲正弦和雙曲余弦推導(dǎo)而來。
 - # 函數(shù)tanh(藍色)和函數(shù)sigmoid(橙色)一樣,在其飽和區(qū)的接近于0,都容易產(chǎn)生后續(xù)梯度消失、計算量大的問題
 - 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會使一部分神經(jīng)元的輸出為0,這樣就造成了網(wǎng)絡(luò)的稀疏性,并且減少了參數(shù)的相互依存關(guā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ù)的一般化,用于多分類神經(jīng)網(wǎng)絡(luò)輸出。
 - def softmax(x):
 - return np.exp(x) / np.sum(np.exp(x))
 






















 
 
 







 
 
 
 