Giter Site home page Giter Site logo

Comments (2)

tomhennigan avatar tomhennigan commented on June 14, 2024 1

Hey @hylkedonker, the con you've pointed to is the main reason we have not gone for this historically, namely that it is confusing to have a decorator that changes the signature of the function.

Haiku (like Optax, Stax and other libraries) produces a pair of functions with different signatures to the input function. You can actually treat this object as a pair if you would like:

def f(x):
  ..

f_init, f_apply = hk.transform(f)

And if you prefer to continue to reference the name f you can actually override that in the unpacking:

def f(x):
  ..

f_init, f = hk.transform(f)

params = f_init(..)
out = f(params, ..)

Again we don't suggest this in our guides since it is quite confusing for new users that the signature of f appears to have changed from the function definition.

If you really prefer stacking decorators, then you can monkey patch Haiku in place to support this functionality:

hk.Transformed.__call__ = property(fget=lambda self: self.apply)
hk.TransformedWithState.__call__ = property(fget=lambda self: self.apply)

Here is a worked example: https://colab.research.google.com/gist/tomhennigan/d9957a2233604e116d8f4ba4d9c9e3bb/add-__call__-to-transformed-withstate.ipynb

from dm-haiku.

hylkedonker avatar hylkedonker commented on June 14, 2024

Thanks for clarifying!

from dm-haiku.

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.