Skip to content
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

ImportError: cannot import name 'zero_gradients' from 'advertorch.attacks.utils' #115

Open
abdalimran opened this issue Apr 28, 2024 · 3 comments

Comments

@abdalimran
Copy link

Environment:

  1. Python 3.10.13
  2. advertorch-0.2.4
  3. torch 2.2.1

Installed advertorch using pip install --upgrade git+https://github.com/BorealisAI/advertorch.git
While doing from advertorch.attacks import LinfPGDAttack, I got the following error:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
Cell In[2], [line 1](vscode-notebook-cell:?execution_count=2&line=1)
----> [1](vscode-notebook-cell:?execution_count=2&line=1) from advertorch.attacks import LinfPGDAttack

File [/opt/miniconda3/envs/deeplearning/lib/python3.10/site-packages/advertorch/__init__.py:14](https://file+.vscode-resource.vscode-cdn.net/opt/miniconda3/envs/deeplearning/lib/python3.10/site-packages/advertorch/__init__.py:14)
     [11](https://file+.vscode-resource.vscode-cdn.net/opt/miniconda3/envs/deeplearning/lib/python3.10/site-packages/advertorch/__init__.py:11) with open(os.path.join(os.path.dirname(__file__), 'VERSION')) as f:
     [12](https://file+.vscode-resource.vscode-cdn.net/opt/miniconda3/envs/deeplearning/lib/python3.10/site-packages/advertorch/__init__.py:12)     __version__ = f.read().strip()
---> [14](https://file+.vscode-resource.vscode-cdn.net/opt/miniconda3/envs/deeplearning/lib/python3.10/site-packages/advertorch/__init__.py:14) from . import attacks  # noqa: F401
     [15](https://file+.vscode-resource.vscode-cdn.net/opt/miniconda3/envs/deeplearning/lib/python3.10/site-packages/advertorch/__init__.py:15) from . import defenses

File [/opt/miniconda3/envs/deeplearning/lib/python3.10/site-packages/advertorch/attacks/__init__.py:48](https://file+.vscode-resource.vscode-cdn.net/opt/miniconda3/envs/deeplearning/lib/python3.10/site-packages/advertorch/attacks/__init__.py:48)
     [46](https://file+.vscode-resource.vscode-cdn.net/opt/miniconda3/envs/deeplearning/lib/python3.10/site-packages/advertorch/attacks/__init__.py:46) from .spsa import LinfSPSAAttack
     [47](https://file+.vscode-resource.vscode-cdn.net/opt/miniconda3/envs/deeplearning/lib/python3.10/site-packages/advertorch/attacks/__init__.py:47) from .fast_adaptive_boundary import FABAttack
---> [48](https://file+.vscode-resource.vscode-cdn.net/opt/miniconda3/envs/deeplearning/lib/python3.10/site-packages/advertorch/attacks/__init__.py:48) from .fast_adaptive_boundary import LinfFABAttack
     [49](https://file+.vscode-resource.vscode-cdn.net/opt/miniconda3/envs/deeplearning/lib/python3.10/site-packages/advertorch/attacks/__init__.py:49) from .fast_adaptive_boundary import L2FABAttack
     [50](https://file+.vscode-resource.vscode-cdn.net/opt/miniconda3/envs/deeplearning/lib/python3.10/site-packages/advertorch/attacks/__init__.py:50) from .fast_adaptive_boundary import L1FABAttack

File [/opt/miniconda3/envs/deeplearning/lib/python3.10/site-packages/advertorch/attacks/fast_adaptive_boundary.py:22](https://file+.vscode-resource.vscode-cdn.net/opt/miniconda3/envs/deeplearning/lib/python3.10/site-packages/advertorch/attacks/fast_adaptive_boundary.py:22)
     [19](https://file+.vscode-resource.vscode-cdn.net/opt/miniconda3/envs/deeplearning/lib/python3.10/site-packages/advertorch/attacks/fast_adaptive_boundary.py:19) except ImportError:
     [20](https://file+.vscode-resource.vscode-cdn.net/opt/miniconda3/envs/deeplearning/lib/python3.10/site-packages/advertorch/attacks/fast_adaptive_boundary.py:20)     from advertorch.utils import torch_flip as flip
---> [22](https://file+.vscode-resource.vscode-cdn.net/opt/miniconda3/envs/deeplearning/lib/python3.10/site-packages/advertorch/attacks/fast_adaptive_boundary.py:22) from advertorch.utils import replicate_input
     [24](https://file+.vscode-resource.vscode-cdn.net/opt/miniconda3/envs/deeplearning/lib/python3.10/site-packages/advertorch/attacks/fast_adaptive_boundary.py:24) from .base import Attack
     [25](https://file+.vscode-resource.vscode-cdn.net/opt/miniconda3/envs/deeplearning/lib/python3.10/site-packages/advertorch/attacks/fast_adaptive_boundary.py:25) from .base import LabelMixin

ImportError: cannot import name 'zero_gradients' from 'advertorch.attacks.utils' (/opt/miniconda3/envs/deeplearning/lib/python3.10/site-packages/advertorch/attacks/utils.py)
@ocean1229-max
Copy link

Solution:
Change ~/anaconda3/envs/d2l/lib/python3.7/site-packages/advertorch/attacks/fast_adaptive_boundary.py(or /opt/miniconda3/envs/deeplearning/lib/python3.10/site-packages/advertorch/attacks/utils.py, )
Delete "from torch.autograd.gradcheck import zero_gradients" and add this:
def zero_gradients(x):
if isinstance(x, torch.Tensor):
if x.grad is not None:
x.grad.detach_()
x.grad.zero_()
elif isinstance(x, collections.abc.Iterable):
for elem in x:
zero_gradients(elem)
this may help you

@jonggyujang0123
Copy link

Solution: Change ~/anaconda3/envs/d2l/lib/python3.7/site-packages/advertorch/attacks/fast_adaptive_boundary.py(or /opt/miniconda3/envs/deeplearning/lib/python3.10/site-packages/advertorch/attacks/utils.py, ) Delete "from torch.autograd.gradcheck import zero_gradients" and add this: def zero_gradients(x): if isinstance(x, torch.Tensor): if x.grad is not None: x.grad.detach_() x.grad.zero_() elif isinstance(x, collections.abc.Iterable): for elem in x: zero_gradients(elem) this may help you

Thank you for this solution.

For more help, I converted this into a block code version.

  1. vi ~/anaconda3/envs/[YOUR-ENV]/lib/python3.9/site-packages/advertorch/attacks/fast_adaptive_boundary.py
  2. Remove Line 14 from torch.autograd.gradcheck import zero_gradients
  3. Add the following function
def zero_gradients(x):
    if isinstance(x, torch.Tensor):
        if x.grad is not None:
            x.grad.detach_()
            x.grad.data.zero_()
    elif isinstance(x, container_abcs.Iterable):
        for elem in x:
            zero_gradients(elem)

@Oceanshy12-YANG
Copy link

Solution: Change ~/anaconda3/envs/d2l/lib/python3.7/site-packages/advertorch/attacks/fast_adaptive_boundary.py(or /opt/miniconda3/envs/deeplearning/lib/python3.10/site-packages/advertorch/attacks/utils.py, ) Delete "from torch.autograd.gradcheck import zero_gradients" and add this: def zero_gradients(x): if isinstance(x, torch.Tensor): if x.grad is not None: x.grad.detach_() x.grad.zero_() elif isinstance(x, collections.abc.Iterable): for elem in x: zero_gradients(elem) this may help you

Thank you for this solution.

For more help, I converted this into a block code version.

  1. vi ~/anaconda3/envs/[YOUR-ENV]/lib/python3.9/site-packages/advertorch/attacks/fast_adaptive_boundary.py
  2. Remove Line 14 from torch.autograd.gradcheck import zero_gradients
  3. Add the following function
def zero_gradients(x):
    if isinstance(x, torch.Tensor):
        if x.grad is not None:
            x.grad.detach_()
            x.grad.data.zero_()
    elif isinstance(x, container_abcs.Iterable):
        for elem in x:
            zero_gradients(elem)

I'm glad to be able to help you. I also encountered this problem yesterday, and then I found this solution, and then solved it through this solution. You can read the original text of the Website: https://blog.csdn.net/TaoLinna/article/details/137058509

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants