Example #1
0
def compute_duality_gap_lasso(X, y, w, lbd):
    """Compute duality gap for Lasso.
        """
    # part of the code is designed for sparse matrix.
    if type(lbd) is not np.array:
        lbd = np.ones(X.shape[1]) * lbd
    rho = y - X.dot(w)

    if not isspmatrix(X):
        normrho2 = np.linalg.norm(rho, axis=0, ord=2)**2
        primalcost = 0.5 * normrho2 + np.sum(np.abs(w) * lbd)
    else:
        normrho2 = spnorm(rho, axis=0)**2
        primalcost = 0.5 * normrho2 + (abs(w * lbd).sum())

    correl = X.T.dot(rho)

    # nu is a feasible approximation of nu
    max_viol = np.max(np.abs(correl) / lbd)

    if max_viol > 1:
        nu = rho / max_viol
    else:
        nu = rho
    if not isspmatrix(X):
        dualcost = -0.5 * np.sum(nu * nu) + np.sum(nu * y)
    else:
        dualcost = -0.5 * spnorm(nu)**2 + (nu.multiply(y)).sum()
    gap = primalcost - dualcost
    return gap, rho, correl, nu
Example #2
0
def compute_duality_gap_prox_lasso(X, y, w, lbd, w_0, alpha):
    """Compute duality gap for prox_Lasso."""
    if type(lbd) is not np.array:
        lbd = np.ones(X.shape[1]) * lbd

    rho = y - X.dot(w)

    if not isspmatrix(X):
        normrho2 = np.linalg.norm(rho, axis=0, ord=2)**2
        primalcost = 0.5 * normrho2 + np.sum(np.abs(w) * lbd) + \
            0.5 / alpha * np.sum((w - w_0)**2)
    else:
        normrho2 = spnorm(rho, axis=0)**2
        primalcost = 0.5 * normrho2 + (abs(w * lbd).sum())
    v = (w - w_0) / alpha
    correl = X.T.dot(rho)

    max_viol = np.max(np.abs(correl - v) / lbd)

    if max_viol > 1:
        nu = rho / max_viol
        v = v / max_viol
    else:
        nu = rho
    if not isspmatrix(X):
        dualcost = - 0.5 * np.sum(nu * nu) + np.sum(nu * y) - \
            v.T.dot(w_0) - alpha / 2 * np.sum(v**2)
    else:
        dualcost = -0.5 * spnorm(nu)**2 + (nu.multiply(y)).sum() - \
            v.T.dot(w_0) - alpha / 2 * np.sum(v**2)
    gap = primalcost - dualcost
    return gap, rho, correl, nu, v
Example #3
0
 def test_sparse_vector_norms(self):
     for sparse_type in self._sparse_types:
         for M in self._test_matrices:
             S = sparse_type(M)
             for axis in (0, 1, -1, -2, (0,), (1,), (-1,), (-2,)):
                 assert_allclose(spnorm(S, axis=axis), npnorm(M, axis=axis))
                 for ord in None, 2, np.inf, -np.inf, 1, 0.5, 0.42:
                     assert_allclose(spnorm(S, ord, axis=axis), npnorm(M, ord, axis=axis))
Example #4
0
 def test_vector_norm(self):
     v = [4.5825756949558398, 4.2426406871192848, 4.5825756949558398]
     for m, a in (self.b, 0), (self.b.T, 1):
         for axis in a, (a, ), a - 2, (a - 2, ):
             assert_allclose(spnorm(m, 1, axis=axis), [7, 6, 7])
             assert_allclose(spnorm(m, np.inf, axis=axis), [4, 3, 4])
             assert_allclose(spnorm(m, axis=axis), v)
             assert_allclose(spnorm(m, ord=2, axis=axis), v)
             assert_allclose(spnorm(m, ord=None, axis=axis), v)
Example #5
0
 def test_sparse_vector_norms(self):
     for sparse_type in self._sparse_types:
         for M in self._test_matrices:
             S = sparse_type(M)
             for axis in (0, 1, -1, -2, (0, ), (1, ), (-1, ), (-2, )):
                 assert_allclose(spnorm(S, axis=axis), npnorm(M, axis=axis))
                 for ord in None, 2, np.inf, -np.inf, 1, 0.5, 0.42:
                     assert_allclose(spnorm(S, ord, axis=axis),
                                     npnorm(M, ord, axis=axis))
Example #6
0
 def test_vector_norm(self):
     v = [4.5825756949558398, 4.2426406871192848, 4.5825756949558398]
     for m, a in (self.b, 0), (self.b.T, 1):
         for axis in a, (a, ), a-2, (a-2, ):
             assert_allclose(spnorm(m, 1, axis=axis), [7, 6, 7])
             assert_allclose(spnorm(m, np.inf, axis=axis), [4, 3, 4])
             assert_allclose(spnorm(m, axis=axis), v)
             assert_allclose(spnorm(m, ord=2, axis=axis), v)
             assert_allclose(spnorm(m, ord=None, axis=axis), v)
