-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Replacing AbstractModule and reuse_variables decorator when upgrading to Sonnet 2 #236
Comments
In your usecase you can actually move variable creation to the constructor if you prefer. the
No need for this, in Sonnet 2 variable sharing is done using regular Python objects (e.g. just create your modules once, and just create parameters of your module once, to reuse parameters just refer to the ones you made before).
It isn't needed for reusing variables (because as you say there aren't any in that method), it will have a side effect in Sonnet 1 of entering a name scope so operations would have a clear name in the TF graph, this won't affect correctness or performance but might be useful for debugging. In Sonnet 2 all module methods enter a name scope, so if you are debugging a
There are a few other changes you can make:
Here is my version of your module, seems to work: https://colab.research.google.com/gist/tomhennigan/520f004cc781e8231f53ea9dd62bea86/example-of-porting-to-sonnet-2.ipynb |
Hey @tomhennigan, thanks so much! I'm trying to migrate the rest of the code before I can fully confirm these changes work, and am working through some issues now. One of them is that Sonnet 2 throws an error when eager execution is disabled. Is there a way to get Sonnet 2 working without eager execution? |
Hi @isabellahuang , Sonnet 2 is designed to work both eagerly and inside import tensorflow as tf
print(tf.__version__) |
Thanks @tomhennigan , I think some leftover |
I want to upgrade my TF version from 1.15 to 2.5, and therefore need to use Sonnet 2 instead of Sonnet 1. I need to convert all my classes that inherit from
snt.AbstractModule
and want to know how to do it correctly.My current Sonnet 1 module is here:
and what I believe the Sonnet 2 compatible module should look like
The main changes I made are to add a
__call__()
method that initializes all the variables once (with the@snt.once
decorator), then returns the value using the old_build()
method. Is this correct?Also, how do we deal with the previous
enter_variable_scope()
call and@snt.reuse_variables
decorator? Theself.inverse
method is called outside of the module, which I understand is a reason for using@snt.reuse_variables
on it. I simply got rid of them in the new module but I'm not sure this is right.Lastly, is the
@snt.reuse_variables
decorator in the original module even used, seeing as there are notf.get_variable
calls in theself.inverse
method? Every existing example I've seen for how to use@snt.reuse_variables
has atf.get_variable
call in the method.Thanks
The text was updated successfully, but these errors were encountered: