Exemplo n.º 1
0
import graphgallery as gg
from graphgallery import functional as gf
from graphgallery.datasets import NPZDataset

data = NPZDataset('cora',
                  root="~/GraphData/datasets/",
                  verbose=False,
                  transform="standardize")

graph = data.graph
splits = data.split_nodes(random_state=15)

################### Surrogate model ############################
# Nettack takes no activation layer
trainer = gg.gallery.nodeclas.GCN(seed=42).make_data(graph).build(acts=None)
his = trainer.fit(splits.train_nodes, splits.val_nodes, verbose=1, epochs=100)
# surrogate weights
if gg.backend() == "tensorflow":
    w1, w2 = trainer.model.weights
    W = w1 @ w2
else:
    w1, w2 = trainer.model.parameters()
    W = (w2 @ w1).T
W = gf.tensoras(W)

################### Attacker model ############################
target = 0
attacker = gg.attack.targeted.Nettack(graph, seed=123).process(W)
attacker.attack(target,
                direct_attack=True,
                structure_attack=True,
Exemplo n.º 2
0
#!/usr/bin/env python
# coding: utf-8

import torch
import graphgallery
import torch_geometric

print("GraphGallery version: ", graphgallery.__version__)
print("Torch version: ", torch.__version__)
print("Torch_Geometric version: ", torch_geometric.__version__)
'''
Load Datasets
Synthetic node classification dataset from PDN: https://github.com/benedekrozemberczki/PDN
'''
from graphgallery.datasets import NPZDataset
data = NPZDataset('pdn', root="~/GraphData/datasets/", verbose=False)
graph = data.graph
splits = data.split_nodes()

graphgallery.set_backend("pyg")

from graphgallery.gallery.nodeclas import PDN
trainer = PDN(device="gpu", seed=123).make_data(graph).build()
his = trainer.fit(splits.train_nodes, splits.val_nodes, verbose=1, epochs=100)
results = trainer.evaluate(splits.test_nodes)
print(f'Test loss {results.loss:.5}, Test accuracy {results.accuracy:.2%}')