Example #7
0
def test_sparray_norm():
    row = np.array([0, 0, 1, 1])
    col = np.array([0, 1, 2, 3])
    data = np.array([4, 5, 7, 9])
    test_arr = scipy.sparse.coo_array((data, (row, col)), shape=(2, 4))
    test_mat = scipy.sparse.coo_matrix((data, (row, col)), shape=(2, 4))
    assert_equal(spnorm(test_arr, ord=1, axis=0), np.array([4, 5, 7, 9]))
    assert_equal(spnorm(test_mat, ord=1, axis=0), np.array([4, 5, 7, 9]))
    assert_equal(spnorm(test_arr, ord=1, axis=1), np.array([9, 16]))
    assert_equal(spnorm(test_mat, ord=1, axis=1), np.array([9, 16]))
Example #8
0
 def test_sparse_matrix_norms_with_axis(self):
     for sparse_type in self._sparse_types:
         for M in self._test_matrices:
             S = sparse_type(M)
             for axis in None, (0, 1), (1, 0):
                 assert_allclose(spnorm(S, axis=axis), npnorm(M, axis=axis))
                 for ord in "fro", np.inf, -np.inf, 1, -1:
                     assert_allclose(spnorm(S, ord, axis=axis), npnorm(M, ord, axis=axis))
             # Some numpy matrix norms are allergic to negative axes.
             for axis in (-2, -1), (-1, -2), (1, -2):
                 assert_allclose(spnorm(S, axis=axis), npnorm(M, axis=axis))
                 assert_allclose(spnorm(S, "f", axis=axis), npnorm(M, "f", axis=axis))
                 assert_allclose(spnorm(S, "fro", axis=axis), npnorm(M, "fro", axis=axis))
Example #9
0
def is_hermitian(operator):
    """
    Check if matrix or operator is hermitian.

    :param (numpy.ndarray|qutip.Qobj) operator: The operator or matrix to be tested.
    :return: True if the operator is hermitian.
    :rtype: bool
    """
    if isinstance(operator, qt.Qobj):
        return (operator.dag() -
                operator).norm(FROBENIUS) / operator.norm(FROBENIUS) < EPS
    if isinstance(operator, np.ndarray):
        return np.linalg.norm(operator.T.conj() -
                              operator) / np.linalg.norm(operator) < EPS
    return spnorm(operator.H - operator) / spnorm(operator) < EPS
Example #10
0
 def test_sparse_matrix_norms_with_axis(self):
     for sparse_type in self._sparse_types:
         for M in self._test_matrices:
             S = sparse_type(M)
             for axis in None, (0, 1), (1, 0):
                 assert_allclose(spnorm(S, axis=axis), npnorm(M, axis=axis))
                 for ord in 'fro', np.inf, -np.inf, 1, -1:
                     assert_allclose(spnorm(S, ord, axis=axis),
                                     npnorm(M, ord, axis=axis))
             # Some numpy matrix norms are allergic to negative axes.
             for axis in (-2, -1), (-1, -2), (1, -2):
                 assert_allclose(spnorm(S, axis=axis), npnorm(M, axis=axis))
                 assert_allclose(spnorm(S, 'f', axis=axis),
                                 npnorm(M, 'f', axis=axis))
                 assert_allclose(spnorm(S, 'fro', axis=axis),
                                 npnorm(M, 'fro', axis=axis))
Example #11
0
def rescale_matrix(matrix,
                   scaling,
                   axis,
                   binary=True,
                   return_scaling_values=False):
    '''Function to scale either rows or columns of the sparse rating matrix'''
    result = None
    scaling_values = None
    if scaling == 1:  # no scaling (standard SVD case)
        result = matrix

    if binary:
        norm = np.sqrt(matrix.getnnz(
            axis=axis))  # compute Euclidean norm as if values are binary
    else:
        norm = spnorm(matrix, axis=axis, ord=2)  # compute Euclidean norm

    scaling_values = power(norm, scaling - 1, where=norm != 0)
    scaling_matrix = diags(scaling_values)

    if axis == 0:  # scale columns
        result = matrix.dot(scaling_matrix)
    if axis == 1:  # scale rows
        result = scaling_matrix.dot(matrix)

    if return_scaling_values:
        result = (result, scaling_values)
    return result
Example #12
0
 def test_matrix_norm_axis(self):
     for m, axis in ((self.b, None), (self.b, (0, 1)), (self.b.T, (1, 0))):
         assert_allclose(spnorm(m, axis=axis), 7.745966692414834)        
         assert_allclose(spnorm(m, 'fro', axis=axis), 7.745966692414834)
         assert_allclose(spnorm(m, np.inf, axis=axis), 9)
         assert_allclose(spnorm(m, -np.inf, axis=axis), 2)
         assert_allclose(spnorm(m, 1, axis=axis), 7)
         assert_allclose(spnorm(m, -1, axis=axis), 6)
Example #13
0
 def test_matrix_norm_axis(self):
     for m, axis in ((self.b, None), (self.b, (0, 1)), (self.b.T, (1, 0))):
         assert_allclose(spnorm(m, axis=axis), 7.745966692414834)
         assert_allclose(spnorm(m, 'fro', axis=axis), 7.745966692414834)
         assert_allclose(spnorm(m, np.inf, axis=axis), 9)
         assert_allclose(spnorm(m, -np.inf, axis=axis), 2)
         assert_allclose(spnorm(m, 1, axis=axis), 7)
         assert_allclose(spnorm(m, -1, axis=axis), 6)
