-
Notifications
You must be signed in to change notification settings - Fork 31
/
attack_utils.py
44 lines (33 loc) · 1.11 KB
/
attack_utils.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
35
36
37
38
39
40
41
42
43
44
import numpy as np
import keras.backend as K
from tensorflow.python.platform import flags
FLAGS = flags.FLAGS
def linf_loss(X1, X2):
return np.max(np.abs(X1 - X2), axis=(1, 2, 3))
def gen_adv_loss(logits, y, loss='logloss', mean=False):
"""
Generate the loss function.
"""
if loss == 'training':
# use the model's output instead of the true labels to avoid
# label leaking at training time
y = K.cast(K.equal(logits, K.max(logits, 1, keepdims=True)), "float32")
y = y / K.sum(y, 1, keepdims=True)
out = K.categorical_crossentropy(logits, y, from_logits=True)
elif loss == 'logloss':
out = K.categorical_crossentropy(logits, y, from_logits=True)
else:
raise ValueError("Unknown loss: {}".format(loss))
if mean:
out = K.mean(out)
else:
out = K.sum(out)
return out
def gen_grad(x, logits, y, loss='logloss'):
"""
Generate the gradient of the loss function.
"""
adv_loss = gen_adv_loss(logits, y, loss)
# Define gradient of loss wrt input
grad = K.gradients(adv_loss, [x])[0]
return grad