DSE-MSU / DeepRobust

A pytorch adversarial library for attack and defense methods on images and graphs
MIT License
995 stars 192 forks source link

Bugfix for graph PGD class - test failing #16

Closed henrykenlay closed 4 years ago

henrykenlay commented 4 years ago

Thanks for making this library!

I'm using PyTorch 1.5.0 (cpu version). The test examples/graph/test_pgd.py was failing with the following error:

Traceback (most recent call last):
  File "examples/graph/test_pgd.py", line 95, in <module>
    main()
  File "examples/graph/test_pgd.py", line 83, in main
    model.attack(features, adj, labels, idx_train, perturbations)
  File "/anaconda3/lib/python3.7/site-packages/deeprobust/graph/global_attack/topology_attack.py", line 72, in attack
    self.random_sample(ori_adj, ori_features, labels, idx_train, perturbations)
  File "/anaconda3/lib/python3.7/site-packages/deeprobust/graph/global_attack/topology_attack.py", line 80, in random_sample
    s = self.adj_changes.cpu().numpy()
RuntimeError: Can't call numpy() on Variable that requires grad. Use var.detach().numpy() instead.

I changed line 80 from s = self.adj_changes.cpu().numpy() to s = self.adj_changes.cpu().detach().numpy() and now the test passes