locuslab/fast_adversarial

Probable gradient accumulation bug in mnist_train.py

akshaygrao77 opened this issue · 1 comments

I observed that the delta.grad accumulates gradients over the inner maximization steps in PGD. Isn't this a bug? Can you please clarify whether this was intensional or a bug?

This is the part of the code I am talking about

            elif args.attack == 'pgd':
                delta = torch.zeros_like(X).uniform_(-args.epsilon, args.epsilon)
                delta.data = torch.max(torch.min(1-X, delta.data), 0-X)
                for _ in range(args.attack_iters):
                    delta.requires_grad = True
                    output = model(X + delta)
                    loss = criterion(output, y)
                    opt.zero_grad()
                    loss.backward()
                    grad = delta.grad.detach()
                    I = output.max(1)[1] == y
                    delta.data[I] = torch.clamp(delta + args.alpha * torch.sign(grad), -args.epsilon, args.epsilon)[I]
                    delta.data[I] = torch.max(torch.min(1-X, delta.data), 0-X)[I]
                delta = delta.detach()