class_docstr: Identical to the TestOptimizeLinear in tests_tf/test_attacks.
class_docstr_tok: ["Identical","to","the","TestOptimizeLinear","in","tests_tf\/test_attacks","."]
code: def test_optimize_linear_l2_satisfies_norm_constraint(self):
      for eps in self.eps_list:
          eta = utils.optimize_linear(self.rand_grad, eps=eps, norm=2)
          # optimize_linear uses avoid_zero_div as the divisor for
          # gradients with overly small l2 norms when performing norm
          # normalizations on the gradients so as to safeguard against
          # zero division error. Therefore, the replaced gradient vectors
          # will not be l2-unit vectors after normalization. In this test,
          # these gradients are filtered out by the one_mask
          # below and are not tested.
          # NOTE the value of avoid_zero_div should be the same as the
          # avoid_zero_div used in the optimize_linear function
          avoid_zero_div = torch.tensor(1e-12)
          square = torch.max(
              avoid_zero_div,
              torch.sum(
                  self.rand_grad**2, self.red_ind, keepdim=True
              ),
          )
          norm = eta.pow(2).sum(dim=self.red_ind, keepdim=True).sqrt()
          one_mask = (square <= avoid_zero_div).to(
              torch.float
          ) * norm + (square > avoid_zero_div).to(torch.float)
          self.assertTrue(torch.allclose(norm, eps * one_mask))
  
file_path: cleverhans/cleverhans/torch/tests/test_utils.py
imports: import numpy
  import torch
  import cleverhans.torch.utils
  from cleverhans.devtools.checks import CleverHansTest
label_desc: Use image node_id 8 for calling the TestOptimizeLinear obj's underlying member method code with example usage: obj.test_optimize_linear_l2_satisfies_norm_constraint() without return types