advertorch
advertorch copied to clipboard
ImportError: cannot import name 'zero_gradients' from 'torch.autograd.gradcheck'
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.
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.
Ah, that worked. Thanks!
Got it, thanks! I will still keep this issue open as it exists when install using normal pip
.
Feel free to close it,
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)
This issue appeared again at pytorch 1.10
ImportError: cannot import name 'zero_gradients' from 'torch.autograd.gradcheck'
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?
The issue still persists in version 1.12
still has this problem, can anyone give any help?
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)
可以通过下面这个方法解决: 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)
`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)`
`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