Ejemplo n.º 1
0
def KEDM(param, A):
    N = param.N
    d = param.d
    K = ktools.K_base(param)
    ###########################################################################
    MaxIter = 500
    verbosity = True
    ###########################################################################
    output = KEDM_OUT()

    tau_list = ktools.generalsampler(param, 'basis')
    trn_list = ktools.generalsampler(param, 'trn_sample')
    tst_list = ktools.generalsampler(param, 'tst_sample')
    log_list = ktools.generalsampler(param, 'log_sample')

    D, W, ei = ktools.generateData(param, trn_list, A)

    G = []
    con = []
    for k in range(K):
        G.append(cvx.Variable((N, N), PSD=True))
        con.append(cvx.sum(G[k], axis=0) == 0)
    for t in log_list:
        weights = ktools.W(t, tau_list, param)
        weights = weights / np.linalg.norm(weights)
        G_tot = ktools.G_t(G, weights, True)
        con.append(G_tot >> 0)
    cost = 0
    for i_t, t in enumerate(trn_list):
        weights = ktools.W(t, tau_list, param)
        G_tot = ktools.G_t(G, weights, True)
        con.append(G_tot >> 0)
        D_G = ktools.gram2edm(G_tot, N, True)
        W_vec = np.diag(W[i_t].flatten())
        alpha = (np.linalg.norm(np.matmul(W_vec, D[i_t].flatten())))**(-2)
        cost += alpha * cvx.norm(cvx.matmul(W_vec, cvx.vec(D[i_t] - D_G)))**2

    obj = cvx.Minimize(cost)
    prob = cvx.Problem(obj, con)

    try:
        prob.solve(solver=cvx.CVXOPT, verbose=False, normalize=True)
    except Exception as message:
        print(message)

    output.status = str(prob.status)
    if str(prob.status) == 'optimal':
        G = ktools.rankProj(G, param)
        eo = ktools.testError(param, tau_list, tst_list, G, A)
        output.G = G
        output.tau_list = tau_list
        output.trn_list = trn_list
        output.tst_list = tst_list
        output.ei = ei
        output.eo = eo
        output.A = A
    return output
Ejemplo n.º 2
0
 def __init__(self):
     self.N = 6
     self.d = 2
     self.P = 3
     self.omega = 2 * np.pi
     self.mode = 1
     self.T_trn = np.array([-1, 1])
     self.T_tst = np.array([-1, 1])
     self.N_trn = ktools.K_base(self)
     self.K = ktools.K_base(self)
     self.N_tst = 500
     self.Nr = 5
     self.n_del = 0
     self.sampling = 1
     self.delta = 0.99
     self.std = 1
     self.maxIter = 5
     self.n_del_init = 0
     self.bipartite = False
     self.N0 = 3
     self.Pr = 0.9
     self.path = '../../../results/kedm/python3/'
Ejemplo n.º 3
0
def KEDM(param, A):
    N = param.N
    d = param.d
    K = ktools.K_base(param)
    ###########################################################################
    MaxIter = 500
    verbosity = True
    ###########################################################################
    output = KEDM_OUT()
    
    tau_list = ktools.generalsampler(param, 'basis')
    trn_list = ktools.generalsampler(param, 'trn_sample')
    tst_list = ktools.generalsampler(param, 'tst_sample')
    log_list = ktools.generalsampler(param, 'log_sample')
    
    D, W, ei = ktools.generateData(param, trn_list, A)
    
    G = []
    con = []
    for k in range(K):
        G.append(cvx.Variable((N, N), PSD=True))
        con.append(cvx.sum(G[k],axis = 0) == 0)
    for t in log_list:
        weights = ktools.W(t, tau_list, param)
        weights = weights/np.linalg.norm(weights)
        G_tot = ktools.G_t(G, weights, True)
        con.append(G_tot>>0)
    cost = 0
    for i_t, t in enumerate(trn_list):
        weights = ktools.W(t, tau_list, param)
        G_tot = ktools.G_t(G, weights, True)
        con.append(G_tot >> 0)
        D_G = ktools.gram2edm(G_tot, N, True)
        W_vec = np.diag(W[i_t].flatten())
        alpha = (np.linalg.norm( np.matmul(W_vec, D[i_t].flatten()) ) )**(-2)
        cost += alpha*cvx.norm( cvx.matmul(W_vec, cvx.vec(D[i_t]-D_G) ) )**2
    
    
    obj = cvx.Minimize(cost)
    prob = cvx.Problem(obj,con)