Esempio n. 1
0
def extend_func_POIM(Q, P, M, L, r, mu, sigma, sm, maxdegree, small_k):    
    
    R = []
    cR = []
    for k in range(small_k):
        cR.append(np.zeros((math.pow(4, k + 1) * L)))
        R.append(np.zeros((math.pow(4, k + 1), L)))
    
    
    for p in range(len(P)):
        v = []
        D = P[p] - small_k + 1
        for m in range(M[p]):
            vh = []
            for msub in range(D):
                vh.append(compute_w(L, r[p][m][:, msub:msub + small_k], sigma[p][m], mu[p][m] + msub, small_k))
            v.append(vh)
        cR[small_k - 1] += compute_scoring(mu[p], sigma[p], L, v, small_k, sm[p], D) #ein += statt = eingefuegt am 16.10
            
            
    for i in range(small_k):
        R[i] = utils.vec2matrix(cR[i], i + 1)  
    
    
    f = 0
    for p in range(len(P)):#small_k):
        for i in range(len(R[small_k - 1])): 
            for j in range(len(R[small_k - 1][0])):
                f += math.pow(R[small_k - 1][i][j] - Q[small_k - 1][i][j], 2) 
    fval = 1. / 2 * f     
    return fval
Esempio n. 2
0
def extend_gradient(Q, P, M, L, r, mu, sigma, sm, maxdegree, opt_var, small_k):
    R = []
    cR = []
    for k in range(small_k):
        cR.append(np.zeros((math.pow(4, k + 1) * L)))
        R.append(np.zeros((math.pow(4, k + 1), L)))
    
    
    
    
    
    Rgradmu = []  
    Rgradsig = []  
    Rgradpwm = []
    Rgradsm = []
    for p in range(len(P)): 
        D = P[p] - small_k + 1
        K = P[p]
        Rgradm = []
        if opt_var[2] != 0:
            Rgradsigm = []
        Rgradpwmm = []
        Rgradsmm = []
        
        v = []
        vgrad = []
        vgradsig = []
        vgradpwm = []
        for m in range(M[p]):
            vhh = []
            vgradhh = []
            vgradsighh = []
            vgradpwmhh = []
            
            for msub in range(D):
            
                vh, vgradh, vgradsigh, vgradpwmh = compute_w(L, r[p][m][:, msub:msub + small_k], sigma[p][m], mu[p][m] + msub, small_k)
                vhh.append(vh)
                vgradhh.append(vgradh)
                vgradsighh.append(vgradsigh)
                vgradpwmhh.append(vgradpwmh)
                
            v.append(vhh)
            vgrad.append(vgradhh)
            vgradsig.append(vgradsighh)
            vgradpwm.append(vgradpwmhh)
        
            
        R1, R2, R3, R4 = compute_scoring(P, M, L, r[p], mu[p], sigma[p], maxdegree, v, vgrad, vgradsig, vgradpwm, small_k, sm[p], opt_var, D)

        for m in range(M[p]):
            Rgradm.append(utils.vec2matrix(sm[p][m]*R2[m], small_k))
            if opt_var[2] != 0:
                Rgradsigm.append(utils.vec2matrix(sm[p][m]*R3[m], small_k))
            if opt_var[1] != 0:
                Rgradsmm.append(utils.vec2matrix(R1[m], small_k))
            for n in range(4):
                for u in range(P[p]):  
                    Rgradpwmm.append(utils.vec2matrix(sm[p][m]*R4[m][n][u], small_k))
            cR[small_k - 1] += sm[p][m]*R1[m]
            

        Rgradmu.append(Rgradm)
        if opt_var[2] != 0:
            Rgradsig.append(Rgradsigm)
        Rgradpwm.append(Rgradpwmm)
        if opt_var[1] != 0:
            Rgradsm.append(Rgradsmm)
    
    for i in range(small_k):
        R[i] = utils.vec2matrix(cR[i], i + 1)  

    
    
    
    gradmu = []
    gradsig = []
    gradpwm = []
    gradsm = []
    
    for d in range(len(P)):
        gradmu.append([0.]* M[d])
        gradsig.append([0.]* M[d])
        gradpwm.append(np.zeros((M[d], 4, P[d])))
        gradsm.append([0.]* M[d])
       
    ### here is the inner derivative multiplied with the outer derivative    
    for p in range(len(P)):
        for i in range(len(R[small_k - 1])): 
            for j in range(len(R[small_k - 1][0])):
                diff = R[small_k - 1][i][j] - Q[small_k - 1][i][j]
                count = 0
                for m in range(M[p]):
                    gradmu[p][m] += (diff * Rgradmu[p][m][i][j])
                    if opt_var[2] != 0:
                        gradsig[p][m] += (diff * Rgradsig[p][m][i][j])
                    if opt_var[1] != 0:
                        gradsm[p][m] += (diff * Rgradsm[p][m][i][j])
                
                    for n in range(4):
                        for u in range(P[p]): 
                            gradpwm[p][m][n][u] += (diff * Rgradpwm[p][count][i][j])
                            count += 1
    fobj2 = open("pruef_grad1", "wb")
    pickle.dump([gradmu, gradsig, gradpwm, gradsm], fobj2)
    fobj2.close()
    return gradmu, gradsig, gradpwm, gradsm