Skip to content

Commit

Permalink
add sign and some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mishabounty committed Oct 29, 2024
1 parent 3ffaff8 commit 28896b9
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
1 change: 0 additions & 1 deletion experiments/attack_defense_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,6 @@ def test_pgd():
"predicted_class": predicted_class,
"predicted_probability": predicted_probability,
"real_class": real_class}

# ____________________________________________________________

# ______________________ Attack on graph _____________________
Expand Down
9 changes: 5 additions & 4 deletions src/attacks/evasion_attacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ def _attack_on_node(self, model_manager, gen_dataset):
optimizer.step()
with torch.no_grad():
x.copy_(torch.max(torch.min(x, orig_x + self.epsilon), orig_x - self.epsilon))
x_clamp = torch.clamp(x, -self.epsilon, self.epsilon)
x.copy_(x_clamp)
x.copy_(torch.clamp(x, -self.epsilon, self.epsilon))
# return the modified lines back to the original tensor x
gen_dataset.data.x[subset] = x.detach()
self.attack_diff = gen_dataset
Expand All @@ -130,6 +129,7 @@ def _attack_on_graph(self, model_manager, gen_dataset):

if self.is_feature_attack: # feature attack
x = x.clone()
orig_x = x.clone()
x.requires_grad = True
optimizer = torch.optim.Adam([x], lr=self.learning_rate, weight_decay=5e-4)

Expand All @@ -139,10 +139,11 @@ def _attack_on_graph(self, model_manager, gen_dataset):
# print(loss)
model.zero_grad()
loss.backward()
x.grad.sign_()
optimizer.step()
with torch.no_grad():
x_clamp = torch.clamp(x, 0, self.epsilon)
x.copy_(x_clamp)
x.copy_(torch.max(torch.min(x, orig_x + self.epsilon), orig_x - self.epsilon))
x.copy_(torch.clamp(x, -self.epsilon, self.epsilon))
gen_dataset.dataset[graph_idx].x.copy_(x.detach())
self.attack_diff = gen_dataset
else: # structure attack
Expand Down

0 comments on commit 28896b9

Please sign in to comment.