最近一直在研究神经网络,于是周末空闲之余通过一篇文章的启发制作了一个3层神经网络,用来计算加法(两个输入,3个隐含,1个输出)。训练5000000次后效果还是不错的,几乎可以计算所有和小于10的加法了。
(代码)
import numpy as np import random def sigmoid(x): return 1/(1+(np.exp(-x))) w = [] h = [] o = 0 s = 0.6 for i in range(9): w.append(random.random()) for i in range(3): h.append(0) def train(): for i in range(5000000): if i%2 == 0: x1 = random.randint(0,5)/10 x2 = random.randint(0,5)/10 y = x1 + x2 else: x1 = random.uniform(0, 5) / 10 x2 = random.uniform(0, 5) / 10 y = x1 + x2 h[0] = sigmoid(x1*w[0]+x2*w[3]) h[1] = sigmoid(x1*w[1]+x2*w[4]) h[2] = sigmoid(x1*w[2]+x2*w[5]) o = sigmoid(h[0]*w[6]+h[1]*w[7]+h[2]*w[8]) err = pow((o-y),2) c1 = -(o-y)*o*(1-o) c2 = (c1*w[6])*(h[0])*(1-h[0]) c3 = (c1*w[7])*(h[1])*(1-h[1]) c4 = (c1*w[8])*(h[2])*(1-h[2]) w[0] = w[0]+(x1*(c2)*s) w[1] = w[1]+(x1*(c3)*s) w[2] = w[2]+(x1*(c4)*s) w[3] = w[3]+(x2*(c2)*s) w[4] = w[4]+(x2*(c3)*s) w[5] = w[5]+(x2*(c4)*s) w[6] = w[6]+(h[0]*(c1)*s) w[7] = w[7]+(h[1]*(c1)*s) w[8] = w[8]+(h[2]*(c1)*s) print('Epoch:'+str(i)+' x1:'+str(x1*10)+' x2:'+str(x2*10)+' y:'+str(y*10)+' Output:'+str(o*10)+' Error:'+str(err)+' 差:'+str(abs(o-y))) while True: x1 = float(input('x1: '))/10 x2 = float(input('x2: '))/10 y = x1 + x2 h[0] = sigmoid(x1 * w[0] + x2 * w[3]) h[1] = sigmoid(x1 * w[1] + x2 * w[4]) h[2] = sigmoid(x1 * w[2] + x2 * w[5]) o = sigmoid(h[0] * w[6] + h[1] * w[7] + h[2] * w[8]) print('Output: '+str(o*10)+' y: '+str(y*10)+'\n') train()不过这个模型有2个缺陷:无法保存训练结果,以及纯手工进行计算操作(没办法,我能力有限呀...)。下个周末我会来改进的。