MCPcopy Create free account
hub / github.com/CausalLearning/robust-unlearnable-examples / _clip_

Method _clip_

attacks/pgd_attacker.py:58–71  ·  view source on GitHub ↗
(self, adv_x, x)

Source from the content-addressed store, hash-verified

56 return adv_x.data
57
58 def _clip_(self, adv_x, x):
59 adv_x -= x
60 if self.norm_type == 'l-infty':
61 adv_x.clamp_(-self.radius, self.radius)
62 else:
63 if self.norm_type == 'l2':
64 norm = (adv_x.reshape(adv_x.shape[0],-1)**2).sum(dim=1).sqrt()
65 elif self.norm_type == 'l1':
66 norm = adv_x.reshape(adv_x.shape[0],-1).abs().sum(dim=1)
67 norm = norm.reshape( -1, *( [1] * (len(x.shape)-1) ) )
68 adv_x /= (norm + 1e-10)
69 adv_x *= norm.clamp(max=self.radius)
70 adv_x += x
71 adv_x.clamp_(-0.5, 0.5)

Callers 1

perturbMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected