class_docstr: Test case for package-level functions for correctness.
class_docstr_tok: ["Test","case","for","package-level","functions","for","correctness","."]
code: def _joint_inner_loop(self, fnet, diffopt=None, num_steps=1):
      ref_fast_weights = self.reference_net.get_fast_weights()
      target_fast_weights = None
      if diffopt is None:
          # If diffopt not provided we manually will update these
          target_fast_weights = list(fnet.parameters())
  
      # Things we want to track
      ref_train_losses = []
      ref_train_grads = []
      target_train_losses = []
      target_train_grads = []
  
      for _ in range(num_steps):
          inputs = torch.rand(
              self.batch_size,
              self.num_in_channels,
              self.in_h,
              self.in_w,
          )
          labels = torch.rand(self.batch_size, self.num_classes)
  
          # Do inner loop step for reference net
          ref_preds = self.reference_net(
              inputs, params=ref_fast_weights
          )
          ref_loss = F.mse_loss(ref_preds, labels)
          ref_grads = torch.autograd.grad(
              ref_loss, ref_fast_weights.values(), create_graph=True
          )
  
          ref_train_losses.append(ref_loss)
          ref_train_grads.append(ref_grads)
  
          ref_fast_weights = OrderedDict(
              (name, param - self.lr * grad)
              for ((name, param), grad) in zip(
                  ref_fast_weights.items(), ref_grads
              )
          )
  
          # Do inner loop step for target net
          if diffopt is None:
              target_preds = fnet(inputs, params=target_fast_weights)
          else:
              target_preds = fnet(inputs)
          target_loss = F.mse_loss(target_preds, labels)
  
          if diffopt is None:
              target_grads = torch.autograd.grad(
                  target_loss, target_fast_weights, create_graph=True
              )
              target_fast_weights = [
                  w - (self.lr * g)
                  for w, g in zip(target_fast_weights, target_grads)
              ]
          else:
              target_grads = torch.autograd.grad(
                  target_loss,
                  list(fnet.parameters()),
                  create_graph=True,
              )
              diffopt.step(target_loss)
  
          target_train_losses.append(target_loss)
          target_train_grads.append(target_grads)
  
      # metaval
      inputs = torch.rand(
          self.batch_size, self.num_in_channels, self.in_h, self.in_w
      )
      labels = torch.rand(self.batch_size, self.num_classes)
      ref_preds = self.reference_net(inputs, params=ref_fast_weights)
      ref_meta_loss = F.mse_loss(ref_preds, labels)
  
      if diffopt is None:
          target_preds = fnet(inputs, params=target_fast_weights)
      else:
          target_preds = fnet(inputs)
      target_meta_loss = F.mse_loss(target_preds, labels)
  
      ref_fast_weights = ref_fast_weights.values()
      target_fast_weights = fnet.parameters()
      if diffopt is None:
          target_fast_weights = fnet.parameters()
  
      packed = (
          (
              ref_meta_loss,
              ref_fast_weights,
              ref_train_losses,
              ref_train_grads,
          ),
          (
              target_meta_loss,
              target_fast_weights,
              target_train_losses,
              target_train_grads,
          ),
      )
      return packed
  
imports: import unittest
  import copy
  from collections import OrderedDict
  import numpy
  import torch
  from torch import nn, optim
  from torch.nn import functional
  import higher
label_desc: Use image node_id 8 for calling the TestCorrectness obj's underlying member method code with example usage: obj._joint_inner_loop(fnet, diffopt, num_steps) and returns: packed