# BP神经网络

# -*- coding: utf-8 -*-
"""
Created on Tue Jan 17 12:22:53 2017

@author: DaiPuWei
"""

'''
神经网络模型---------BP神经网络,以蠓虫(Af,Apf两种类别)分类为例
利用梯度下降法为例
'''

import pandas as pd
import math
import random

def make_matrix(row,col,fill=0.0):
'''
构造m*n矩阵的函数,用来生成神经网络模型中的权值等矩阵
'''

mat = []
for i in range(row):
mat.append([fill]*col)
return mat

def sigmoid(alpha,x):
'''
神经网络模型的激励函数f(x) = 1/(1+e^(ax))
'''

y = 1.0 / (1 + math.exp(-1*alpha * x))
return y

def sigmod_derivate(alpha,x):
'''
神经网络模型的激励函数f(x) = 1/(1+e^(-ax))的导数:
g(x) = a*{1-1/[1+e^(-ax)]}
'''

y = alpha * (1.0 - 1.0/(1+math.exp(-1*alpha*x)))
return y

class BPNeuralNetwork:
'''
BP神经网络模型的具体类
'''

def __init__(self):
'''
BP神经网络的构造函数
input_n是输入神经元的个数
hidden_n是中间神经元的个数
output_n是输出神经元的个数
input_cells是输入神经元
hidden_cells是输入神经元
output_cells是输入神经元
input_correction是输入神经元的矫正系数
output_correction是输入神经元的矫正系数
'''
self.input_n = 0
self.hidden_n = 0
self.output_n = 0
self.input_cells = []
self.hidden_cells = []
self.output_cells = []
self.input_weights = []
self.output_weights = []
self.input_correction = []
self.output_correction = []

def setup(self, input_number, hidden_number, output_number):
'''
BP神经网络的相关参数的初始化函数
'''

# input_n加1是为了引入阈值
self.input_n = input_number + 1
self.hidden_n = hidden_number
self.output_n = output_number

# init cells
self.input_cells = [1.0] * self.input_n
self.hidden_cells = [1.0] * self.hidden_n
self.output_cells = [1.0] * self.output_n

# init weight
self.input_weights = make_matrix(self.input_n, self.hidden_n)
self.output_weights = make_matrix(self.hidden_n, self.output_n)

# random activate
for i in range(self.input_n):
for j in range(self.hidden_n):
self.input_weights[i][j] = random.uniform(-0.2,0.2)
for i in range(self.hidden_n):
for j in range(self.output_n):
self.input_weights[i][j] = random.randint(-2,2)

# init correction matrix
self.input_correction = make_matrix(self.input_n, self.hidden_n)
self.output_correction = make_matrix(self.hidden_n, self.output_n)

def predict(self,inputs,alpha):
'''
向输入神经元输入数据进行学习，计算各层神经元的值
alpha是激励函数的相关系数
inputs是输入数据
'''

# activate input layer
for i in range(self.input_n-1):
self.input_cells[i] = inputs[i]
# activate hidden layer
for j in range(self.hidden_n):
total = 0.0
for i in range(self.input_n):
total = total + self.input_cells[i] * self.input_weights[i][j]
self.hidden_cells[j] = sigmoid(alpha,total)
# activate output layer
for k in range(self.output_n):
total =  0.0
for j in range(self.hidden_n):
total = total + self.hidden_cells[j] * self.output_weights[j][k]
self.output_cells[k] = sigmoid(alpha,total)
return self.output_cells[:]

def back_propagate(self,inputs,ideal_output,learn,correct,alpha):
'''
这是向后反馈的函数，计算
inputs是输入数据
ideal_output是理想输出
learn是学习效率
correct是矫正系数
alpha是激励函数相关系数
'''

# feed forward
self.predict(inputs,alpha)

# get output layer error
output_deltas = [0.0] * self.output_n
for o in range(self.output_n):
error = ideal_output[o] - self.output_cells[o]
output_deltas[o] = sigmod_derivate(alpha,self.output_cells[o]) * error

