import numpy as np import pandas as pd # 卷积层 输出层有w b 池化层没有 # 卷积层为3×3=9,9+1=10,增加1个偏置 # 输出层和卷积层有关系,以3×3为例,卷积层为原来的偶数(n-2)**2 # 池化层则为(n-2)**2/4 # 输出层w为结果池化层的结果,b为偏置 # 输出有几个,输出w,b有几组 # 卷积网络先要确定数据维度,必须为n×n的形式,在确定用3×3卷积,有几个卷积 # X的shape[0]=shape[1]记为n,卷积层数量记为jjs,输出结果记为jgs def sigmoid(s): """激活函数 """ if s >= 0: return 1 / (1 + np.exp(-s)) else: return np.exp(s) / (1 + np.exp(s)) def np_sigmoid(nps): vfunc = np.vectorize(sigmoid) return vfunc(nps) jjs = 3 jgs = 3 jjc_wb = [] for i in range(jjs): jjc_w = [] for j in range(9): # 3×3卷积 zd_w = np.random.uniform(-10, 10) jjc_w.append(zd_w) jjc_b = np.random.uniform(-10, 10) jjc_xw = [jjc_w, jjc_b] jjc_wb.append(jjc_xw) n = 6 # n为X.shape[0] 3×3卷积 chjg = (n-2)**2/4 sccwsl = int(jjs*chjg) scc_wb = [] for i in range(jjs): scc_w = [] for j in range(sccwsl): # 3×3卷积 zd_w = np.random.uniform(-10, 10) scc_w.append(zd_w) scc_b = np.random.uniform(-10, 10) scc_xw = [scc_w, scc_b] scc_wb.append(scc_xw) datax_pd = pd.read_excel('5_qiujieqi.xlsx') x_xh = 11 x_xhl = [] for i in range(96): x_xhl.append(x_xh) x_xh = x_xh + 6 x_d = [] x_x1 = 1 x_x = [] y_x = [] for label, value in datax_pd.items(): if x_x1 == 6: for j in range(1, 7): x_x.append(value[j]) x_x1 = 1 if x_x1 == 5: for j in range(1, 7): x_x.append(value[j]) x_x1 = 6 if x_x1 == 4: for j in range(1, 7): x_x.append(value[j]) x_x1 = 5 if x_x1 == 3: for j in range(1, 7): x_x.append(value[j]) x_x1 = 4 if x_x1 == 2: for j in range(1, 7): x_x.append(value[j]) x_x1 = 3 for x_xh in x_xhl: if x_x1 == 1 and label.endswith(str(x_xh)): for j in range(1, 7): x_x.append(value[j]) y_x.append([value[7], value[8], value[9]]) x_x1 = 2 break if len(x_x) > 37: print('a') if len(x_x) == 36: x_d.append(x_x) x_x = [] x_d_zl = [] for x_d1 in x_d: x_d_xl = [] for i in range(6): for j in range(6): x_d_xl.append(x_d1[i+j*6]) x_d_zl.append(x_d_xl) datax = np.asarray(x_d_zl) datay = np.asarray(y_x) jjc_wbqj = [[[1.161269386,-0.847760033,1.35625076, 5.824526019,-14.5709768,-6.945467562, 4.389375941,8.316772552,1.213804203 ], -14.70601046 ], [[1.950374681,14.20993825,5.029097444, 4.343936633,-1.470569271,-12.47776145, -1.345575361,-5.858890856,-2.407511485 ], -13.57167429 ], [[-0.784801342,4.15936062,-0.542494559, 0.467923926,-8.464882649,-4.507941607, 0.183396612,-2.567196568,0.230537979 ], -4.879271091 ]] scc_wbqj =[ [[-0.31727899,0.151466135, -3.223004932,1.32264779, -25.8766921,-0.49388284, 0.402471712,-1.570998118, -3.660733315,1.908146602, 0.008517898,-0.158523627 ],14.76357831], [[0.010053797,-0.641899938, -11.06928236,9.000875923, 12.09619334,0.003477926, -0.352090359,10.1593121, 0.429831101,1.671591263, 2.059677,-0.092649267 ],-21.11607069], [[2.774635859,-0.233485702, 43.6159215,0.045105185, 0.755282449,0.212502069, -0.138444151,-8.730724507, -1.085594612,-0.850629297, -0.166869274,-3.018887224], -9.164268267] ] jjc_wbqjnp= np.asarray(jjc_wbqj, dtype=object) scc_wbqjnp = np.asarray(scc_wbqj, dtype=object) jisuany = [] for i in range(datax.shape[0]): outc=[] wc = 0 for j in range(len(jjc_wbqj)): jjc_sr = datax[i].reshape(6, 6) jjc_z = [] for i0 in range(jjc_sr.shape[0] - 2): for j0 in range(jjc_sr.shape[1] - 2): x_1 = np.asarray([jjc_sr[i0, j0], jjc_sr[i0, j0 + 1], jjc_sr[i0, j0 + 2]]) x_2 = np.asarray([jjc_sr[i0 + 1, j0], jjc_sr[i0 + 1, j0 + 1], jjc_sr[i0 + 1, j0 + 2]]) x_3 = np.asarray([jjc_sr[i0 + 2, j0], jjc_sr[i0 + 2, j0 + 1], jjc_sr[i0 + 2, j0 + 2]]) xij = np.asarray([x_1, x_2, x_3]) jjc_z.append(xij) jjc_z = np.asarray(jjc_z) b = jjc_wbqj[j][1] a = [] for i1 in range(jjc_z.shape[0]): w_f1 = np.asarray(jjc_wbqj[j][0]).reshape(3,3) a1 = np.sum(w_f1 * jjc_z[i1]) + b a.append(a1) a = np.asarray(a).reshape(4, 4) a = np_sigmoid(a) p = [] for i2 in range(a.shape[0]): for j2 in range(a.shape[1]): if i2 % 2 == 0 and j2 % 2 == 0: p_1 = np.asarray([a[i2, j2], a[i2, j2 + 1]]) p_2 = np.asarray([a[i2 + 1, j2], a[i2 + 1, j2 + 1]]) pij = np.max([p_1, p_2]) p.append(pij) outc.append(p) outc = np.asarray(outc).ravel() outr = [] for outi in range(len(scc_wbqjnp)): sccw = scc_wbqj[outi][0] sccb = scc_wbqj[outi][1] sccw1 = sccw * outc sccr = np.sum(sccw * outc) + sccb outr.append(sccr) outr = np_sigmoid(outr) jisuany.append(outr) datay_c1 = np.sum(outr - datay) datay_c2 = datay_c1 ** 2 * 0.5 wc = wc + datay_c2 jisuany = np.asarray(jisuany) print('a')
《深度学习的数学》涌井良幸等著第五章求解器python实现
最新推荐文章于 2024-07-07 18:46:11 发布