神经网络理论基础及 Python 实现

www.jianshu.com/p/4824a45fc87d#

……

4.1、算法详细介绍

1、由输入层向前传送：

Ij为当前层单元值，Oi为上一层的单元值，wij为两层之间，连接两个单元值的权重值，sitaj为每一层的偏向值。我们要对每一层的输出进行非线性的转换，示意图如下：

2、根据误差反向传送 对于输出层：其中Tk是真实值，Ok是预测值

3、终止条件

4、非线性转化函数

（1）tanh(x)函数：

tanh(x)=sinh(x)/cosh(x)

sinh(x)=(exp(x)-exp(-x))/2

cosh(x)=(exp(x)+exp(-x))/2

（2）逻辑函数，本文上面用的就是逻辑函数

import numpy as np

def tanh(x):

returnnp.tanh(x)

def tanh_deriv(x):

return1.0-np.tanh(x)*np.tanh(x)

def logistic(x):

return1/(1+np.exp(-x))

def logistic_derivative(x):

returnlogistic(x)*(1-logistic(x))

classNeuralNetwork:

def __init__(self,layers,activation='tanh'):

"""

:param layers: A list containing the number of units in each layer.

Should be at least two values

:param activation: The activation function to be used. Can be

"logistic" or "tanh"

"""

ifactivation=='logistic':

self.activation=logistic

self.activation_deriv=logistic_derivative

elifactivation=='tanh':

self.activation=tanh

self.activation_deriv=tanh_deriv

self.weights=[]

foriinrange(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):

X=np.atleast_2d(X)

temp=np.ones([X.shape[],X.shape[1]+1])

temp[:,:-1]=X

X=temp

y=np.array(y)

forkinrange(epochs):

a=[X[i]]

forlinrange(len(self.weights)):

a.append(self.activation(np.dot(a[l],self.weights[l])))

error=y[i]-a[-1]

deltas=[error *self.activation_deriv(a[-1])]

forlinrange(len(a)-2,,-1):

deltas.append(deltas[-1].dot(self.weights[l].T)*self.activation_deriv(a[l]))

deltas.reverse()

foriinrange(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[]+1)

temp[:-1]=x

a=temp

forlinrange(,len(self.weights)):

a=self.activation(np.dot(a,self.weights[l]))

returna

from BP import NeuralNetwork

import numpyasnp

nn=NeuralNetwork([2,2,1],'tanh')

x=np.array([[,],[,1],[1,],[1,1]])

y=np.array([1,,,1])

nn.fit(x,y,0.1,10000)

foriin[[,],[,1],[1,],[1,1]]:

print(i,nn.predict(i))

• 发表于:
• 原文链接http://kuaibao.qq.com/s/20180104B0MQ6R00?refer=cp_1026
• 腾讯「云+社区」是腾讯内容开放平台帐号（企鹅号）传播渠道之一，根据《腾讯内容开放平台服务协议》转载发布内容。

2019-10-22

2019-10-22

2019-10-22

2019-10-22

2019-10-22

2019-10-22

2018-06-01

2019-10-22

2019-10-22

2019-10-22

2019-10-22

2019-10-22