Example #14
0
 def test_sparse_matrix_norms(self):
     for sparse_type in self._sparse_types:
         for M in self._test_matrices:
             S = sparse_type(M)
             assert_allclose(spnorm(S), npnorm(M))
             assert_allclose(spnorm(S, 'fro'), npnorm(M, 'fro'))
             assert_allclose(spnorm(S, np.inf), npnorm(M, np.inf))
             assert_allclose(spnorm(S, -np.inf), npnorm(M, -np.inf))
             assert_allclose(spnorm(S, 1), npnorm(M, 1))
             assert_allclose(spnorm(S, -1), npnorm(M, -1))
Example #15
0
 def test_sparse_matrix_norms(self):
     for sparse_type in self._sparse_types:
         for M in self._test_matrices:
             S = sparse_type(M)
             assert_allclose(spnorm(S), npnorm(M))
             assert_allclose(spnorm(S, 'fro'), npnorm(M, 'fro'))
             assert_allclose(spnorm(S, np.inf), npnorm(M, np.inf))
             assert_allclose(spnorm(S, -np.inf), npnorm(M, -np.inf))
             assert_allclose(spnorm(S, 1), npnorm(M, 1))
             assert_allclose(spnorm(S, -1), npnorm(M, -1))
Example #16
0
def baseline_add():
    adj_new = randomly_delete_edges(adj_orig, k=FLAGS.k)
    testacc_clean, valid_acc_clean = GCN.run(FLAGS.dataset,
                                             adj_orig,
                                             name="clean")
    testacc, valid_acc = GCN.run(FLAGS.dataset, adj_new, name="original")
    cos = features_csr.dot(features_csr.transpose())
    norm = spnorm(features_csr, axis=1)
    norm = norm[:, np.newaxis]
    norm_mat = norm.dot(norm.T)
    cos = cos / norm_mat
    normalize_cos = 0.5 + 0.5 * cos
    normalize_cos = np.array(normalize_cos)
    zero_mat = np.zeros([num_nodes, num_nodes])
    adj_new_dense = adj_new.todense()
    flag_adj = np.triu(np.ones([num_nodes, num_nodes]), k=1) - np.triu(
        adj_new_dense, k=1)
    zero_mat[flag_adj > 0] = normalize_cos[flag_adj > 0]
    one_mat = zero_mat.flatten()
    one_mat[np.isnan(one_mat)] = 0
    add_idx = np.argsort(one_mat)
    add_idx = add_idx[-FLAGS.k:]
    row_idx = add_idx % num_nodes
    col_idx = add_idx // num_nodes
    for idx in range(len(row_idx)):
        adj_new[row_idx, col_idx] = 1
        adj_new[col_idx, row_idx] = 1
    testacc_new, valid_acc_new = GCN.run(FLAGS.dataset, adj_new, name="new1")
    testacc_new2, valid_acc_new = GCN.run(FLAGS.dataset, adj_new, name="new2")
    testacc_new3, valid_acc_new = GCN.run(FLAGS.dataset, adj_new, name="new3")
    print("**#" * 10)
    print("clean one")
    print(testacc_clean)
    print("**#" * 10)
    print("original one")
    print(testacc)
    print("new one")
    print(testacc_new)
    print("new two")
    print(testacc_new2)
    print("new three")
    print(testacc_new3)
    print("**#" * 10)
    return testacc_clean, testacc, testacc_new, testacc_new2, testacc_new3
Example #17
0
    def test_matrix_norm(self):

        # Frobenius norm is the default
        assert_allclose(spnorm(self.b), 7.745966692414834)        
        assert_allclose(spnorm(self.b, 'fro'), 7.745966692414834)

        assert_allclose(spnorm(self.b, np.inf), 9)
        assert_allclose(spnorm(self.b, -np.inf), 2)
        assert_allclose(spnorm(self.b, 1), 7)
        assert_allclose(spnorm(self.b, -1), 6)

        # _multi_svd_norm is not implemented for sparse matrix
        assert_raises(NotImplementedError, spnorm, self.b, 2)
        assert_raises(NotImplementedError, spnorm, self.b, -2)
Example #18
0
    def test_matrix_norm(self):

        # Frobenius norm is the default
        assert_allclose(spnorm(self.b), 7.745966692414834)
        assert_allclose(spnorm(self.b, 'fro'), 7.745966692414834)

        assert_allclose(spnorm(self.b, np.inf), 9)
        assert_allclose(spnorm(self.b, -np.inf), 2)
        assert_allclose(spnorm(self.b, 1), 7)
        assert_allclose(spnorm(self.b, -1), 6)

        # _multi_svd_norm is not implemented for sparse matrix
        assert_raises(NotImplementedError, spnorm, self.b, 2)
        assert_raises(NotImplementedError, spnorm, self.b, -2)
