0

I am coding the MAML meta-learning model. I am not sure if I am doing correctly or not? After training on support set and calculating the loss over query set, I am updating the model parameters by sending the loss to a function defined in the meta learning model class as follows:

    def my_update_params(self, loss, update_lr):

        # backward pass
        grads = torch.autograd.grad(loss, self.graph_model.parameters(), create_graph=True, only_inputs=True,
                                    allow_unused=True)

        # parameter update
        updated_params = OrderedDict()
        for (name, param) , grad in zip(self.graph_model.named_parameters(),grads):
            print(param)
            if param.requires_grad:

                if grad is None:
                    updated_params = param
                    #print("Grad is None...")
                else:

                    updated_param = param - update_lr * grad
            updated_params[name] = updated_param

        
        return grads, updated_params

In order to save the model parameters in updated_params = OrderedDict().

I saw in another publicly available code that authors update the parameters in a different way:

  def update_params1(self, loss, update_lr):
            grads =torch.autograd.grad(loss, self.graph_model.parameters(), create_graph=True, allow_unused=True)
            # parameter update
            return parameters_to_vector(grads), parameters_to_vector(self.graph_model.parameters())-parameters_to_vector(grads)*update_lr

and the loop for training over meta-learning tasks:

new_grad, new_params = self.update_params1(losses_s, update_lr = self.update_lr)
vector_to_parameters(new_params, self.graph_model.parameters())

My question is in the function my_update_params , how to convert the parameters saved in the OrderedDict() to the pytorch model parameters? In this case vector_to_parameters cannot be used.

S.EB
  • 1,966
  • 4
  • 29
  • 54

0 Answers0