make sanity checks work even if eps / eps_iter are tf vars
@@ -559,9 +559,20 @@ class ProjectedGradientDescent(Attack):
# Define adversarial example (and clip if necessary)
adv_x = x + eta
- if self.clip_min is not None and self.clip_max is not None:
+ if self.clip_min is not None or self.clip_max is not None:
+ assert self.clip_min is not None and self.clip_max is not None
adv_x = tf.clip_by_value(adv_x, self.clip_min, self.clip_max)
+ asserts = []
+
+ asserts.append(tf.assert_less_equal(self.eps_iter, self.eps))
+ if self.ord == np.inf and self.clip_min is not None:
+ asserts.append(tf.assert_less_equal(self.eps,
+ self.clip_max - self.clip_min))
+
+ with tf.control_dependencies(asserts):
+ adv_x = tf.identity(adv_x)
+
return adv_x
def parse_params(self,
@@ -597,7 +608,6 @@ class ProjectedGradientDescent(Attack):
"""
# Save attack-specific parameters
- assert eps_iter <= eps, (eps_iter, eps)
self.eps = eps
if rand_init is None:
rand_init = self.default_rand_init
@@ -619,9 +629,6 @@ class ProjectedGradientDescent(Attack):
# Check if order of the norm is acceptable given current implementation
if self.ord not in [np.inf, 1, 2]:
raise ValueError("Norm order must be either np.inf, 1, or 2.")
- if (self.ord == np.inf and self.clip_min is not None and
- self.clip_max is not None):
- assert self.eps <= self.clip_max - self.clip_min
return True
