Git Product home page Git Product logo

Comments (11)

david-berthelot avatar david-berthelot commented on July 28, 2024

In Objax, contrary to other JAX frameworks, you don't need to pass model variables around. They are stored in modules, and for functions they're only needed when you JIT them.

In your particular example, you don't need to pass model_vars to train_op, you already do it in the Jit call.

def train_op(x, y, lr):
    g, v = gv(x, y)
    for grad, p in zip(g, model_vars.subset(objax.TrainVar)):
      p.value -= lr * grad   
    return v

Check the SGD source code, it's 3 lines of code, basically you can copy it and make your own optimizer:
https://objax.readthedocs.io/en/latest/_modules/objax/optimizer/sgd.html#SGD

from objax.

RXZ2020 avatar RXZ2020 commented on July 28, 2024

Thanks for the fast reply. When I ran this code I received:
ValueError: Direct assignment not allowed, use TrainRef to update a TrainVar.
I got the same error by replacing model_vars with model.vars() which I think are equivalent.

from objax.

RXZ2020 avatar RXZ2020 commented on July 28, 2024

I got it working by replacing
model_vars.subset(objax.TrainVar)
with
ModuleList(TrainRef(x) for x in model.vars().subset(TrainVar))
and importing the associated modules

from objax.module import Module, ModuleList
from objax.variable import TrainRef, TrainVar, VarCollection

However, I have two issues which is,

  1. this code is very slow because I'm not using Objax.Jit on the optimizer routine (took roughly 16 seconds for 1 epoch).
  2. it is not very minimalistic. (is there a way to hide ModuleList for instance?)

Do you have any suggestions?

from objax.

david-berthelot avatar david-berthelot commented on July 28, 2024

Oh right, yes = is disallow on TrainVar to avoid users accidentally writing a trainable variable.

For minimalism, you could use assign instead of =:

def train_op(x, y, lr):
    g, v = gv(x, y)
    for grad, p in zip(g, model_vars.subset(objax.TrainVar)):
      p.assign(p.value - lr * grad)   
    return v

For performance, you simply need to JIT train_op, everything inside gets jitted but it won't allow you to write TrainVar objects. So the solution would be:

gv = objax.GradValues(loss, model.vars())
refs = objax.ModuleList(objax.TrainRef(x) for x in model.vars().subset(objax.TrainVar))

def train_op(x, y, lr):
    g, v = gv(x, y)
    for grad, p in zip(g, refs.vars()):
      p.value -= lr * grad   
    return v


train_op = objax.Jit(train_op, gv.vars() + refs.vars())

from objax.

RXZ2020 avatar RXZ2020 commented on July 28, 2024

Thanks for your reply! I am still having some trouble however. After using,

def train_op(x, y, lr):
    g, v = gv(x, y)
    for grad, p in zip(g, model_vars.subset(objax.TrainVar)):
      p.assign(p.value - lr * grad)   
    return v

exactly as it appears, I received: UnexpectedTracerError: Encountered an unexpected tracer. Perhaps this tracer escaped through global state from a previously traced function. The functions being transformed should not save traced values to global state.

After fixing this problem I will try out jit(train_op(...)). However, is there a way to directly modify
train_op = objax.Jit(train_op, gv.vars())
to account for the optimizer?

from objax.

david-berthelot avatar david-berthelot commented on July 28, 2024

I updated my comment, check if it works.

from objax.

RXZ2020 avatar RXZ2020 commented on July 28, 2024

I've changed my code per your suggestion. Unfortunately now I have this error:
TypeError: cannot convert dictionary update sequence element #0 to a sequence

from objax.

david-berthelot avatar david-berthelot commented on July 28, 2024

Updated again.

from objax.

RXZ2020 avatar RXZ2020 commented on July 28, 2024

Thanks. This completely solves my question.
Just as a suggestion (feel free to disagree here) it seems that this line
objax.ModuleList(objax.TrainRef(x) for x in model.vars().subset(objax.TrainVar))
could be made more minimalistic somewhere down the line, or at least a bit more intuitive.
All the user wants to do is to update the trainable variables in the model.
Perhaps there should be a way to directly access them with minimal code and then just update them as usual
e.g., trainable_vars = trainable_vars + lr * gradient

from objax.

david-berthelot avatar david-berthelot commented on July 28, 2024

Well consider that Objax is object-oriented and an optimizer is meant to be an objax.Module. If you look at SGD it's just 3 lines of code:
https://objax.readthedocs.io/en/latest/_modules/objax/optimizer/sgd.html#SGD

On the other hand, if you're looking for a functional design, maybe pure JAX is what will suit you best or one of the functional frameworks out there (I mean they're pretty much all functional).

from objax.

david-berthelot avatar david-berthelot commented on July 28, 2024

Reopening since I'm experimenting with design changes to allow for the original code, e.g.:

def train_op(x, y, lr):
    g, v = gv(x, y)
    for grad, p in zip(g, model_vars.subset(objax.TrainVar)):
      p.assign(p.value - lr * grad)   
    return v

from objax.

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.