Beispiel #1
0
    def process(self,
                surrogate,
                train_nodes,
                unlabeled_nodes=None,
                reset=True):
        assert isinstance(surrogate, gg.gallery.nodeclas.DenseGCN), surrogate

        # poisoning attack in DeepRobust
        if unlabeled_nodes is None:
            victim_nodes = gf.asarray(train_nodes)
            victim_labels = self.graph.node_label[victim_nodes]
        else:  # Evasion attack in original paper
            self_training_labels = self.estimate_self_training_labels(surrogate, unlabeled_nodes)
            victim_nodes = np.hstack([train_nodes, unlabeled_nodes])
            victim_labels = np.hstack([self.graph.node_label[train_nodes], self_training_labels])

        with tf.device(self.device):
            adj_tensor = gf.astensor(self.graph.adj_matrix.A)
            self.victim_nodes = gf.astensor(victim_nodes)
            self.victim_labels = gf.astensor(victim_labels)
            self.adj_tensor = adj_tensor
            self.x_tensor = gf.astensor(self.graph.node_attr)
            self.complementary = tf.ones_like(adj_tensor) - tf.eye(self.num_nodes) - 2. * adj_tensor
            self.loss_fn = sparse_categorical_crossentropy
            self.adj_changes = tf.Variable(tf.zeros_like(adj_tensor))
            self.surrogate = surrogate.model

            # used for `CW_loss=True`
            self.label_matrix = tf.gather(tf.eye(self.num_classes), self.victim_labels)
            self.range_idx = tf.range(victim_nodes.size, dtype=self.intx)
            self.indices_real = tf.stack([self.range_idx, self.victim_labels],
                                         axis=1)
        if reset:
            self.reset()
        return self
Beispiel #2
0
    def predict(self, predict_data=None, return_logits=True):

        if not self.model:
            raise RuntimeError(
                'You must compile your model before training/testing/predicting. Use `trainer.build()`.'
            )

        cache = self.cache
        cfg = self.cfg.predict
        cfg.return_logits = return_logits

        if predict_data is None:
            predict_data = np.arange(self.graph.num_nodes)

        if not isinstance(predict_data, Sequence):
            predict_data = gf.asarray(predict_data)
            predict_data = self.predict_sequence(predict_data)

        cache.predict_data = predict_data

        logits = self.predict_step(predict_data)

        if not return_logits:
            logits = softmax(logits)

        return logits.squeeze()
Beispiel #3
0
    def predict(self, predict_data=None, transform=None):

        if not self.model:
            raise RuntimeError(
                'You must compile your model before training/testing/predicting. Use `trainer.build()`.'
            )

        cache = self.cache
        cfg = self.cfg.predict
        cfg.transform = transform

        if predict_data is None:
            predict_data = np.arange(self.graph.num_nodes)

        if not isinstance(predict_data, Sequence):
            predict_data = gf.asarray(predict_data)
            predict_data = self.predict_loader(predict_data)
        if cfg.cache_predict_data:
            cache.predict_data = predict_data

        logits = self.predict_step(predict_data)

        T = gf.get(transform)
        self.transform.logit_transform = T
        logits = T(logits)
        return logits.squeeze()
Beispiel #4
0
    def process(self,
                surrogate,
                train_nodes,
                unlabeled_nodes=None,
                reset=True):
        assert isinstance(surrogate, gg.gallery.GCN), surrogate

        # poisoning attack in DeepRobust
        if unlabeled_nodes is None:
            victim_nodes = gf.asarray(train_nodes)
            victim_labels = self.graph.node_label[victim_nodes]
        else:  # Evasion attack in original paper
            self_training_labels = self.estimate_self_training_labels(
                surrogate, unlabeled_nodes)
            victim_nodes = np.hstack([train_nodes, unlabeled_nodes])
            victim_labels = np.hstack(
                [self.graph.node_label[train_nodes], self_training_labels])

        adj_tensor = gf.astensor(self.graph.adj_matrix.A, device=self.device)
        self.victim_nodes = gf.astensor(victim_nodes, device=self.device)
        self.victim_labels = gf.astensor(victim_labels, device=self.device)
        self.adj_tensor = adj_tensor
        self.x_tensor = gf.astensor(self.graph.node_attr, device=self.device)
        self.complementary = (torch.ones_like(adj_tensor) -
                              torch.eye(self.num_nodes).to(self.device) -
                              2. * adj_tensor)
        self.loss_fn = nn.CrossEntropyLoss()
        self.adj_changes = nn.Parameter(torch.zeros_like(self.adj_tensor))
        self.surrogate = surrogate.model.to(self.device)
        self.surrogate.eval()

        # # used for `CW_loss=True`
        self.label_matrix = torch.eye(self.num_classes)[self.victim_labels].to(
            self.device)
        self.range_idx = torch.arange(victim_nodes.size).to(self.device)
        self.indices_real = torch.stack([self.range_idx, self.victim_labels])
        if reset:
            self.reset()
        return self
Beispiel #5
0
 def test(self, index):
     index = asarray(index)
     y_true = self.graph.node_label[index]
     y_pred = self.classifier.predict(self.embeddings[index])
     accuracy = accuracy_score(y_true, y_pred)
     return BunchDict(loss=None, accuracy=accuracy)
Beispiel #6
0
 def predict(self, index):
     index = asarray(index)
     logit = self.classifier.predict_proba(self.embeddings[index])
     return logit
Beispiel #7
0
 def train(self, index):
     index = asarray(index)
     self.classifier.fit(self.embeddings[index],
                         self.graph.node_label[index])
 def train_sequence(self, index, **kwargs):
     index = gf.asarray(index)
     return self.embeddings[index], self.graph.node_label[index]