神经网络算法实现
                            
                            
                            1. 關(guān)于非線性轉(zhuǎn)化方程(non-linear transformation function) 
sigmoid函數(shù)(S 曲線)用來作為activation function:
1.1 雙曲函數(shù)(tanh) tanh是雙曲函數(shù)中的一個(gè),tanh()為雙曲正切。在數(shù)學(xué)中,雙曲正切“tanh”是由基本雙曲函數(shù)雙曲正弦和雙曲余弦推導(dǎo)而來 公式 定義 雙曲正切函數(shù)是雙曲函數(shù)中的一個(gè)函數(shù)。 定義域和值域 函數(shù):y=tanh x;定義域:R,值域:(-1,1)。 y=tanh x是一個(gè)奇函數(shù),其函數(shù)圖像為過原點(diǎn)并且穿越Ⅰ、Ⅲ象限的嚴(yán)格單調(diào)遞增曲線,其圖像被限制在兩水平漸近線y=1和y=-1之間。 周期性 雙曲正切函數(shù)y=tanhx,其不是周期函數(shù)。 導(dǎo)數(shù) 雙曲正切函數(shù)的導(dǎo)數(shù)公式: ? ? ? ? ? ? 求導(dǎo):f(x)'=1-f(x)*f(x) 1.2 ?邏輯函數(shù)(logistic function) Logistic函數(shù)或Logistic曲線是一種常見的S形函數(shù),它是皮埃爾·弗朗索瓦·韋呂勒在1844或1845年在研究它與人口增長(zhǎng)的關(guān)系時(shí)命名的。廣義Logistic曲線可以模仿一些情況人口增長(zhǎng)(P)的S形曲線。起初階段大致是指數(shù)增長(zhǎng); 然后隨著開始變得飽和,增加變慢;最后,達(dá)到成熟時(shí)增加停止。 公式:d d x f ( x ) = f ( x ) ( 1 ? f ( x ) ) {\displaystyle {\frac ze8trgl8bvbq{dx}}f(x)=f(x)(1-f(x))}  
 
 
 
2. 實(shí)現(xiàn)一個(gè)簡(jiǎn)單的神經(jīng)網(wǎng)絡(luò)算法
#!/usr/bin/env python #-*-coding:utf-8-*- #神經(jīng)網(wǎng)絡(luò)的實(shí)現(xiàn)方法import numpy as np #定義雙曲函數(shù) def tanh(x):return np.tanh(x) #雙曲線求導(dǎo) def tanh_deriv(x):return 1.0-np.tanh(x)*np.tanh(x) #邏輯函數(shù) def logistic(x):return 1/(1+np.exp(-x)) #求導(dǎo) def logistic_derivative(x):return logistic(x)*(1-logistic(x))class NeuralNetwork:def __init__(self,layers,activation='tanh'):if activation == 'logistic':self.activation=logisticself.activation_deriv=logistic_derivativeelif activation == 'tanh':self.activation=tanhself.activation_deriv=tanh_derivself.weights=[]#初始化權(quán)重for i in range(1,len(layers)-1):self.weights.append((2*np.random.random((layers[i-1]+1,layers[i]+1))-1)*0.25)self.weights.append((2*np.random.random((layers[i]+1,layers[i+1]))-1)*0.25)def fit(self,X,y,learning_rate=0.2,epochs=10000):#每次隨機(jī)抽取epochs個(gè)實(shí)例X=np.atleast_2d(X)temp=np.ones([X.shape[0],X.shape[1]+1])temp[:,0:-1]=XX=temp#bias初值y=np.array(y)for k in range(epochs):#隨機(jī)抽取每行i=np.random.randint(X.shape[0])a=[X[i]]#更新的實(shí)例#正向更新for l in range(len(self.weights)):a.append(self.activation(np.dot(a[l],self.weights[l])))error=y[i]-a[-1]#反向傳送最后一個(gè)錯(cuò)誤率deltas=[error*self.activation_deriv(a[-1])]#輸出層Errj=Oj(1-Oj)(Tj-Oj)#根據(jù)誤差反向傳送#隱藏層for l in range(len(a)-2,0,-1):deltas.append(deltas[-1].dot(self.weights[l].T)*self.activation_deriv(a[l]))deltas.reverse()#更新權(quán)重for i in range(len(self.weights)):layer=np.atleast_2d(a[i])delta=np.atleast_2d(deltas[i])self.weights[i]+=learning_rate*layer.T.dot(delta)def predict(self,x):x=np.array(x)temp=np.ones(x.shape[0]+1)temp[0:-1]=xa=tempfor l in range(0,len(self.weights)):a=self.activation(np.dot(a,self.weights[l]))return a
 
 
 
                            
                        
                        
                        sigmoid函數(shù)(S 曲線)用來作為activation function:
1.1 雙曲函數(shù)(tanh) tanh是雙曲函數(shù)中的一個(gè),tanh()為雙曲正切。在數(shù)學(xué)中,雙曲正切“tanh”是由基本雙曲函數(shù)雙曲正弦和雙曲余弦推導(dǎo)而來 公式 定義 雙曲正切函數(shù)是雙曲函數(shù)中的一個(gè)函數(shù)。 定義域和值域 函數(shù):y=tanh x;定義域:R,值域:(-1,1)。 y=tanh x是一個(gè)奇函數(shù),其函數(shù)圖像為過原點(diǎn)并且穿越Ⅰ、Ⅲ象限的嚴(yán)格單調(diào)遞增曲線,其圖像被限制在兩水平漸近線y=1和y=-1之間。 周期性 雙曲正切函數(shù)y=tanhx,其不是周期函數(shù)。 導(dǎo)數(shù) 雙曲正切函數(shù)的導(dǎo)數(shù)公式: ? ? ? ? ? ? 求導(dǎo):f(x)'=1-f(x)*f(x) 1.2 ?邏輯函數(shù)(logistic function) Logistic函數(shù)或Logistic曲線是一種常見的S形函數(shù),它是皮埃爾·弗朗索瓦·韋呂勒在1844或1845年在研究它與人口增長(zhǎng)的關(guān)系時(shí)命名的。廣義Logistic曲線可以模仿一些情況人口增長(zhǎng)(P)的S形曲線。起初階段大致是指數(shù)增長(zhǎng); 然后隨著開始變得飽和,增加變慢;最后,達(dá)到成熟時(shí)增加停止。 公式:
求導(dǎo):
2. 實(shí)現(xiàn)一個(gè)簡(jiǎn)單的神經(jīng)網(wǎng)絡(luò)算法
#!/usr/bin/env python #-*-coding:utf-8-*- #神經(jīng)網(wǎng)絡(luò)的實(shí)現(xiàn)方法import numpy as np #定義雙曲函數(shù) def tanh(x):return np.tanh(x) #雙曲線求導(dǎo) def tanh_deriv(x):return 1.0-np.tanh(x)*np.tanh(x) #邏輯函數(shù) def logistic(x):return 1/(1+np.exp(-x)) #求導(dǎo) def logistic_derivative(x):return logistic(x)*(1-logistic(x))class NeuralNetwork:def __init__(self,layers,activation='tanh'):if activation == 'logistic':self.activation=logisticself.activation_deriv=logistic_derivativeelif activation == 'tanh':self.activation=tanhself.activation_deriv=tanh_derivself.weights=[]#初始化權(quán)重for i in range(1,len(layers)-1):self.weights.append((2*np.random.random((layers[i-1]+1,layers[i]+1))-1)*0.25)self.weights.append((2*np.random.random((layers[i]+1,layers[i+1]))-1)*0.25)def fit(self,X,y,learning_rate=0.2,epochs=10000):#每次隨機(jī)抽取epochs個(gè)實(shí)例X=np.atleast_2d(X)temp=np.ones([X.shape[0],X.shape[1]+1])temp[:,0:-1]=XX=temp#bias初值y=np.array(y)for k in range(epochs):#隨機(jī)抽取每行i=np.random.randint(X.shape[0])a=[X[i]]#更新的實(shí)例#正向更新for l in range(len(self.weights)):a.append(self.activation(np.dot(a[l],self.weights[l])))error=y[i]-a[-1]#反向傳送最后一個(gè)錯(cuò)誤率deltas=[error*self.activation_deriv(a[-1])]#輸出層Errj=Oj(1-Oj)(Tj-Oj)#根據(jù)誤差反向傳送#隱藏層for l in range(len(a)-2,0,-1):deltas.append(deltas[-1].dot(self.weights[l].T)*self.activation_deriv(a[l]))deltas.reverse()#更新權(quán)重for i in range(len(self.weights)):layer=np.atleast_2d(a[i])delta=np.atleast_2d(deltas[i])self.weights[i]+=learning_rate*layer.T.dot(delta)def predict(self,x):x=np.array(x)temp=np.ones(x.shape[0]+1)temp[0:-1]=xa=tempfor l in range(0,len(self.weights)):a=self.activation(np.dot(a,self.weights[l]))return a
總結(jié)
 
                            
                        - 上一篇: Ecilpse常用快捷键
- 下一篇: html之figure元素和figcap
