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 'torch.autograd.gradcheck' #99

Open
ashok-arjun opened this issue Dec 5, 2021 · 12 comments

Comments

@ashok-arjun
Copy link

This issue exists because this line:
from advertorch.attacks.utils import zero_gradients

which refers to the zero_gradients function in PyTorch is obsolete.

This is because zero_gradients is removed from PyTorch 1.9. It was present until PyTorch 1.7.

@masoudhashemi
Copy link
Collaborator

This issue is fixed in the code. It may have not been updated in the pip repo. Please re-install advertorch from the code (pip install -e .). sorry for the inconvenience.

@LostOxygen
Copy link

Ah, that worked. Thanks!

@ashok-arjun
Copy link
Author

ashok-arjun commented Dec 11, 2021

Got it, thanks! I will still keep this issue open as it exists when install using normal pip.

Feel free to close it,

@BetterZhuang
Copy link

This issue appeared again.
from torch.autograd.gradcheck import zero_gradients ImportError: cannot import name 'zero_gradients' from 'torch.autograd.gradcheck' (C:\Users\pc\AppData\Roaming\Python\Python37\site-packages\torch\autograd\gradcheck.py)

@CHENBIN99
Copy link

This issue appeared again at pytorch 1.10
ImportError: cannot import name 'zero_gradients' from 'torch.autograd.gradcheck'

@fam4r
Copy link

fam4r commented Jun 18, 2022

This solved for me in Colab:

!pip install --upgrade git+https://github.com/BorealisAI/advertorch.git

@masoudhashemi @gwding can someone please update the pip package?

@anukriti15
Copy link

The issue still persists in version 1.12

@larksudo
Copy link

still has this problem, can anyone give any help?

@waite116
Copy link

waite116 commented Apr 3, 2023

The error seems to have changed a bit from the start, but still seems to be a persistent issue with zero_gradients:

cannot import name 'zero_gradients' from 'advertorch.attacks.utils' (/usr/local/lib/python3.9/dist-packages/advertorch/attacks/utils.py)

@lzb1616
Copy link

lzb1616 commented Apr 28, 2023

可以通过下面这个方法解决:
from typing import Collection
import torch
import torchvision
from torchvision import datasets, transforms
from torch.autograd import Variable

from torch.autograd.gradcheck import zero_gradients 这句话运行不了,改为下面这段函数

import torch.utils.data.dataloader as Data
import torch.nn as nn
from torchvision import models
import numpy as np
import cv2

def zero_gradients(x):
if isinstance(x, torch.Tensor):
if x.grad is not None:
x.grad.detach_()
x.grad.zero_()
elif isinstance(x, Collection.abc.Iterable):
for elem in x:
zero_gradients(elem)

@lzb1616
Copy link

lzb1616 commented Apr 28, 2023

`from typing import Collection
import torch
import torchvision
from torchvision import datasets, transforms
from torch.autograd import Variable

from torch.autograd.gradcheck import zero_gradients这句话运行不了,改为下面这段函数

import torch.utils.data.dataloader as Data
import torch.nn as nn
from torchvision import models
import numpy as np
import cv2

def zero_gradients(x):
if isinstance(x, torch.Tensor):
if x.grad is not None:
x.grad.detach_()
x.grad.zero_()
elif isinstance(x, Collection.abc.Iterable):
for elem in x:
zero_gradients(elem)`

@linhanbin0453
Copy link

`from typing import Collection import torch import torchvision from torchvision import datasets, transforms from torch.autograd import Variable

from torch.autograd.gradcheck import zero_gradients这句话运行不了,改为下面这段函数

import torch.utils.data.dataloader as Data import torch.nn as nn from torchvision import models import numpy as np import cv2

def zero_gradients(x): if isinstance(x, torch.Tensor): if x.grad is not None: x.grad.detach_() x.grad.zero_() elif isinstance(x, Collection.abc.Iterable): for elem in x: zero_gradients(elem)`

don't know what the cv2 refers

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