Example #19
0
    def test_norm(self):
        a = np.arange(9) - 4
        b = a.reshape((3, 3))
        b = scipy.sparse.csr_matrix(b)

        #Frobenius norm is the default
        assert_equal(spnorm(b), 7.745966692414834)
        assert_equal(spnorm(b, 'fro'), 7.745966692414834)

        assert_equal(spnorm(b, np.inf), 9)
        assert_equal(spnorm(b, -np.inf), 2)
        assert_equal(spnorm(b, 1), 7)
        assert_equal(spnorm(b, -1), 6)

        #_multi_svd_norm is not implemented for sparse matrix
        assert_raises(NotImplementedError, spnorm, b, 2)
        assert_raises(NotImplementedError, spnorm, b, -2)
Example #20
0
    def test_norm(self):
        a = np.arange(9) - 4
        b = a.reshape((3, 3))
        b = scipy.sparse.csr_matrix(b)

        #Frobenius norm is the default
        assert_equal(spnorm(b), 7.745966692414834)        
        assert_equal(spnorm(b, 'fro'), 7.745966692414834)

        assert_equal(spnorm(b, np.inf), 9)
        assert_equal(spnorm(b, -np.inf), 2)
        assert_equal(spnorm(b, 1), 7)
        assert_equal(spnorm(b, -1), 6)

        #_multi_svd_norm is not implemented for sparse matrix
        assert_raises(NotImplementedError, spnorm, b, 2)
        assert_raises(NotImplementedError, spnorm, b, -2)
Example #21
0
def max_Li_logistic(X, reg):
    # import pdb; pdb.set_trace()
    if issparse(X):
        return 0.25 * spnorm(X.power(2), np.inf) + reg
    else:
        return 0.25 * np.max(np.sum(X**2, axis=1)) + reg
Example #22
0
def compute_sparse_norm(sparse_data):
    sparse_norm = spnorm(sparse_data, axis=1)
    return sparse_norm.astype(np.float32)
Example #23
0
    #tmp1=np.dot(L_21, score2)
    #print tmp1.shape
    score1 = (1 - args.w) * score1 + args.w * np.dot(L_11, np.dot(L_21, score2))
    score2 = (1 - args.w) * score2 + args.w * np.dot(L_22, np.dot(L_12, score1))
    #print "score1 shape"
    #print np.shape(score1)
    #print "prevs1 shape"
    #print np.shape(prevs1)
    #print prevs1-score1
    #difs1=np.sum(np.absolute(prevs1-score1),0)
    #difs1=np.sum(np.absolute(prevs1-score1),0)
    score1=score1/(np.sum(np.sum(score1.todense())))
    score2=score2/(np.sum(np.sum(score2.todense())))
    #difs1=LA.norm((prevs1-score1).todense().getA(),ord=2)
    #difs2=LA.norm((prevs2-score2).todense().getA(),ord=2)
    difs1=spnorm(prevs1-score1,'fro')
    difs2=spnorm(prevs2-score2,'fro')
    print str(i)+"th iter : dif1 is "+str(difs1)+" dif2 is "+str(difs2)
#score1 = abs(v[:, 0])/sum(abs(v[:, 0]))
#score2 = (1 - args.w) * S2 + args.w * np.dot(L_22, np.dot(L_21, S1))

if args.outfile1 != None:
	output_file(args.outfile1, score1.todense())
else:
	sys.stdout.write("Scores for layer1: ")
	for i in range(0, num1):
		sys.stdout.write("%f " %score1[i])
	sys.stdout.write("\n")

if args.outfile2 != None:
	output_file(args.outfile2, score2.todense())
Example #24
0
def weighted_prox_lasso_bcd_screening(X,
                                      y,
                                      lbd,
                                      w_0=np.array([]),
                                      alpha=1e9,
                                      nbitermax=10000,
                                      winit=np.array([]),
                                      screen_init=np.array([]),
                                      screen_frq=3,
                                      dual_gap_tol=1e-6,
                                      do_screen=True):
    """
    solve min_w 0.5 *|| y - Xw ||^2 + 1/2\alpha ||w - w_0||^2 + \sum_i \lbda_i |w_i|

    using coordinatewise descent + screening

    """
    n_features = X.shape[1]
    bool_sparse = isspmatrix(X)
    if not bool_sparse:
        normX = np.linalg.norm(X, axis=0, ord=2)
        if len(winit) == 0:
            w = np.zeros(n_features)
        else:
            w = np.copy(winit)
    else:
        normX = spnorm(X, axis=0)
        winit = csr_matrix(winit)
        if winit.nnz == 0:
            w = csr_matrix(np.zeros(n_features))
        else:
            w = winit.copy()

    if len(screen_init) == 0:
        screened = np.zeros(n_features)
    else:
        screened = np.copy(screen_init)

    nb_screened = np.zeros(nbitermax)
    if len(w_0) == 0:
        w_0 = np.zeros(n_features)

    i = 0
    gap = float("inf")
    if bool_sparse:
        w = csr_matrix(w.reshape(-1, 1))
    rho = y - X.dot(w)
    while i < nbitermax and gap > dual_gap_tol:
        for k in range(n_features):
            if not screened[k]:
                # Updating the variable
                # computing the partial residual through update of the residual
                xk = X[:, k]
                s = rho + (xk * w[k])
                Xts = (xk.T.dot(s) + w_0[k] / alpha)
                wp = prox_l1(Xts, lbd[k]) / (normX[k]**2 + 1 / alpha)
                rho = rho - xk * (wp - w[k])
                w[k] = wp
        gap, rho, correl, nu, v = compute_duality_gap_prox_lasso(
            X, y, w, lbd, w_0, alpha)
        if i % screen_frq == 0 and do_screen:
            # Updating screeening
            for k in range(n_features):
                if not screened[k]:
                    bound = (normX[k] + 1 / alpha) * np.sqrt(2 * gap)
                    if not bool_sparse:
                        rhotxk = np.sum(nu * X[:, k])
                    else:
                        rhotxk = nu.multiply(X[:, k]).sum()
                    screened[k] = ((abs(rhotxk - v[k]) + bound) < lbd[k])
            nb_screened[i] = (sum(screened == 1))
        else:
            nb_screened[i] = nb_screened[i - 1]
        #print(i,gap,nb_screened[i])
        i += 1
    return w, nb_screened[:i], screened, rho, correl
