1 #neuralnetwork.py
2 #modified by Robin 2015/03/03
3
4 importnumpy as np5 from math importexp, pow6 from mpl_toolkits.mplot3d importAxes3D7 importmatplotlib.pyplot as plt8 importsys9 importcopy10 from scipy.linalg importnorm, pinv11
12 classLayer:13 '''层'''
14 def __init__(self, w, b, neure_number, transfer_function, layer_index):15 self.transfer_function =transfer_function16 self.neure_number =neure_number17 self.layer_index =layer_index18 self.w =w19 self.b =b20
21 classNetStruct:22 '''神经网络结构'''
23 def __init__(self, ni, nh, no, active_fun_list):24 #ni 输入层节点(int)
25 #ni 隐藏层节点(int 或 list)
26 #no 输出层节点(int)
27 #active_fun_list 隐藏层激活函数类型(list)
28 #==> 1
29 self.neurals = [] #各层的神经元数目
30 self.neurals.append(ni)31 ifisinstance(nh, list):32 self.neurals.extend(nh)33 else:34 self.neurals.append(nh)35 self.neurals.append(no)36 #==> 2
37 if len(self.neurals)-2 ==len(active_fun_list):38 active_fun_list.append('line')39 self.active_fun_list =active_fun_list40 #==> 3
41 self.layers = [] #所有的层
42 for i inrange(0, len(self.neurals)):43 if i ==0:44 self.layers.append(Layer([], [], self.neurals[i], 'none', i))45 continue
46 f = self.neurals[i - 1]47 s =self.neurals[i]48 self.layers.append(Layer(np.random.randn(s, f), np.random.randn(s, 1), self.neurals[i], self.active_fun_list[i-1], i))49
50 classNeuralNetwork:51 '''神经网络'''
52 def __init__(self, net_struct, mu = 1e-3, beta = 10, iteration = 100, tol = 0.1):53 '''初始化'''
54 self.net_struct =net_struct55 self.mu =mu56 self.beta =beta57 self.iteration =iteration58 self.tol =tol59
60 def train(self, x, y, method = 'lm'):61 '''训练'''
62 self.net_struct.x =x63 self.net_struct.y =y64 if(method == 'lm'):65 self.lm()66
67 defsim(self, x):68 '''预测'''
69 self.net_struct.x =x70 self.forward()71 layer_num =len(self.net_struct.layers)72 predict = self.net_struct.layers[layer_num - 1].output_val73 returnpredict74
75 def actFun(self, z, active_type = 'sigm'):76 '''激活函数'''
77 #activ_type: 激活函数类型有 sigm、tanh、radb、line
78 if active_type == 'sigm':79 f = 1.0 / (1.0 + np.exp(-z))80 elif active_type == 'tanh':81 f = (np.exp(z) + np.exp(-z)) / (np.exp(z) + np.exp(-z))82 elif active_type == 'radb':83 f = np.exp(-z *z)84 elif active_type ==