-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathattack.py
34 lines (29 loc) · 1.16 KB
/
attack.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import torch
from torch import nn
import torch.nn.functional as F
class AttackPGD(nn.Module):
def __init__(self, model, config):
super(AttackPGD, self).__init__()
self.model = model
self.rand = config['random_start']
self.step_size = config['step_size']
self.epsilon = config['epsilon']
self.num_steps = config['num_steps']
self.attack = config['attack']
def forward(self, inputs, targets):
if not self.attack:
return self.model(inputs), inputs
x = inputs.detach()
if self.rand:
x = x + torch.zeros_like(x).uniform_(-self.epsilon, self.epsilon)
for i in range(self.num_steps):
x.requires_grad_()
with torch.enable_grad():
logits = self.model(x)
loss = F.cross_entropy(logits, targets, size_average=False)
grad = torch.autograd.grad(loss, [x])[0]
# print(grad)
x = x.detach() + self.step_size * torch.sign(grad.detach())
x = torch.min(torch.max(x, inputs - self.epsilon), inputs + self.epsilon)
x = torch.clamp(x, 0, 1)
return self.model(x), x