Example #25
0
def weighted_lasso_bcd_screening(X,
                                 y,
                                 lbd,
                                 nbitermax=10000,
                                 winit=np.array([]),
                                 screen_init=np.array([]),
                                 screen_frq=3,
                                 dual_gap_tol=1e-6):
    """
    solve min_w 0.5 *|| y - Xw ||^2 +  \sum_i \lbda_i |w_i|
    using coordinatewise descent + screening
    """

    n_features = X.shape[1]
    bool_sparse = isspmatrix(X)

    if not bool_sparse:
        normX = np.linalg.norm(X, axis=0, ord=2)
        if len(winit) == 0:
            w = np.zeros(n_features)
        else:
            w = np.copy(winit)
    else:
        normX = spnorm(X, axis=0)
        winit = csr_matrix(winit)
        if winit.nnz == 0:
            w = csr_matrix(np.zeros(n_features))
        else:
            w = winit.copy()

    if len(screen_init) == 0:
        screened = np.zeros(n_features)
    else:
        screened = np.copy(screen_init)
    nb_screened = np.zeros(nbitermax)
    i = 0
    gap = float("inf")
    if bool_sparse:
        w = csr_matrix(w.reshape(-1, 1))
    rho = y - X.dot(w)
    for i in range(nbitermax):
        for k in range(n_features):
            if not screened[k]:
                # computing the partial residual through update of the res
                xk = X[:, k]
                s = rho + (xk * w[k])
                Xts = xk.T.dot(s)
                if not bool_sparse:
                    # w[k] = np.sign(Xts)* np.maximum((np.abs(Xts) - lbd[k]),0)
                    wp = prox_l1(Xts, lbd[k]) / (
                        normX[k]**2
                    )  #np.sign(Xts) * np.maximum((np.abs(Xts) - lbd), 0) / (normX[k]**2)
                    rho = rho - xk * (wp - w[k])
                    w[k] = wp

                else:  # sparse
                    Xts = Xts[0, 0]
                    wp = prox_l1(Xts, lbd[k]) / (
                        normX[k]**2
                    )  #np.sign(Xts) * np.maximum((np.abs(Xts) - lbd), 0) / (normX[k]**2)
                    # wp = np.sign(Xts) * np.maximum((np.abs(Xts) - lbd[k]), 0) / (normX[k]**2)
                    rho = rho - xk * (csr_matrix(wp) - w[k])

        gap, rho, correl, nu = compute_duality_gap_lasso(X, y, w, lbd)
        if i % screen_frq == 0:
            # Updating screeening
            for k in range(n_features):
                if not screened[k]:
                    bound = normX[k] * np.sqrt(2 * gap)
                    if not bool_sparse:
                        rhotxk = np.sum(nu * X[:, k])
                    else:
                        rhotxk = nu.multiply(X[:, k]).sum()
                    screened[k] = ((abs(rhotxk) + bound) < lbd[k])
            nb_screened[i] = (sum(screened == 1))
        else:
            nb_screened[i] = nb_screened[i - 1]
            gap = float("inf")
        i += 1
        #print('L', i, gap, 's', nb_screened[i - 1])
        if gap < dual_gap_tol:
            # print('L Sorti', gap)
            break

    return w, nb_screened[:i], screened, rho, correl
