-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implementation of PRBCD
and GRBCD
attacks
#9
Open
EdisonLeeeee
wants to merge
21
commits into
master
Choose a base branch
from
rbcd
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
9bdaf7a
RBCD
EdisonLeeeee a49bf6d
import
EdisonLeeeee 862c4f0
update
EdisonLeeeee a3dbc4d
Merge branch 'master' into rbcd
EdisonLeeeee 21f32b1
update
EdisonLeeeee dec61bc
update
EdisonLeeeee 7f4293b
update
EdisonLeeeee 9fda871
update
EdisonLeeeee 066adf2
update
EdisonLeeeee b38e70e
update
EdisonLeeeee a3eb36f
update
EdisonLeeeee 718e520
Merge branch 'master' into rbcd
EdisonLeeeee d89e293
Update
EdisonLeeeee 733fd9a
Update
EdisonLeeeee a675146
Merge branch 'master' into rbcd
EdisonLeeeee 521ce3e
Update
EdisonLeeeee 044155f
Merge branch 'master' into rbcd
EdisonLeeeee 76c3fb0
Update
EdisonLeeeee e9828af
Merge branch 'master' into rbcd
EdisonLeeeee 4668f58
Merge branch 'master' into rbcd
EdisonLeeeee a486b21
Update
EdisonLeeeee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import os.path as osp | ||
|
||
import torch | ||
import torch_geometric.transforms as T | ||
|
||
from greatx.attack.targeted import GRBCDAttack, PRBCDAttack | ||
from greatx.datasets import GraphDataset | ||
from greatx.nn.models import GCN | ||
from greatx.training import Trainer | ||
from greatx.training.callbacks import ModelCheckpoint | ||
from greatx.utils import mark, split_nodes | ||
|
||
dataset = 'Cora' | ||
root = osp.join(osp.dirname(osp.realpath(__file__)), '../../..', 'data') | ||
dataset = GraphDataset(root=root, name=dataset, | ||
transform=T.LargestConnectedComponents()) | ||
|
||
data = dataset[0] | ||
splits = split_nodes(data.y, random_state=15) | ||
|
||
num_features = data.x.size(-1) | ||
num_classes = data.y.max().item() + 1 | ||
|
||
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') | ||
|
||
# ================================================================== # | ||
# Attack Setting # | ||
# ================================================================== # | ||
target = 1 # target node to attack | ||
target_label = data.y[target].item() | ||
|
||
# ================================================================== # | ||
# Before Attack # | ||
# ================================================================== # | ||
trainer_before = Trainer(GCN(num_features, num_classes), device=device) | ||
ckp = ModelCheckpoint('model_before.pth', monitor='val_acc') | ||
trainer_before.fit(data, mask=(splits.train_nodes, splits.val_nodes), | ||
callbacks=[ckp]) | ||
output = trainer_before.predict(data, mask=target) | ||
print("Before attack:") | ||
print(mark(output, target_label)) | ||
|
||
# ================================================================== # | ||
# Attacking (PRBCDAttack) # | ||
# ================================================================== # | ||
attacker = PRBCDAttack(data, device=device) | ||
attacker.setup_surrogate(trainer_before.model) | ||
attacker.reset() | ||
attacker.attack(target) | ||
|
||
# ================================================================== # | ||
# After evasion Attack # | ||
# ================================================================== # | ||
output = trainer_before.predict(attacker.data(), mask=target) | ||
print("After evasion attack:") | ||
print(mark(output, target_label)) | ||
|
||
# ================================================================== # | ||
# After poisoning Attack # | ||
# ================================================================== # | ||
trainer_after = Trainer(GCN(num_features, num_classes), device=device) | ||
ckp = ModelCheckpoint('model_after.pth', monitor='val_acc') | ||
trainer_after.fit(attacker.data(), mask=(splits.train_nodes, splits.val_nodes), | ||
callbacks=[ckp]) | ||
output = trainer_after.predict(attacker.data(), mask=target) | ||
print("After poisoning attack:") | ||
print(mark(output, target_label)) | ||
|
||
# ================================================================== # | ||
# Attacking (GRBCDAttack) # | ||
# ================================================================== # | ||
attacker = GRBCDAttack(data, device=device) | ||
attacker.setup_surrogate(trainer_before.model) | ||
attacker.reset() | ||
attacker.attack(target) | ||
|
||
# ================================================================== # | ||
# After evasion Attack # | ||
# ================================================================== # | ||
output = trainer_before.predict(attacker.data(), mask=target) | ||
print("After evasion attack:") | ||
print(mark(output, target_label)) | ||
|
||
# ================================================================== # | ||
# After poisoning Attack # | ||
# ================================================================== # | ||
trainer_after = Trainer(GCN(num_features, num_classes), device=device) | ||
ckp = ModelCheckpoint('model_after.pth', monitor='val_acc') | ||
trainer_after.fit(attacker.data(), mask=(splits.train_nodes, splits.val_nodes), | ||
callbacks=[ckp]) | ||
output = trainer_after.predict(attacker.data(), mask=target) | ||
print("After poisoning attack:") | ||
print(mark(output, target_label)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import os.path as osp | ||
|
||
import torch | ||
import torch_geometric.transforms as T | ||
|
||
from greatx.attack.untargeted import GRBCDAttack, PRBCDAttack | ||
from greatx.datasets import GraphDataset | ||
from greatx.nn.models import GCN | ||
from greatx.training import Trainer | ||
from greatx.training.callbacks import ModelCheckpoint | ||
from greatx.utils import split_nodes | ||
|
||
dataset = 'Cora' | ||
root = osp.join(osp.dirname(osp.realpath(__file__)), '../../..', 'data') | ||
dataset = GraphDataset(root=root, name=dataset, | ||
transform=T.LargestConnectedComponents()) | ||
|
||
data = dataset[0] | ||
splits = split_nodes(data.y, random_state=15) | ||
|
||
num_features = data.x.size(-1) | ||
num_classes = data.y.max().item() + 1 | ||
|
||
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') | ||
|
||
# ================================================================== # | ||
# Before Attack # | ||
# ================================================================== # | ||
trainer_before = Trainer(GCN(num_features, num_classes), device=device) | ||
ckp = ModelCheckpoint('model_before.pth', monitor='val_acc') | ||
trainer_before.fit(data, mask=(splits.train_nodes, splits.val_nodes), | ||
callbacks=[ckp]) | ||
logs = trainer_before.evaluate(data, splits.test_nodes) | ||
print(f"Before attack\n {logs}") | ||
|
||
# ================================================================== # | ||
# Attacking (PRBCDAttack) # | ||
# ================================================================== # | ||
attacker = PRBCDAttack(data, device=device) | ||
attacker.setup_surrogate( | ||
trainer_before.model, | ||
victim_nodes=splits.test_nodes, | ||
# set True to use ground-truth labels | ||
ground_truth=False, | ||
) | ||
attacker.reset() | ||
attacker.attack(0.05) | ||
|
||
# ================================================================== # | ||
# After evasion Attack # | ||
# ================================================================== # | ||
logs = trainer_before.evaluate(attacker.data(), splits.test_nodes) | ||
print(f"After evasion attack\n {logs}") | ||
# ================================================================== # | ||
# After poisoning Attack # | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would make clear that these poisoning attacks are heuristic in the sense that you are transferring the evasion attack. |
||
# ================================================================== # | ||
trainer_after = Trainer(GCN(num_features, num_classes), device=device) | ||
ckp = ModelCheckpoint('model_after.pth', monitor='val_acc') | ||
trainer_after.fit(attacker.data(), mask=(splits.train_nodes, splits.val_nodes), | ||
callbacks=[ckp]) | ||
logs = trainer_after.evaluate(attacker.data(), splits.test_nodes) | ||
print(f"After poisoning attack\n {logs}") | ||
|
||
# ================================================================== # | ||
# Attacking (GRBCDAttack) # | ||
# ================================================================== # | ||
attacker = GRBCDAttack(data, device=device) | ||
attacker.setup_surrogate( | ||
trainer_before.model, | ||
victim_nodes=splits.test_nodes, | ||
# set True to use ground-truth labels | ||
ground_truth=False, | ||
) | ||
attacker.reset() | ||
attacker.attack(0.05) | ||
|
||
# ================================================================== # | ||
# After evasion Attack # | ||
# ================================================================== # | ||
logs = trainer_before.evaluate(attacker.data(), splits.test_nodes) | ||
print(f"After evasion attack\n {logs}") | ||
# ================================================================== # | ||
# After poisoning Attack # | ||
# ================================================================== # | ||
trainer_after = Trainer(GCN(num_features, num_classes), device=device) | ||
ckp = ModelCheckpoint('model_after.pth', monitor='val_acc') | ||
trainer_after.fit(attacker.data(), mask=(splits.train_nodes, splits.val_nodes), | ||
callbacks=[ckp]) | ||
logs = trainer_after.evaluate(attacker.data(), splits.test_nodes) | ||
print(f"After poisoning attack\n {logs}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find it confusing to call it
surrogate
. Rather call itvictim
,target
, or similar. I think it should not be the default to use surrogates, but rather the exception. See our paper "Are Defenses for GNNs robust?"