import math
import random
def rand(a, b):
return (b - a) * random.random() + a
def make_matrix(m, n, fill=0.0): # 创造一个指定大小的矩阵
mat = []
for i in range(m):
mat.append([fill] * n)
return mat
def sigmoid(x):
return 1.0 / (1.0 + math.exp(-x))
def sigmod_derivate(x):
return x * (1 - x)
class BPnet(object):
def setup(self, ni, nh, no):
self.input_n = ni + 1 # 因为需要多加一个偏置神经元,提供一个可控的输入修正
self.hidden_n = nh
self.output_n = no
# 初始化神经元
self.input_cells = self.input_n * [1.0]
self.hidden_cells = self.hidden_n * [1.0]
self.output_cells = self.output_n * [1.0]
# 初始化权重矩阵
self.input_weights = make_matrix(self.input_n, self.hidden_n)
self.output_weights = make_matrix(self.hidden_n, self.output_n)
# 权重矩阵随机激活
for i in range(self.input_n):
for h in range(self.hidden_n):
self.input_weights[i][h] = rand(-0.2, 0.2)
for h in range(self.hidden_n):
for o in range(self.output_n):
self.output_weights[h][o] = rand(-0.2, 0.2)
# 初始化矫正矩阵
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):
# 激活输入层
for i in range(self.input_n - 1):
self.input_cells[i] = inputs[i]
# 激活隐藏层
for j in range(self.hidden_n):
total = 0.0
for i in range(self.input_n):
total += self.input_cells[i] * self.input_weights[i][j]
self.hidden_cells[j] = sigmoid(total)
for k in range(self.output_n):
total = 0.0
for j in range(self.hidden_n):
total += self.hidden_cells[j] * self.output_weights[j][k]
self.output_cells[k] = sigmoid(total)
return self.output_cells[:]
def back_propagate(self, case, label, learn, correct):
# 前馈
self.predict(case)
# 获取输出层误差
output_deltas = [0.0] * self.output_n
for o in range(self.output_n):
error = label[o] - self.output_cells[o]
output_deltas[o] = sigmod_derivate(self.output_cells[o]) * 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 += output_deltas[o] * self.output_weights[h][o]
hidden_deltas[h] = sigmod_derivate(self.hidden_cells[h]) * error
# 更新输出权重
for h in range(self.hidden_n):
for o in range(self.output_n):
# Wij=Wij+λEjOi+μCij
change = output_deltas[o] * self.hidden_cells[h]
self.output_weights[h][o] += learn * change + correct * self.output_correction[h][o]
# 更新输入权重
for i in range(self.input_n):
for h in range(self.hidden_n):
# Wij=Wij+λEjOi+μCij
change = hidden_deltas[h] * self.input_cells[i]
self.input_weights[i][h] += learn * change + correct * self.input_correction[i][h]
self.input_correction[i][h] = change
# 获取全局误差
error = 0.0
for o in range(len(label)):
error += 0.5 * (label[o] - self.output_cells[o]) ** 2
return error
def train(self, cases, labels, limit=10000, learn=0.05, correct=0.1):
for i in range(limit):
# if i % 1000 == 0:
# print("iterator: " + str(i))
# print(self.output_weights)
error = 0.0
for i in range(len(cases)):
label = labels[i]
case = cases[i]
error += self.back_propagate(case, label, learn, correct)
def test(self):
cases = [
[0, 0],
[0, 1],
[1, 0],
[1, 1],
]
labels = [[0], [1], [1], [1]]
self.setup(2, 2, 1) # 设置各层的神经元数量
self.train(cases, labels, 10000, 0.5, 0.1)
for case in cases:
print(self.predict(case))
if __name__ == '__main__':
net = BPnet()
net.setup(2, 2, 1)
cases = [
[0, 0],
[0, 1],
[1, 0],
[1, 1],
]
labels = [[0], [1], [1], [1]]
for iter in range(100000):
for index in range(len(cases)):
case = cases[index]
predict = net.predict(case)
backward = net.back_propagate(case, labels[index], 0.5, 0.1)
if iter==0:
print(predict)
print(net.predict([0,0]))
print(net.predict([0, 1]))
print(net.predict([1, 0]))
print(net.predict([1, 1]))
net.test()
训练十万次的结果