# get hidden layer error
hidden_deltas = [0.0] * self.hidden_n
for h in range(self.hidden_n):
error = 0.0
for o in range(self.output_n):
error = error + output_deltas[o] - self.output_weights[h][o]
hidden_deltas[h] = sigmod_derivate(alpha,self.hidden_cells[h]) * error

# update output weights
for h in range(self.hidden_n):
for o in range(self.output_n):
change = output_deltas[o] * self.hidden_cells[h]
self.output_weights[h][o] = self.output_weights[h][o] + learn *change + correct *self.output_correction[h][o]
self.output_correction[h][o] = change

# update  input weights
for i in range(self.input_n):
for h in range(self.hidden_n):
change = hidden_deltas[h] * self.input_cells[i]
self.input_weights[i][h] = self.input_weights[i][h] + learn *change + correct *self.input_correction[i][h]
self.output_correction[h][o] = change

# get global error
error = 0.0
for o in range(len(ideal_output)):
error = error + 0.5 * (ideal_output[o]-self.output_cells[o])**2
return error

def train(self,inputs,ideal_outputs,alpha,limit = 10000,learn = 0.05,correct = 0.1):
'''
BP神经网络模型训练函数
inputs是输入数据
ideal_outputs是理想输出
limit是训练次数，默认是10000次
correct是矫正系数
'''

for i in range(limit):
error = 0.0
for i in range(len(inputs)):
ideal_output = ideal_outputs[i]
input_data = inputs[i]
error = error + self.back_propagate(input_data,ideal_output,learn,correct,alpha)

def test(self):
'''
BP神经网络模型测试算法

'''

#读取文件数据
with pd.ExcelFile('./蠓虫分类.xlsx') as fin:

#数据整理
Af_len = len(Af)
Apf_len = len(Apf)
test_len = len(test_data)

inputs = []
for i in range(Af_len):
tmp = []
tmp.append(Af['触角'][i])
tmp.append(Af['翅膀长度'][i])
inputs.append(tmp)
for i in range(Apf_len):
tmp = []
tmp.append(Apf['触角'][i])
tmp.append(Apf['翅膀长度'][i])
inputs.append(tmp)
for i in range(test_len):
tmp = []
tmp.append(test_data['触角'][i])
tmp.append(test_data['翅膀长度'][i])
inputs.append(tmp)

#理想输出
ideal_outputs = []
for i in range(Af_len):
tmp = [1,0]
ideal_outputs.append(tmp)
for i in range(Apf_len):
tmp = [0,1]
ideal_outputs.append(tmp)
for i in range(test_len):
tmp = [0,0]
ideal_outputs.append(tmp)

self.setup(2,3,2)
limit = 10000
learn = 0.05
correct = 0.1
alpha = 0.7
self.train(inputs,ideal_outputs,alpha,limit,learn,correct)

for i in range(len(inputs)-test_len,len(inputs)):
input = inputs[i]
result = self.predict(input,alpha)
print(result)

if __name__ == '__main__':
BP = BPNeuralNetwork()
BP.test()


#### 一文读懂BP神经网络

2016-11-24 18:49:44

#### BP神经网络（完整的理论和经验公式）

2017-02-07 20:16:16

#### 简单易学的机器学习算法——神经网络之BP神经网络

2014-06-21 11:49:19

#### BP神经网络（推荐）

2017-03-25 17:21:47

#### BP神经网络公式推导及实现(MNIST)

2015-12-26 11:32:40

#### BP神经网络学习过程

2016-06-30 23:14:59

#### BP神经网络例子（可直接运行）

2017年03月27日 3KB 下载

#### BP神经网络基本原理

2005-11-22 17:06:00

#### 神经网络学习 之 BP神经网络

2015-11-30 21:17:56

#### BP神经网络稳定性分析

2017年09月19日 597KB 下载