albanD:

make sure that you do not save any Tensor in a list or something similar during tr


Well, the model has been trained,  and when I use the model to generate adversarial samples , the problem appear
the error is :
Traceback (most recent call last):
File “gen_adv.py”, line 28, in 
perturbed_data = Attack.gen_adv(model, device, data, target, eps)
File “/content/drive/My Drive/Colab Notebooks/animal10/Attack.py”, line 42, in gen_adv
loss.backward()
File “/usr/local/lib/python3.6/dist-packages/torch/tensor.py”, line 195, in backward
torch.autograd.backward(self, gradient, retain_graph, create_graph)
File “/usr/local/lib/python3.6/dist-packages/torch/autograd/init.py”, line 99, in backward
allow_unreachable=True)  # allow_unreachable flag
RuntimeError: CUDA out of memory. Tried to allocate 392.00 MiB (GPU 0; 7.43 GiB total capacity; 5.78 GiB already allocated; 392.94 MiB free; 6.53 GiB reserved in total by PyTorch)
It seems that "  loss.backward() " is the key, and I don’t save any Tensor in a list or something similar…
The model can withstand the input of dozens of pictures, but when the number of pictures exceeds thousands, the program fails…
Thanks for answer my question:)