import numpy as np import pandas as pd def jiedian_new(qj, j): jiedian_w = [] jiedian_b = 0 if j == 0 and qj == 12: jiedian_w.append(0.49038557898997) jiedian_w.append(0.348475676796501) jiedian_w.append(0.0725879008695083) jiedian_w.append(0.837472826850604) jiedian_w.append(-0.0706798311519743) jiedian_w.append(-3.6169369170322) jiedian_w.append(-0.53557819719488) jiedian_w.append(-0.0228584789393108) jiedian_w.append(-1.71745249082217) jiedian_w.append(-1.45563751579807) jiedian_w.append(-0.555799932254451) jiedian_w.append(0.852476539980059) jiedian_b = -0.185002356132065 if j == 1 and qj == 12: jiedian_w.append(0.442372911956926) jiedian_w.append(-0.536877487857221) jiedian_w.append(1.00782536916829) jiedian_w.append(1.07196001297575) jiedian_w.append(-0.732814485632708) jiedian_w.append(0.822959617857012) jiedian_w.append(-0.453282364154155) jiedian_w.append(-0.0138979392949318) jiedian_w.append(-0.0274233258563056) jiedian_w.append(-0.426670298661898) jiedian_w.append(1.87560275441379) jiedian_w.append(-2.30528048189891) jiedian_b = 0.525676844318642 if j == 2 and qj == 12: jiedian_w.append(0.654393041569443) jiedian_w.append(-1.38856820257739) jiedian_w.append(1.24648311661583) jiedian_w.append(0.0572877158406771) jiedian_w.append(-0.183237472237546) jiedian_w.append(-0.74305066513479) jiedian_w.append(-0.460930664925325) jiedian_w.append(0.331118557255208) jiedian_w.append(0.449470835925128) jiedian_w.append(-1.29645372413246) jiedian_w.append(1.56850561324256) jiedian_w.append(-0.470667153317658) jiedian_b = -1.16862269778991 if j == 0 and qj == 3: jiedian_w.append(0.3880031194962) jiedian_w.append(0.803384989025837) jiedian_w.append(0.0292864334994403) jiedian_b = -1.43803971240614 if j == 1 and qj == 3: jiedian_w.append(0.0254467679708455) jiedian_w.append(-0.790397993881956) jiedian_w.append(1.55313793058729) jiedian_b = -1.37933790823328 jiedian_z = 0 jiedian_a = 0.5 jiedian_al = [] for i1 in range(qj): jiedian_al.append(np.random.uniform(-10, 10)) jiedian_w = np.asarray(jiedian_w) jiedian_al = np.asarray(jiedian_al) jiedian_delta = 1 jiedian_gradew = np.asarray([]) jiedian_gradeb = np.asarray([]) return np.asarray([jiedian_al, jiedian_w, jiedian_b, jiedian_z, jiedian_a, jiedian_delta, jiedian_gradew, jiedian_gradeb], dtype=object) def new_net(net_layers): net_jiedianlist_d = [] net_before = None for i in range(len(net_layers)): net_jiedianlist_x = [] i1 = net_layers[i] for j in range(i1): net_w = net_before if net_before is not None else 0 net_jiedian = jiedian_new(net_w, j) net_jiedianlist_x.append(net_jiedian) net_jiedianlist_x = np.asarray(net_jiedianlist_x, dtype=object) net_jiedianlist_d.append(net_jiedianlist_x) net_before = net_layers[i] net_jiedianlist_d = np.asarray(net_jiedianlist_d, dtype=object) return net_jiedianlist_d def getxy(): datax_pd = pd.read_excel('wl.xlsx') # 10 14 ... 264 x_xh = 10 x_xhl = [] for i in range(64): x_xhl.append(x_xh) x_xh = x_xh + 4 x_d = [] x_x1 = 1 x_x = [] y_x = [] for label, value in datax_pd.items(): if x_x1 == 4: y_x.append(value[7]) x_x1 = 1 if x_x1 == 3: for j in range(3, 7): x_x.append(value[j]) x_x1 = 4 if x_x1 == 2: for j in range(3, 7): x_x.append(value[j]) x_x1 = 3 for x_xh in x_xhl: if label.endswith(str(x_xh)): for j in range(3, 7): x_x.append(value[j]) x_x1 = 2 break if len(x_x) == 12: x_d.append(x_x) x_x = [] x_d_zl = [] for x_d1 in x_d: x_d_xl = [] x_d_xl.append(x_d1[0]) x_d_xl.append(x_d1[4]) x_d_xl.append(x_d1[8]) x_d_xl.append(x_d1[1]) x_d_xl.append(x_d1[5]) x_d_xl.append(x_d1[9]) x_d_xl.append(x_d1[2]) x_d_xl.append(x_d1[6]) x_d_xl.append(x_d1[10]) x_d_xl.append(x_d1[3]) x_d_xl.append(x_d1[7]) x_d_xl.append(x_d1[11]) x_d_zl.append(x_d_xl) datax = np.asarray(x_d_zl) y_d = [] for y_x1 in y_x: if y_x1 == 0: y_x_l = [1, 0] y_d.append(y_x_l) else: y_x_l = [0, 1] y_d.append(y_x_l) datay = np.asarray(y_d) return datax, datay def sigmoid(s): """激活函数 """ if s > 10000: return 1 if s < -10000: return 0 if s >= 0: return 1 / (1 + np.exp(-s)) else: return np.exp(s) / (1 + np.exp(s)) def danceng(dc_jiedianlist, x, y_ac): # 每层节点 分为 al(上一层输入a) w b z a before_layer = None rsy = [] for layer in range(len(dc_jiedianlist)): layer1 = dc_jiedianlist[layer] if before_layer is None: for i, jiedian1 in enumerate(layer1): jiedian1[3] = x[i] jiedian1[4] = x[i] if before_layer is not None: al1 = [] for i, jiedian in enumerate(before_layer): al1.append(jiedian[4]) for i, jiedian in enumerate(layer1): w = np.asarray(jiedian[1]).reshape(1, -1) al2 = np.asarray(al1).reshape(-1, 1) z = np.dot(w, al2) + jiedian[2] z1 = np.sum(w * al2) + jiedian[2] z = z[0][0] a = sigmoid(z) if layer == len(dc_jiedianlist) - 1: rsy.append(a) jiedian[0] = al2 jiedian[3] = z jiedian[4] = a before_layer = layer1 deltalayerbefore = None for layer in range(len(dc_jiedianlist)): layer1 = dc_jiedianlist[len(dc_jiedianlist) - 1 - layer] if len(dc_jiedianlist) - 1 - layer == 0: break if deltalayerbefore is None: y_aclx = [x for x in y_ac] for i, jiedian1 in enumerate(layer1): jiedian1[5] = (jiedian1[4]-y_aclx[i]) * jiedian1[4] * (1 - jiedian1[4]) allist = jiedian1[0].ravel() # jiedian61 = jiedian1[6].ravel() # jiedian62 = (jiedian1[5] * allist).ravel() # jiedian63 = jiedian62 if len(jiedian61) == 0 else np.asarray(jiedian61, jiedian62) jiedian1[6] = np.append(jiedian1[6], jiedian1[5] * allist, axis=0) jiedian1[7] = np.append(jiedian1[7], jiedian1[5]) else: for i, jiedian1 in enumerate(layer1): w_ql = [] delta_ql = [] for _, jiedian1_b in enumerate(deltalayerbefore): w_ql.append(jiedian1_b[1][i]) delta_ql.append(jiedian1_b[5]) w_qln = np.asarray(w_ql).reshape(1, -1) delta_qln = np.asarray(delta_ql).reshape(-1, 1) z = np.dot(w_qln, delta_qln) z1 = z[0][0] jiedian1[5] = z1 * jiedian1[4] * (1 - jiedian1[4]) allist = jiedian1[0].ravel() jiedian1[6] = np.append(jiedian1[6], jiedian1[5] * allist, axis=0) jiedian1[7] = np.append(jiedian1[7], jiedian1[5]) deltalayerbefore = layer1 rc = 0 for ri in range(len(rsy)): rc = rc + 1/2*(rsy[ri] - y_ac[ri])**2 return rc, rsy, dc_jiedianlist def updatewb(upnet, learning_rate): for layer in range(len(upnet)): layer1 = upnet[layer] if layer > 0: for i, jiedian in enumerate(layer1): w = jiedian[1] b = jiedian[2] dw = np.sum(jiedian[6].reshape(-1, len(w)),axis=0) db = np.sum(jiedian[7]) jiedian[1] = w - dw * learning_rate jiedian[2] = b - db * learning_rate jiedian[6] = np.asarray([]) jiedian[7] = np.asarray([]) return upnet cxlayer = [12, 3, 2] cxnet = new_net(cxlayer) cxx, cxy = getxy() for zongcishu in range(1000): jscj = 0 for dancenghangshu in range(cxx.shape[0]): jsc, jsy, cxnet = danceng(cxnet, cxx[dancenghangshu], cxy[dancenghangshu]) jscj = jscj + jsc learning_rate = 0.2 cxnet = updatewb(cxnet, learning_rate) print(cxnet) if zongcishu == 49: print('a')
测试结果与书中附的excel计算结果相同