def backProp_bpLayer(dfm2,fm1,fm2,sfm,W1,b1,W2,b2): "return [dw,db,din] 权值的梯度,偏置值的梯度,输入数据的梯度" dfm2=dfm2*fun.dsigmoid(fm2) db2=np.zeros(b2.shape) dw2=np.zeros(W2.shape) dfm1=np.zeros(fm1.shape) for i in xrange(0,db2.shape[0]): this_fm2=fm2[i] this_dfm2=dfm2[i] db2[i]=np.sum(this_dfm2) this_w2=W2[i] this_dfm1,dw2[i]=CNN.dMultiply(this_dfm2,this_fm2,fm1,this_w2) dfm1=dfm1+this_dfm1 dfm1=dfm1*fun.dsigmoid(fm1) #dfm1=dfm1.reshape(fm1.shape) db1=np.zeros(b1.shape) dsfm=np.zeros(sfm.shape) dw1=np.zeros(W1.shape) for i in xrange(b1.shape[0]): this_dfm1=dfm1[i] this_fm1=fm1[i] db1[i]=np.sum(this_dfm1) this_dsfm,dw1[i]=fun.dconv3d(this_dfm1,this_fm1,sfm,W1[i]) dsfm=dsfm+this_dsfm return [dsfm,dw1,db1,dw2,db2]
def backProp_conv(dcfm,cfm,fm,ct,w,b): '将卷积层反向传播' 'dcfm 误差关于cfm的偏导数' 'cfm 卷基层的输出' 'fm 卷基层的输入' 'ct fm和cfm之间的连接表' 'w 卷积核' 'b 偏置' dfm=np.zeros(fm.shape) dw=np.zeros(w.shape) db=np.zeros(b.shape) dcfm=dcfm*fun.dsigmoid(cfm) for i in xrange(0,b.shape[0]): this_dcfm=dcfm[i] db[i]=np.sum(this_dcfm) for i in xrange(0,w.shape[0]): this_fm=fm[ct[0,i]] this_w=w[i] this_dcfm=dcfm[ct[1,i]] '误差关于dfm的偏导数' this_dfm=fun.dconv2_in(this_dcfm, this_w,this_w) dfm[ct[0,i]]=dfm[ct[0,i]]+this_dfm '误差关于dw的偏导数' dw[i]=fun.dconv2_kernel(this_dcfm, this_fm,this_w) return [dfm,dw,db]
def backPropSubsampling(dsfm,sfm,fm,sw,sb,pool_size,stride): dims,height,width=fm.shape dfm=np.zeros((dims,height,width)) dsw=np.zeros(sw.shape) dsb=np.zeros(sb.shape) dsfm=dsfm*fun.dsigmoid(sfm) kernel=np.ones(pool_size) for i in xrange(0,dims): this_dsfm=dsfm[i] this_kernel=kernel*sw[i] dsb[i]=np.sum(this_dsfm[:]) dsfm_beforeSubsampling=np.zeros((height-pool_size[0]+1,width-pool_size[1]+1)) dx=0 dy=0 for y in xrange(0,dsfm_beforeSubsampling.shape[0],stride): dx=0 for x in xrange(0,dsfm_beforeSubsampling.shape[1],stride): dsfm_beforeSubsampling[y,x]=this_dsfm[dy,dx] dx+=1 dy+=1 dfm[i]=fun.dconv2_in(dsfm_beforeSubsampling, fm[i],this_kernel) dthis_kernel=fun.dconv2_kernel(dsfm_beforeSubsampling,fm[i],this_kernel) dsw[i]=np.sum(dthis_kernel) return [dfm,dsw,dsb]