Update Momentum Iterative Attacks
@@ -524,24 +524,30 @@ class MomentumIterativeMethod(Attack):
# Normalize current gradient and add it to the accumulated gradient
red_ind = list(xrange(1, len(grad.get_shape())))
- grad = grad / tf.reduce_mean(tf.abs(grad),
- reduction_indices=red_ind,
- keep_dims=True)
+ avoid_zero_div = 1e-12
+ grad = grad / tf.maximum(avoid_zero_div,
+ tf.reduce_mean(tf.abs(grad),
+ red_ind,
+ keep_dims=True))
momentum = self.decay_factor * momentum + grad
if self.ord == np.inf:
normalized_grad = tf.sign(momentum)
- elif self.ord in [1, 2]:
- reduc_ind = list(xrange(1, len(momentum.get_shape())))
- if self.ord == 1:
- norm = tf.reduce_sum(tf.abs(momentum),
- reduction_indices=reduc_ind,
- keep_dims=True)
- elif self.ord == 2:
- norm = tf.sqrt(tf.reduce_sum(tf.square(momentum),
- reduction_indices=reduc_ind,
+ elif self.ord == 1:
+ norm = tf.maximum(avoid_zero_div,
+ tf.reduce_sum(tf.abs(momentum),
+ red_ind,
keep_dims=True))
normalized_grad = momentum / norm
+ elif self.ord == 2:
+ square = tf.reduce_sum(tf.square(momentum),
+ red_ind,
+ keep_dims=True)
+ norm = tf.sqrt(tf.maximum(avoid_zero_div, square))
+ normalized_grad = momentum / norm
+ else:
+ raise NotImplementedError("Only L-inf, L1 and L2 norms are "
+ "currently implemented.")
# Update and clip adversarial example in current iteration
scaled_grad = self.eps_iter * normalized_grad
