Exemple #1
0
    def __init__(self,
                 adj,
                 x,
                 labels,
                 idx_train=None,
                 idx_val=None,
                 seed=None,
                 name=None,
                 device='CPU:0',
                 surrogate=None,
                 surrogate_args={},
                 **kwargs):
        super().__init__(adj,
                         x=x,
                         labels=labels,
                         seed=seed,
                         name=name,
                         device=device,
                         **kwargs)

        if surrogate is None:
            surrogate = train_a_surrogate(self, 'GCN', idx_train, idx_val,
                                          **kwargs)

        elif not isinstance(surrogate, SemiSupervisedModel):
            raise RuntimeError(
                "surrogate model should be instance of `graphgallery.nn.SemiSupervisedModel`."
            )

        with tf.device(self.device):
            self.surrogate = surrogate
            self.loss_fn = sparse_categorical_crossentropy
            self.tf_x = astensor(self.x)
    def __init__(self,
                 adj,
                 x,
                 labels,
                 idx_train=None,
                 seed=None,
                 name=None,
                 device='CPU:0',
                 surrogate=None,
                 surrogate_args={},
                 **kwargs):
        super().__init__(adj,
                         x=x,
                         labels=labels,
                         seed=seed,
                         name=name,
                         device=device,
                         **kwargs)

        if surrogate is None:
            surrogate = train_a_surrogate(self, 'DenseGCN', idx_train,
                                          **surrogate_args)
        elif not isinstance(surrogate, DenseGCN):
            raise RuntimeError(
                "surrogate model should be the instance of `graphgallery.nn.DenseGCN`."
            )

        self.allow_feature_attack = True

        with tf.device(self.device):
            self.surrogate = surrogate
            self.loss_fn = sparse_categorical_crossentropy
Exemple #3
0
    def __init__(self,
                 adj,
                 x,
                 labels,
                 idx_train,
                 surrogate=None,
                 surrogate_args={},
                 seed=None,
                 name=None,
                 device='CPU:0',
                 **kwargs):

        super().__init__(adj=adj,
                         x=x,
                         labels=labels,
                         seed=seed,
                         name=name,
                         device=device,
                         **kwargs)
        adj, x = self.adj, self.x

        if surrogate is None:
            surrogate = train_a_surrogate(self, 'DenseGCN', idx_train,
                                          **surrogate_args)
        elif not isinstance(surrogate, DenseGCN):
            raise RuntimeError(
                "surrogate model should be the instance of `graphgallery.nn.DenseGCN`."
            )

        idx_attack = asintarr(idx_train)

        with tf.device(self.device):
            self.idx_attack = astensor(idx_attack)
            self.labels_attack = astensor(labels[idx_attack])
            self.tf_adj = astensor(self.adj.A)
            self.tf_x = astensor(x)
            self.complementary = tf.ones_like(self.tf_adj) - tf.eye(
                self.n_nodes) - 2. * self.tf_adj
            self.loss_fn = sparse_categorical_crossentropy
            self.adj_changes = tf.Variable(
                tf.zeros(adj.shape, dtype=self.floatx))
            self.surrogate = surrogate

            # used for CW_loss=True
            self.label_matrix = tf.gather(tf.eye(self.n_classes),
                                          self.labels_attack)
Exemple #4
0
    def __init__(self,
                 adj,
                 x,
                 labels,
                 idx_train=None,
                 surrogate=None,
                 surrogate_args={},
                 seed=None,
                 name=None,
                 device='CPU:0',
                 **kwargs):
        super().__init__(adj=adj,
                         x=x,
                         labels=labels,
                         seed=seed,
                         name=name,
                         device=device,
                         **kwargs)

        adj, x = self.adj, self.x

        # nettack can conduct feature attack
        self.allow_feature_attack = True

        if surrogate is None:
            # By default, Nettack uses a linear GCN model.
            if not 'activations' in surrogate_args:
                surrogate_args['activations'] = None
            surrogate = train_a_surrogate(self, 'GCN', idx_train,
                                          **surrogate_args)
        elif not isinstance(surrogate, GCN):
            raise RuntimeError(
                'surrogate model should be the instance of `graphgallery.nn.models.GCN`.'
            )

        self.sparse_x = sp.csr_matrix(x)

        surrogate_weights = surrogate.get_weights()
        assert len(
            surrogate_weights
        ) == 2, 'surrogate weights are the weights of two layer GCN without bias, i.e., W1 and W2.'

        # GCN weight matrices
        W1, W2 = surrogate_weights
        self.W = W1 @ W2
        self.cooc_matrix = self.sparse_x.T @ self.sparse_x
