灰色神经网络python_可变多隐层神经网络的python实现

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 == 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值