class_docstr: Test case for the optim module.
code: def testGradientCorrectness(
      self, _, model_builder, opt_builder, kwargs=None
  ):
      kwargs = {} if kwargs is None else kwargs
      lr = 0.1
      model = model_builder(self)
      eps = 1e-3
  
      tests = 10
      count = 0
      threshold = 0.6  # proportion of tests that should pass
  
      for i in range(tests):
          xs = [torch.rand(10, 4) for _ in range(2)]
  
          def closure():
              cmodel = copy.deepcopy(model)
              opt = opt_builder(cmodel.parameters(), lr=lr, **kwargs)
              for x in xs[:-1]:
                  opt.zero_grad()
                  cmodel(x).pow(2).sum().backward()
                  opt.step()
              loss = cmodel(xs[-1]).pow(2).sum()
              return loss
  
          fd_grads = finite_difference(model, closure, eps)
  
          opt = opt_builder(model.parameters(), lr=lr, **kwargs)
          with higher.innerloop_ctx(model, opt) as (fmodel, diffopt):
              for x in xs[:-1]:
                  loss = fmodel(x).pow(2).sum()
                  diffopt.step(loss)
              loss = fmodel(xs[-1]).pow(2).sum()
              grads = torch.autograd.grad(
                  loss, fmodel.parameters(time=0), allow_unused=True
              )
              close = []
              for g, fg in zip(grads, fd_grads):
                  if g is None:
                      # trusting that the tensor shouldn't have been used...
                      close.append(True)
                  else:
                      self.assertFalse(
                          torch.any(torch.isnan(g)),
                          "NaNs found in gradient.",
                      )
                      close.append(torch.allclose(g, fg, 1e-1, 1e-1))
              if all(close):
                  count += 1
      self.assertTrue(
          count / tests >= threshold,
          msg="Proportion of successful finite gradient checks below {:.0f}% "
          "threshold ({:.0f}%).".format(
              threshold * 100, 100 * count / tests
          ),
      )
  
imports: import unittest
  import copy
  from parameterized import parameterized
  import torch
  from torch import nn, optim
  import higher
label_desc: Use image node_id 5 for calling the TestOptim obj's underlying member method code with example usage: obj.testGradientCorrectness(_, model_builder, opt_builder, kwargs) and returns: loss