bug fix in MIM
Add softmax cross-entropy loss as in the original paper.
@@ -19,7 +19,7 @@ def momentum_iterative_method(
y=None,
targeted=False,
decay_factor=1.0,
- sanity_checks=True,
+ sanity_checks=True
):
"""
Tensorflow 2.0 implementation of Momentum Iterative Method (Dong et al. 2017).
@@ -83,15 +83,14 @@ def momentum_iterative_method(
i = 0
while i < nb_iter:
# Define gradient of loss wrt input
- grad = compute_gradient(model_fn, adv_x, y, targeted)
+ grad = compute_gradient(model_fn, loss_fn, adv_x, y, targeted)
# Normalize current gradient and add it to the accumulated gradient
red_ind = list(range(1, len(grad.shape)))
avoid_zero_div = tf.cast(1e-12, grad.dtype)
grad = grad / tf.math.maximum(
avoid_zero_div,
- tf.math.reduce_mean(tf.math.abs(grad), red_ind, keepdims=True),
- )
+ tf.math.reduce_mean(tf.math.abs(grad), red_ind, keepdims=True))
momentum = decay_factor * momentum + grad
optimal_perturbation = optimize_linear(momentum, eps_iter, norm)
@@ -107,3 +106,9 @@ def momentum_iterative_method(
assert np.all(asserts)
return adv_x
+
+def loss_fn(labels, logits):
+ """
+ Added softmax cross entropy loss for MIM as in the original MI-FGSM paper.
+ """
+ return tf.nn.sparse_softmax_cross_entropy_with_logits(labels, logits, name=None)
\ No newline at end of file
