(self, adv_x, x)
| 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) |