text: import torch
  from torch.optim.sgd import SGD
  
  
  class MetaSGD(SGD):
      def __init__(self, net, *args, **kwargs):
          super(MetaSGD, self).__init__(*args, **kwargs)
          self.net = net
  
      def set_parameter(self, current_module, name, parameters):
          if '.' in name:
              name_split = name.split('.')
              module_name = name_split[0]
              rest_name = '.'.join(name_split[1:])
              for children_name, children in current_module.named_children():
                  if module_name == children_name:
                      self.set_parameter(children, rest_name, parameters)
                      break
          else:
              current_module._parameters[name] = parameters
  
      def meta_step(self, grads):
          group = self.param_groups[0]
          # weight_decay = group['weight_decay']
          # momentum = group['momentum']
          # dampening = group['dampening']
          # nesterov = group['nesterov']
  
          lr = group['lr']
          # test = set(dict(self.net.named_parameters()).keys())
          # for (name, parameter), grad in zip(self.net.named_parameters(), grads):
          #     if grad == None:
          #         # grad = torch.zeros_like(parameter, device= parameter.device)
          #         continue
          #     # parameter.detach_()
          #     self.set_parameter(self.net, name, parameter.add(grad, alpha=-lr))
  
          with torch.no_grad():
              for p, g in zip(self.net.parameters(), grads):
                  if g is None:
                      continue
                  new_val = p - lr * g
                  p.copy_(new_val)
  
  
  