Example #26
0
def expmv(t, A, v, tol=1e-7, krylov_dim=30):
    """
        Evaluates exp(t * A) @ v efficiently using Krylov subspace projection
        techniques and matrix-vector product operations.

    This function implements the expv function of the Expokit library
    (https://www.maths.uq.edu.au/expokit). It is in particular suitable for
    large sparse matrices.

    Args:
    t (float): real or complex time-like parameter
    A (array or sparse): an numpy.array or scipy.sparse square matrix
    v (array): a vector with size compatible with A
    tol (real): the required tolerance (default 1e-7)
    krylov_dim (int): dimension of the Krylov subspace
                      (typically a number between 15 and 50, default 30)

    Returns:
    The array w(t) = exp(t * A) @ v.
    """
    assert A.shape[1] == A.shape[0], "A must be square"
    assert A.shape[1] == v.shape[0], "v and A must have compatible shapes"

    n = A.shape[0]
    m = min(krylov_dim, n)

    anorm = spnorm(A, ord=np.inf)

    out_type = np.result_type(type(t), A.dtype, v.dtype)

    # safety factors
    gamma = 0.9
    delta = 1.2

    btol = 1e-7  # tolerance for "happy-breakdown"
    maxiter = 10  # max number of time-step refinements

    rndoff = anorm * np.spacing(1)

    # estimate first time-step and round to two significant digits
    beta = norm(v)
    r = 1 / m
    fact = (((m + 1) / np.exp(1.0))**(m + 1)) * np.sqrt(2.0 * np.pi * (m + 1))
    tau = (1.0 / anorm) * ((fact * tol) / (4.0 * beta * anorm))**r

    outvec = np.zeros(v.shape, dtype=out_type)

    # storage for Krylov subspace vectors
    v_m = np.zeros((m + 1, len(v)), dtype=out_type)
    # for i in range(1, m + 2):
    #     vm.append(np.empty_like(outvec))
    h_m = np.zeros((m + 2, m + 2), dtype=outvec.dtype)

    t_f = np.abs(t)

    # For some reason numpy.sign has a different definition than Julia or MATLAB
    if isinstance(t, complex):
        tsgn = t / np.abs(t)
    else:
        tsgn = np.sign(t)

    t_k = 0. * t_f
    w = np.array(v, dtype=out_type)
    p = np.empty_like(w)

    m_x = m
    while t_k < t_f:
        tau = min(t_f - t_k, tau)
        # Arnoldi procedure
        v_m[0] = w / beta
        m_x = m

        for j in range(m):
            p = A.dot(v_m[j])

            h_m[:j + 1, j] = v_m[:j + 1, :].conj() @ p
            tmp = h_m[:j + 1, j][:, np.newaxis] * v_m[:j + 1]
            p -= np.sum(tmp, axis=0)

            s = norm(p)
            if s < btol:  # happy-breakdown
                tau = t_f - t_k
                err_loc = btol

                f_m = expm(tsgn * tau * h_m[:j + 1, :j + 1])

                tmp = beta * f_m[:j + 1, 0][:, np.newaxis] * v_m[:j + 1, :]
                w = np.sum(tmp, axis=0)

                m_x = j
                break

            h_m[j + 1, j] = s
            v_m[j + 1] = p / s

        h_m[m + 1, m] = 1.

        if m_x == m:
            avnorm = norm(A @ v_m[m])

        # propagate using adaptive step size
        i = 1
        while i < maxiter and m_x == m:
            f_m = expm(tsgn * tau * h_m)

            err1 = abs(beta * f_m[m, 0])
            err2 = abs(beta * f_m[m + 1, 0] * avnorm)

            if err1 > 10 * err2:  # err1 >> err2
                err_loc = err2
                r = 1 / m
            elif err1 > err2:
                err_loc = (err1 * err2) / (err1 - err2)
                r = 1 / m
            else:
                err_loc = err1
                r = 1 / (m - 1)

            # is time step sufficient?
            if err_loc <= delta * tau * (tau * tol / err_loc)**r:
                tmp = beta * f_m[:m + 1, 0][:, np.newaxis] * v_m[:m + 1, :]
                w = np.sum(tmp, axis=0)
                break

            # estimate new time-step
            tau = gamma * tau * (tau * tol / err_loc)**r
            i += 1

        if i == maxiter:
            raise (RuntimeError("Number of iteration exceeded maxiter. "
                                "Requested tolerance might be too high."))

        beta = norm(w)
        t_k += tau
        tau = gamma * tau * (tau * tol / err_loc)**r  # estimate new time-step
        err_loc = max(err_loc, rndoff)

        h_m.fill(0.)

    return w
Example #27
0
def PSNR(clean_adj, modified_adj):
    # mse = spnorm(clean_adj - modified_adj, ord = 1) / (clean_adj.shape[0]*(clean_adj.shape[1] - 1))
    # mse = max(spnorm(clean_adj - modified_adj, ord = 1),1.0) / (clean_adj.shape[0]*(clean_adj.shape[1] - 1))
    mse = max(spnorm(clean_adj - modified_adj, ord = 1),0.1) / (clean_adj.shape[0]*(clean_adj.shape[1] - 1))
    PSNR = 10 * np.log(1/ mse)
    return PSNR
Example #28
0
def PSNR_with_features(clean_adj, clean_fea, modified_adj, modified_fea):
    mse = spnorm(clean_adj - modified_adj, ord = 1) / (clean_adj.shape[0]*(clean_adj.shape[1] - 1))
    mse = mse + sp.linalg.norm(clean_fea - modified_fea, ord = 1) / (clean_fea.shape[0]* modified_fea.shape[1])
    PSNR = 10 * np.log(1/ mse)
    return PSNR
