def backprop(ANFISObj, columnX, columns, theWSum, theW, theLayerFive): paramGrp = [0]* len(ANFISObj.memFuncs[columnX]) for MF in range(len(ANFISObj.memFuncs[columnX])): parameters = np.empty(len(ANFISObj.memFuncs[columnX][MF][1])) timesThru = 0 for alpha in sorted(ANFISObj.memFuncs[columnX][MF][1].keys()): bucket3 = np.empty(len(ANFISObj.X)) for rowX in range(len(ANFISObj.X)): varToTest = ANFISObj.X[rowX,columnX] tmpRow = np.empty(len(ANFISObj.memFuncs)) tmpRow.fill(varToTest) bucket2 = np.empty(ANFISObj.Y.ndim) for colY in range(ANFISObj.Y.ndim): rulesWithAlpha = np.array(np.where(ANFISObj.rules[:,columnX]==MF))[0] adjCols = np.delete(columns,columnX) senSit = mfDerivs.partial_dMF(ANFISObj.X[rowX,columnX],ANFISObj.memFuncs[columnX][MF],alpha) # produces d_ruleOutput/d_parameterWithinMF dW_dAplha = senSit * np.array([np.prod([ANFISObj.memClass.evaluateMF(tmpRow)[c][ANFISObj.rules[r][c]] for c in adjCols]) for r in rulesWithAlpha]) bucket1 = np.empty(len(ANFISObj.rules[:,0])) for consequent in range(len(ANFISObj.rules[:,0])): fConsequent = np.dot(np.append(ANFISObj.X[rowX,:],1.),ANFISObj.consequents[((ANFISObj.X.shape[1] + 1) * consequent):(((ANFISObj.X.shape[1] + 1) * consequent) + (ANFISObj.X.shape[1] + 1)),colY]) acum = 0 if consequent in rulesWithAlpha: acum = dW_dAplha[np.where(rulesWithAlpha==consequent)] * theWSum[rowX] acum = acum - theW[consequent,rowX] * np.sum(dW_dAplha) acum = acum / theWSum[rowX]**2 bucket1[consequent] = fConsequent * acum sum1 = np.sum(bucket1) if ANFISObj.Y.ndim == 1: bucket2[colY] = sum1 * (ANFISObj.Y[rowX]-theLayerFive[rowX,colY])*(-2) else: bucket2[colY] = sum1 * (ANFISObj.Y[rowX,colY]-theLayerFive[rowX,colY])*(-2) sum2 = np.sum(bucket2) bucket3[rowX] = sum2 sum3 = np.sum(bucket3) parameters[timesThru] = sum3 timesThru = timesThru + 1 paramGrp[MF] = parameters return paramGrp
def backprop(ANFISObj, columnX, columns, theWSum, theW, theLayerFive): """ Parameters ---------- ANFISObj: anfis对象 columnX: 第X个变量id columns:列表[0, 1, 2,...,len(n)-1],共n个变量 theWSum:第二层的和 theW: 第二层 theLayerFive:第五层 Returns ------- paramGrp:bp后的参数Group,[[p1],[p2],[p3],...,[pn]]。共n个mf,[pi]为第i个mf的参数列表。 """ paramGrp = [0] * len(ANFISObj.memFuncs[columnX]) for MF in range(len(ANFISObj.memFuncs[columnX])): parameters = np.empty(len(ANFISObj.memFuncs[columnX][MF][1])) timesThru = 0 # alpha为每个mf的参数。 for alpha in sorted(ANFISObj.memFuncs[columnX][MF][1].keys()): # bucket3用于存放每个inputcase对当前MF-alpha参数的修正值 bucket3 = np.empty(len(ANFISObj.X)) for rowX in range(len(ANFISObj.X)): # 这些是错误语句所用到的变量,现已失效 # varToTest = ANFISObj.X[rowX, columnX] # tmpRow = np.empty(len(ANFISObj.memFuncs)) # tmpRow.fill(varToTest) # bucket2用于存放当前inputcase(即第rowX个input)下,对应output中每个colY值的MF-alpha参数修正值。 bucket2 = np.empty(ANFISObj.Y.shape[1]) for colY in range(ANFISObj.Y.shape[1]): # 选出rules中包含MF的那些rule的编号。 rulesWithAlpha = np.array( np.where(ANFISObj.rules[:, columnX] == MF))[0] # 删除本列后的其他列的编号 adjCols = np.delete(columns, columnX) # MF关于alpha的偏导数 # 这相当于第一层的delta senSit = mfDerivs.partial_dMF( ANFISObj.X[rowX, columnX], ANFISObj.memFuncs[columnX][MF], alpha) # produces d_ruleOutput/d_parameterWithinMF # 每个rule in rulesWithAlpha为形如(3, 4, 1)的元组(该例为有三个输入变量,该rule由三个输入变量中的第3, 4, 1个 # MF的并组成。所以alpha在该rule对应的偏导dW_dAlpha为senSit(alpha所在的MF的偏导)*其他两个输入变量对应的MF值的积。 # dW_dAlpha其实就是前两层的对应rulesWithAopha的delta # 一个错误的计算,正确的在下一行 # dW_dAplha = senSit * np.array([np.prod([ANFISObj.memClass.evaluateMF(tmpRow)[c][ANFISObj.rules[r][c]] for c in adjCols]) for r in rulesWithAlpha]) if ANFISObj.and_func == 'T-S': dW_dAlpha = senSit * np.array([ np.prod([ ANFISObj.memClass.evaluateMF( ANFISObj.X[rowX])[c][ANFISObj.rules[r][c]] for c in adjCols ]) for r in rulesWithAlpha ]) else: dW_dAlpha = senSit * np.array( [1. for r in rulesWithAlpha]) bucket1 = np.empty(len(ANFISObj.rules[:, 0])) for consequent in range(len(ANFISObj.rules[:, 0])): # fConsequent为第4层的delta # 因为第五层与consequent对应的rule的值L5为:rule*x1*w1 + rule*x2*w2 + ...+ rule*xn*wn, n为x的维数 # = rule*(x1*w1 + x2*w2 + ...+ xn*wn) # 故第四层delta=(x1*w1 + x2*w2 + ...+ xn*wn) # 上式中rule为第三层与consequent对应的输出 l4_rule_gap = ANFISObj.X.shape[1] + 1 l4_rule_id = l4_rule_gap * consequent fConsequent = np.dot( np.append(ANFISObj.X[rowX, :], 1.), ANFISObj.consequents[l4_rule_id:l4_rule_id + l4_rule_gap, colY]) # acum为第三层及之前各层的delta # 第三层归一化后的rule(L3) = rule(L2)/ Total(rule_l2) # 后Total(rule_l2)记为Total # 故在第三层求alpha的偏导为: # ................................................... # 记rule共n条,第二层偏导数结果为d_rule(对应程序中的dW_dAlpha) # 1、若当前rulei与alpha相关: # d_rule(L3) = (d_rulei * Total - d_Total * rulei) / (Total * Total) # 其中d_rulei不为0,且d_rulei = dW_dAlpha # 2、若当前rulei与alpha无关: # 则上面的d_rulei = 0 acum = 0 if consequent in rulesWithAlpha: acum = dW_dAlpha[np.where( rulesWithAlpha == consequent)] * theWSum[rowX] acum = acum - theW[rowX, consequent] * np.sum(dW_dAlpha) acum /= theWSum[rowX]**2 # 该consequent的delta为前四层的delta的积 bucket1[consequent] = fConsequent * acum # 加总所有consequent的结果 sum1 = np.sum(bucket1) # sum1*最后一层的误差的delta bucket2[colY] = sum1 * (ANFISObj.Y[rowX, colY] - theLayerFive[rowX, colY]) * (-2) # 加总所有的colY的结果 sum2 = np.sum(bucket2) # MF-alpha中第rowX个input的参数修正delta bucket3[rowX] = sum2 # 加总所有的inputcase的结果 sum3 = np.sum(bucket3) # MF的第timesThru个参数的修正delta parameters[timesThru] = sum3 # 计算MF的下一个参数 timesThru = timesThru + 1 # MF的参数修正delta paramGrp[MF] = parameters return paramGrp