(self, targets, N, M)
return error
反向传播
反向传播
反向传播
def backPropagate(self, targets, N, M):
        ''' 反向传播 '''
        if len(targets) != self.no:
            raise ValueError('与输出层节点数不符！')

        # 计算输出层的误差
        output_deltas = [0.0] * self.no
        for k in range(self.no):
            error = targets[k]-self.ao[k]
            output_deltas[k] = dsigmoid(self.ao[k]) * error

        # 计算隐藏层的误差
        hidden_deltas = [0.0] * self.nh
        for j in range(self.nh):
            error = 0.0
            for k in range(self.no):
                error = error + output_deltas[k]*self.wo[j][k]
            hidden_deltas[j] = dsigmoid(self.ah[j]) * error

        # 更新输出层权重
        for j in range(self.nh):
            for k in range(self.no):
                change = output_deltas[k]*self.ah[j]
                self.wo[j][k] = self.wo[j][k] + N*change + M*self.co[j][k]
                self.co[j][k] = change
                #print(N*change, M*self.co[j][k])

        # 更新输入层权重
        for i in range(self.ni):
            for j in range(self.nh):
                change = hidden_deltas[j]*self.ai[i]
                self.wi[i][j] = self.wi[i][j] + N*change + M*self.ci[i][j]
                self.ci[i][j] = change

        # 计算误差
        error = 0.0
        for k in range(len(targets)):
            error = error + 0.5*(targets[k]-self.ao[k])**2
        return error
def backPropagate ( self , targets , N , M ) : if len ( targets ) != self . no : raise ValueError ( '与输出层节点数不符！') # 计算输出层的误差 output_deltas = [ 0.0 ] * self . no for k in range ( self . no ) : error = targets [ k ] - self . ao [ k ] output_deltas [ k ] = dsigmoid ( self . ao [ k ] ) * error # 计算隐藏层的误差 hidden_deltas = [ 0.0 ] * self . nh for j in range ( self . nh ) : error = 0.0 for k in range ( self . no ) : error = error + output_deltas [ k ] * self . wo [ j ] [ k ] hidden_deltas [ j ] = dsigmoid ( self . ah [ j ] ) * error # 更新输出层权重 for j in range ( self . nh ) : for k in range ( self . no ) : change = output_deltas [ k ] * self . ah [ j ] self . wo [ j ] [ k ] = self . wo [ j ] [ k ] + N * change + M * self . co [ j ] [ k ] self . co [ j ] [ k ] = change #print(N*change, M*self.co[j][k]) # 更新输入层权重 for i in range ( self . ni ) : for j in range ( self . nh ) : change = hidden_deltas [ j ] * self . ai [ i ] self . wi [ i ] [ j ] = self . wi [ i ] [ j ] + N * change + M * self . ci [ i ] [ j ] self . ci [ i ] [ j ] = change # 计算误差 error = 0.0 for k in range ( len ( targets ) ) : error = error + 0.5 * ( targets [ k ] - self . ao [ k ] ) ** 2 return error