Example #29
0
def train():
    # train GCN first
    adj_norm_sparse_csr = adj_norm_sparse.tocsr()
    # sizes = [FLAGS.gcn_hidden1, FLAGS.gcn_hidden2, n_class]
    # surrogate_model = GCN.GCN(sizes, adj_norm_sparse_csr, features_csr, with_relu=True, name="surrogate", gpu_id=gpu_id)
    # surrogate_model.train(adj_norm_sparse_csr, split_train, split_val, node_labels)
    # ori_acc = surrogate_model.test(split_unlabeled, node_labels, adj_norm_sparse_csr)
    testacc, valid_acc = GCN.run(FLAGS.dataset, adj_orig, name="original")
    cos = sp.dot(features_csr.transpose(), features_csr)
    norm = spnorm(features_csr, axis=1)
    norm_mat = norm.dot(norm.transpose())

    if_drop_edge = True
    ## set the checkpoint path
    checkpoints_dir_base = "./checkpoints"
    current_time = datetime.datetime.now().strftime("%y%m%d%H%M%S")
    checkpoints_dir = os.path.join(checkpoints_dir_base, current_time,
                                   current_time)
    ############
    global_steps = tf.get_variable('global_step',
                                   trainable=False,
                                   initializer=0)
    new_learning_rate = tf.train.exponential_decay(FLAGS.learn_rate_init,
                                                   global_step=global_steps,
                                                   decay_steps=10000,
                                                   decay_rate=0.98)
    new_learn_rate_value = FLAGS.learn_rate_init
    ## set the placeholders

    placeholders = {
        'features': tf.sparse_placeholder(tf.float32, name="ph_features"),
        'adj': tf.sparse_placeholder(tf.float32, name="ph_adj"),
        'adj_orig': tf.sparse_placeholder(tf.float32, name="ph_orig"),
        'dropout': tf.placeholder_with_default(0., shape=(),
                                               name="ph_dropout"),
        # 'node_labels': tf.placeholder(tf.float32, name = "ph_node_labels"),
        # 'node_ids' : tf.placeholder(tf.float32, name = "ph_node_ids")
    }
    # build models
    model = None
    if model_str == "gae_gan":
        model = gaegan(placeholders, num_features, num_nodes, features_nonzero,
                       new_learning_rate)
        model.build_model()
    pos_weight = float(adj.shape[0] * adj.shape[0] - adj.sum()) / adj.sum()
    norm = adj.shape[0] * adj.shape[0] / float(
        (adj.shape[0] * adj.shape[0] - adj.sum()) * 2)
    opt = 0
    # Optimizer
    with tf.name_scope('optimizer'):
        if model_str == 'gae_gan':
            opt = Optimizergaegan(
                preds=tf.reshape(model.x_tilde, [-1]),
                labels=tf.reshape(
                    tf.sparse_tensor_to_dense(placeholders['adj_orig'],
                                              validate_indices=False), [-1]),
                #comm_label=placeholders["comm_label"],
                model=model,
                num_nodes=num_nodes,
                pos_weight=pos_weight,
                norm=norm,
                global_step=global_steps,
                new_learning_rate=new_learning_rate)
    # init the sess
    sess = tf.Session()
    sess.run(tf.global_variables_initializer())
    saver = ""
    var_list = tf.global_variables()
    var_list = [
        var for var in var_list
        if ("encoder" in var.name) or ('generate' in var.name)
    ]
    saver = tf.train.Saver(var_list, max_to_keep=10)
    if if_save_model:
        os.mkdir(os.path.join(checkpoints_dir_base, current_time))
        saver.save(sess, checkpoints_dir)  # save the graph

    if restore_trained_our:
        checkpoints_dir_our = "./checkpoints"
        checkpoints_dir_our = os.path.join(checkpoints_dir_our,
                                           FLAGS.trained_our_path)
        # checkpoints_dir_meta = os.path.join(checkpoints_dir_base, FLAGS.trained_our_path,
        #                                     FLAGS.trained_our_path + ".meta")
        #saver.restore(sess,tf.train.latest_checkpoint(checkpoints_dir_our))
        saver.restore(
            sess,
            os.path.join("./checkpoints", "191215231708", "191215231708-1601"))
        print("model_load_successfully")
    # else:  # if not restore the original then restore the base dis one.
    #     checkpoints_dir_base = os.path.join("./checkpoints/base", FLAGS.trained_base_path)
    #     saver.restore(sess, tf.train.latest_checkpoint(checkpoints_dir_base))

    feed_dict = construct_feed_dict(adj_norm, adj_label, features,
                                    placeholders)
    feed_dict.update({placeholders['dropout']: FLAGS.dropout})
    # pred_dis_res = model.vaeD_tilde.eval(session=sess, feed_dict=feed_dict)

    #### save new_adj without norm#############
    if restore_trained_our:
        modified_adj = get_new_adj(feed_dict, sess, model)
        modified_adj = sp.csr_matrix(modified_adj)
        sp.save_npz("transfer_new/transfer_1216_1/qq_5000_gaegan_new.npz",
                    modified_adj)
        sp.save_npz("transfer_new/transfer_1216_1/qq_5000_gaegan_ori.npz",
                    adj_orig)
        print("save the loaded adj")
    # print("before training generator")
    #####################################################

    #####################################################
    G_loss_min = 1000
    for epoch in range(FLAGS.epochs):
        t = time.time()
        # run Encoder's optimizer
        #sess.run(opt.encoder_min_op, feed_dict=feed_dict)
        # run G optimizer  on trained model
        if restore_trained_our:
            sess.run(opt.G_min_op, feed_dict=feed_dict)
        else:  # it is the new model
            if epoch < FLAGS.epochs:
                sess.run(opt.G_min_op, feed_dict=feed_dict)
            #
        ##
        ##
        if epoch % 50 == 0:
            print("Epoch:", '%04d' % (epoch + 1), "time=",
                  "{:.5f}".format(time.time() - t))
            G_loss, laplacian_para, new_learn_rate_value = sess.run(
                [opt.G_comm_loss, opt.reg, new_learning_rate],
                feed_dict=feed_dict)
            #new_adj = get_new_adj(feed_dict, sess, model)
            new_adj = model.new_adj_output.eval(session=sess,
                                                feed_dict=feed_dict)
            temp_pred = new_adj.reshape(-1)
            #temp_ori = adj_norm_sparse.todense().A.reshape(-1)
            temp_ori = adj_label_sparse.todense().A.reshape(-1)
            mutual_info = normalized_mutual_info_score(temp_pred, temp_ori)
            print(
                "Step: %d,G: loss=%.7f ,Lap_para: %f  ,info_score = %.6f, LR=%.7f"
                % (epoch, G_loss, laplacian_para, mutual_info,
                   new_learn_rate_value))
            ## here is the debug part of the model#################################
            laplacian_mat, reg_trace, reg_log, reward_ratio = sess.run(
                [
                    opt.reg_mat, opt.reg_trace, opt.reg_log,
                    opt.new_percent_softmax
                ],
                feed_dict=feed_dict)
            print("lap_mat is:")
            print(np.diag(laplacian_mat))
            print("reg_trace is:")
            print(reg_trace)
            print("reg_log is:")
            print(reg_log)
            print("reward_percentage")
            print(reward_ratio)
            ##########################################
            #';# check the D_loss_min
            if (G_loss < G_loss_min) and (epoch > 1000) and (if_save_model):
                saver.save(sess,
                           checkpoints_dir,
                           global_step=epoch,
                           write_meta_graph=False)
                print("min G_loss new")
            if G_loss < G_loss_min:
                G_loss_min = G_loss

        if (epoch % 200 == 1) and if_save_model:
            saver.save(sess,
                       checkpoints_dir,
                       global_step=epoch,
                       write_meta_graph=False)
            print("Epoch:", '%04d' % (epoch + 1), "time=",
                  "{:.5f}".format(time.time() - t))
    saver.save(sess,
               checkpoints_dir,
               global_step=FLAGS.epochs,
               write_meta_graph=True)
    print("Optimization Finished!")
    feed_dict.update({placeholders['dropout']: 0})
    new_adj = get_new_adj(feed_dict, sess, model)
    new_adj = new_adj - np.diag(np.diag(new_adj))
    new_adj_sparse = sp.csr_matrix(new_adj)
    print((abs(new_adj_sparse - new_adj_sparse.T) > 1e-10).nnz == 0)
    # new_adj_norm, new_adj_norm_sparse = preprocess_graph(new_adj)
    # new_adj_norm_sparse_csr = new_adj_norm_sparse.tocsr()
    # modified_model = GCN.GCN(sizes, new_adj_norm_sparse_csr, features_csr, with_relu=True, name="surrogate", gpu_id=gpu_id)
    # modified_model.train(new_adj_norm_sparse_csr, split_train, split_val, node_labels)
    # modified_acc = modified_model.test(split_unlabeled, node_labels, new_adj_norm_sparse_csr)
    testacc_new, valid_acc_new = GCN.run(FLAGS.dataset,
                                         new_adj_sparse,
                                         name="modified")
    new_adj = get_new_adj(feed_dict, sess, model)
    new_adj = new_adj - np.diag(np.diag(new_adj))
    new_adj_sparse = sp.csr_matrix(new_adj)
    testacc_new2, valid_acc_new = GCN.run(FLAGS.dataset,
                                          new_adj_sparse,
                                          name="modified")
    new_adj = get_new_adj(feed_dict, sess, model)
    new_adj = new_adj - np.diag(np.diag(new_adj))
    new_adj_sparse = sp.csr_matrix(new_adj)
    testacc_new3, valid_acc_new = GCN.run(FLAGS.dataset,
                                          new_adj_sparse,
                                          name="modified")
    #np.save("./data/hinton/hinton_new_adj_48_0815.npy", new_adj)
    #roc_score, ap_score = get_roc_score(test_edges, test_edges_false,feed_dict, sess, model)
    ##### The final results ####
    print("*" * 30)
    print("the final results:\n")
    print("The original acc is: ")
    print(testacc)
    print("*#" * 15)
    print("The modified acc is : ")
    print(testacc_new)
    print("*#" * 15)
    print("The modified acc is : ")
    print(testacc_new2)
    print("*#" * 15)
    print("The modified acc is : ")
    print(testacc_new3)
    return new_adj, testacc, testacc_new, testacc_new2, testacc_new3