Skip to content

Commit 2909cb8

Browse files
Irina NicolaeIrina Nicolae
authored andcommitted
Correct class gradients in loss sensitivity
1 parent 6cfb5ee commit 2909cb8

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/metrics.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,13 @@ def loss_sensitivity(x, classifier, sess):
170170
:return: The average loss sensitivity of the model
171171
:rtype: `float`
172172
"""
173+
from src.attacks.attack import class_derivative
174+
173175
x_op = tf.placeholder(dtype=tf.float32, shape=list(x.shape))
174176
y_pred = classifier.predict(x)
175177
indices = np.argmax(y_pred, axis=1)
176-
grads = [tf.gradients(classifier.model(x_op)[:, i], x_op) for i in range(10)]
178+
grads = class_derivative(classifier._get_predictions(x_op, log=True), x_op,
179+
classifier.model.get_output_shape_at(0)[1])
177180
res = sess.run(grads, feed_dict={x_op: x})
178181
res = np.asarray([r[0] for r in res])[indices, list(range(x.shape[0]))]
179182
res = la.norm(res.reshape(res.shape[0], -1), ord=2, axis=1)

0 commit comments

Comments
 (0)