def union1d(arr1, arr2, toarr=False): arr1 = asintarr(arr1) arr2 = asintarr(arr2) arr = list(arr1.intersection(arr2)) if toarr: arr = np.asarray(arr) return arr
def setdiff1d(arr1, arr2, toarr=False): arr1 = asintarr(arr1) arr2 = asintarr(arr2) arr = list(arr1.difference(arr2)) if toarr: arr = np.asarray(arr) return arr
def __init__(self, adj, x, labels, idx_train, idx_unlabeled, hidden_layers, use_relu, self_training_labels=None, 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, labels = self.adj, self.x, self.labels idx_train = asintarr(idx_train) idx_unlabeled = asintarr(idx_unlabeled) if self_training_labels is None: surrogate = DenseGCN(adj, x, labels, device='GPU', norm_x=None, seed=None) surrogate.build(16, activations='relu' if use_relu else None) his = surrogate.train(idx_train, verbose=0, epochs=200, save_best=False) self_training_labels = surrogate.predict(idx_unlabeled).argmax(1) self.ll_ratio = None # mettack can also conduct feature attack self.allow_feature_attack = True with tf.device(self.device): self.idx_train = astensor(idx_train, dtype=self.intx) self.idx_unlabeled = astensor(idx_unlabeled, dtype=self.intx) self.labels_train = astensor(self.labels[idx_train], dtype=self.floatx) self.self_training_labels = astensor(self_training_labels, dtype=self.floatx) self.tf_adj = astensor(adj.A, dtype=self.floatx) self.tf_x = astensor(x, dtype=self.floatx) self.build(hidden_layers=hidden_layers) self.use_relu = use_relu self.loss_fn = SparseCategoricalCrossentropy(from_logits=True) self.adj_changes = tf.Variable(tf.zeros_like(self.tf_adj)) self.x_changes = tf.Variable(tf.zeros_like(self.tf_x))
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)
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)