pdg inf and 2 added
@@ -21,12 +21,21 @@ def clip_eta(eta, ord, eps):
"""
# Clipping perturbation eta to self.ord norm ball
- if ord not in [np.inf]:
- raise ValueError('ord must be np.inf.')
+ if ord not in [np.inf, 2]:
+ raise ValueError('ord must be np.inf or 2.')
axis = list(range(1, len(eta.shape)))
avoid_zero_div = 1e-12
if ord == np.inf:
eta = np.clip(eta, a_min=-eps, a_max=eps)
else:
- raise ValueError('ord must be np.inf.')
+ if ord == 1:
+ raise NotImplementedError("")
+ # This is not the correct way to project on the L1 norm ball:
+ # norm = tf.maximum(avoid_zero_div, reduce_sum(tf.abs(eta), reduc_ind, keepdims=True))
+ elif ord == 2:
+ # avoid_zero_div must go inside sqrt to avoid a divide by zero in the gradient through this operation
+ norm = np.sqrt(np.maximum(avoid_zero_div, np.sum(np.square(eta), axis=axis, keepdims=True)))
+ # We must *clip* to within the norm ball, not *normalize* onto the surface of the ball
+ factor = np.minimum(1., np.divide(eps, norm))
+ eta = eta * factor
return eta
\ No newline at end of file
