New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FLIP: Support __init__ in Modules #161
Comments
In the proposed syntax, why can’t the user write |
That will work too. In this case it also doesn't matter because the function is a one-liner. If it is more complicated one might want to use the module method from within apply for code reuse
In the example we use the encoder and decoder at most once. So in this case partial would also work. |
This would be useful for normalizing flows as well. In the meantime, there seems to be a namespace issue with the def compose_transforms(transforms):
class TransformSequence(Transform):
def _shared_modules(self):
return [t.shared() for t in transforms]
@flax.nn.module_method
def transform(self, x):
transforms = self._shared_modules()
for t in transforms:
x = t.transform(x)
return x
@flax.nn.module_method
def inverse_and_log_det_jac(self, y):
transforms = self._shared_modules()
log_det_jac = 0.0
for t in reversed(transforms):
y, term = t.inverse_and_log_det_jac(y)
log_det_jac += term
return y, log_det_jac
return TransformSequence Is there a better workaround for now? |
My main worry about supporting Some ideas:
The magic separation of arguments between |
@mattwescott I think your issue was introduced in a recent change to the default name policy. This is probably a bug that should be fixed. I'll look into it ASAP |
I agree with this proposal
That's a very big change to make and takes away a key advantage of calling modules as functions.
I do really dislike the introspection as well. Passing all |
This is no longer relevant since Linen has landed, so I'm closing this for now. |
Introduction
By default Modules are defined using only an apply function and unshared parameters and submodules. This makes it easy to write modules (with control flow) in a concise and readable way.
However, some modules don't fit well in this abstraction. For example consider an autoencoder. During model training we would like to take an observed example and encode and decode it. However, we would also like to be able to call the
encode
anddecode
procedures as separate methods for other use cases besides training.With the current Flax api a simple AutoEncoder can be written as follows:
A number of issues can be noticed in this examples:
_create_modules
behaves a lot like a constructor but also needs to be called manuallyencode
anddecode
from apply leading to even more code duplicationProposal
We would like to improve the syntax of modules that require multi methods and reuse of parameters, sub modules, and hyperparameters across various methods.
The proposed syntax allows us to rewrite the
AutoEncoder
module as followsA few differences w.r.t. to the introduction example:
setup
) defines shared modules and assigns them to fields.A few changes are required to make the new syntax work
When a Module is constructed we must first call the
setup
function. Thesetup
function receives allkwargs
and returns the remaining keyword arguments that should be passed to the module method.when calling a module_method using
self.some_module_method(...)
it behaves as an ordinary python method.An implementation of this proposal lives in draft PR #104
Alternatives
The main issue in this proposal is determining which arguments are passed to
setup
. There are a few variations that can be considered:Introspection is used to determine which keyword arguments belong to setup.
Require users to provide a list of construction arguments
Pass all keyword argument to
setup
. This woud make the implementation easier but would require most module methods to include something like**unused_kwargs
to work correctly.[CURRENT PROPOSAL]
setup
receives all keyword arguments and returns a dictionary of keyword arguments that should be passed to the apply method and other module methodsThe text was updated successfully, but these errors were encountered: