Giter VIP home page Giter VIP logo

Comments (2)

jkang1640 avatar jkang1640 commented on July 18, 2024

After reading the code maml.py, I changed my code this way.

Could anybody tell me if it seems right..? Just one more thing, I have no idea when to use learner.module or learner. For example, which one should I return at the end of the code? Which one to pass on to maml_update?

    from torch import autograd 
    from learn2learn.algorithms import maml_update

    def inner_loop(learner, support_dl, fast_lr, adaptation_steps):
       
        all_loss = []
        # initiliaze gradients to zero for all parameters
        accum_grads = tuple(torch.zeros(param.size(), device=device) for param in learner.module.parameters())

        for step in range(adaptation_steps):
            loss = 0.0

            for inputs in support_dl:
                outputs = learner(**inputs)
                accum_step = len(support_dl.dataset) / support_dl.batch_size # number of steps to accumulate gradients 
                curr_loss = outputs.loss / accum_step
                curr_grads = autograd.grad(curr_loss, learner.module.parameters(), create_graph=False)  # False for FOMAML
                accum_grads = tuple(torch.add(accum_grads[i], curr_grads[i]) for i in range(len(curr_grads)))
                loss += curr_loss.item() # accum loss for logging
                del outputs, curr_loss, curr_grads

            maml_update(learner, lr=fast_lr, grads=accum_grads) # update learner parameters with accumulated gradients
            all_loss.append(loss)

      return learner 

from learn2learn.

seba-1511 avatar seba-1511 commented on July 18, 2024

Hello @jkang1640,

Sequence models are notoriously tricky to train with MAML as it's easy to run into OOM like you did.

Unfortunately there's no easy fix for when you're trying to accumulate gradients in the inner loop. My advice is to either pretrain and freeze some of the parameters of the model as in ANIL (to reduce memory consumption during meta-learning), or maybe go for a first-order algorithm (FOMAM, ANIL).

I'll close this for now.

from learn2learn.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.