Exemple #5
0
    def __init__(self,
                 adj,
                 x,
                 labels,
                 idx_train=None,
                 hops=2,
                 seed=None,
                 name=None,
                 device='CPU:0',
                 surrogate=None,
                 surrogate_args={},
                 **kwargs):

        super().__init__(adj,
                         x=x,
                         labels=labels,
                         seed=seed,
                         name=name,
                         device=device,
                         **kwargs)

        if surrogate is None:
            surrogate = train_a_surrogate(self, 'SGC', idx_train,
                                          **surrogate_args)
        elif not isinstance(surrogate, SGC):
            raise RuntimeError(
                "surrogate model should be the instance of `graphgallery.nn.SGC`."
            )

        self.hops = hops
        # nodes with the same class labels
        self.similar_nodes = [
            np.where(labels == c)[0] for c in range(self.n_classes)
        ]

        with tf.device(self.device):
            W, b = surrogate.weights
            X = astensor(x)
            self.b = b
            self.XW = X @ W
            self.surrogate = surrogate
            self.SGC = SGConvolution(hops)
            self.loss_fn = sparse_categorical_crossentropy
Exemple #6
0
    def __init__(self,
                 adj,
                 x,
                 labels,
                 idx_train,
                 idx_unlabeled=None,
                 surrogate=None,
                 surrogate_args={},
                 seed=None,
                 name=None,
                 device='CPU:0',
                 **kwargs):

        super().__init__(adj=adj,
                         x=x,
                         labels=labels,
                         seed=seed,
                         name=name,
                         device=device,
                         **kwargs)

        adj, x = self.adj, self.x

        if surrogate is None:
            surrogate = train_a_surrogate(self, 'DenseGCN', idx_train,
                                          **surrogate_args)
        elif not isinstance(surrogate, DenseGCN):
            raise RuntimeError(
                "surrogate model should be the instance of `graphgallery.nn.DenseGCN`."
            )

        # poisoning attack in DeepRobust
        if idx_unlabeled is None:
            idx_attack = idx_train
            labels_attack = labels[idx_train]
        else:  # Evasion attack in original paper
            idx_unlabeled = asintarr(idx_unlabeled)
            self_training_labels = self.estimate_self_training_labels(
                surrogate, idx_unlabeled)
            idx_attack = np.hstack([idx_train, idx_unlabeled])
            labels_attack = np.hstack(
                [labels[idx_train], self_training_labels])

        with tf.device(self.device):
            self.idx_attack = astensor(idx_attack)
            self.labels_attack = astensor(labels_attack)
            self.tf_adj = astensor(self.adj.A)
            self.tf_x = astensor(x)
            self.complementary = tf.ones_like(self.tf_adj) - tf.eye(
                self.n_nodes) - 2. * self.tf_adj
            self.loss_fn = SparseCategoricalCrossentropy()
            self.adj_changes = tf.Variable(
                tf.zeros(adj.shape, dtype=self.floatx))
            self.surrogate = surrogate

            # used for `CW_loss=True`
            self.label_matrix = tf.gather(tf.eye(self.n_classes),
                                          self.labels_attack)
            self.range_idx = tf.range(idx_attack.size, dtype=self.intx)
            self.indices_real = tf.stack([self.range_idx, self.labels_attack],
                                         axis=1)