class_docstr: Test case for the optim module.
code: def testGetApplyRoundTrip(self):
      kwargs = {}
      lr = 0.1
      left_lr = 0.2
      model = self._model
      full_parameters = list(model.parameters())
      half = len(full_parameters) // 2
      left_parameters = full_parameters[:half]
      right_parameters = full_parameters[half:]
      param_groups = [
          {"params": left_parameters, "lr": left_lr},
          {"params": right_parameters},
      ]
      opt = optim.Adam(param_groups, lr=lr, **kwargs)
  
      override = higher.optim.get_trainable_opt_params(opt)
  
      def assert_closure(target: torch.Tensor):
          self.assertTrue(
              torch.is_tensor(target) and target.requires_grad
          )
  
      # Check that all items in override are structures containing
      # differentiable tensors requiring gradient
      for hp in override:
          higher.utils._recursive_map(override[hp], assert_closure)
  
      param_groups = [
          {"params": left_parameters, "lr": left_lr + 1},
          {"params": right_parameters},
      ]
  
      # Create new opt with slightly different parameter group hyperparameter
      # values, to simulate divergence between original hyperparameters
      new_opt = optim.Adam(param_groups, lr=lr + 5, **kwargs)
  
      # Overwrite with the "learned" hyperparameters
      higher.optim.apply_trainable_opt_params(new_opt, override)
  
      # Check that values match
      # TODO(egrefen): would be good to do a structure matching test, or an
      # extrinsic eval whereby we check that both opts are functionally
      # equivalent.
      old_flattened = higher.utils.flatten(opt.param_groups)
      new_flattened = higher.utils.flatten(new_opt.param_groups)
      self.assertEqual(len(old_flattened), len(new_flattened))
      zipped = zip(old_flattened, new_flattened)
      for old, new in zipped:
          self.assertEqual(type(old), type(new))
          if torch.is_tensor(old):
              torch.testing.assert_allclose(old, new)
          elif isinstance(old, float) or isinstance(old, int):
              self.assertAlmostEqual(old, new)
          else:
              self.assertEqual(old, new)
  
imports: import unittest
  import copy
  from parameterized import parameterized
  import torch
  from torch import nn, optim
  import higher
label_desc: Use image node_id 11 for calling the TestOptim obj's underlying member method code with example usage: obj.testGetApplyRoundTrip() without return types