-
Notifications
You must be signed in to change notification settings - Fork 1
/
fgsm.py
25 lines (22 loc) · 844 Bytes
/
fgsm.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
import paddle
class FGSMAttack(object):
def __init__(self, model, criterion, img, label, eps):
self.model = model
self.criterion = criterion
self.img = img
self.label = label
self.epsilon = eps
def attack(self):
tensor_img = paddle.to_tensor(self.img)
tensor_label = paddle.to_tensor(self.label)
tensor_img.stop_gradient = False
predict = self.model(tensor_img)
loss = self.criterion(predict, tensor_label)
for param in self.model.parameters():
param.clear_grad()
loss.backward(retain_graph=True)
grad = paddle.to_tensor(tensor_img.grad)
grad = paddle.sign(grad)
tensor_img = tensor_img + self.epsilon * grad
tensor_img = paddle.to_tensor(tensor_img.detach().numpy())
return tensor_img