Linen: Consider raising an error when reading variables from submodule before initialization?
See original GitHub issueWithin a module that uses shape-inference (as most of the built-in Linen modules do), this code is fine:
class MyModule(nn.Module):
def __call__(self, x):
conv = nn.Conv(features=3)
y = conv(x)
params = conv.params()
But if you instead do:
class MyModule(nn.Module):
def __call__(self, x):
conv = nn.Conv(features=3)
params = conv.params()
y = conv(x)
Then I believe you get an empty params
dict (as the parameters of conv
are only initialized once the input shape is known)
Seems like users may be surprised about this, so instead we could just raise an error if the variables are empty when reading them clarifying what is happening, to guide users to the right direction.
Issue Analytics
- State:
- Created 3 years ago
- Comments:10 (5 by maintainers)
Top Results From Across the Web
flax.errors
[docs]class NameInUseError(FlaxError): """This error is raised when trying to create a submodule, param, or variable with an existing name. They are all ...
Read more >Environment variable causing git submodule to fail
If this environment variable is set when calling git submodule , the value of that variable will be appended to the command line....
Read more >WD 1539-1(alt) 08-007 minus coarrays - WG5 Fortran
If a named variable that was not in a common block was initialized in a DATA ... are considered to have been redundant...
Read more >git-submodule Documentation - Git
git-submodule - Initialize, update or inspect submodules ... When the command is run without pathspec, it errors out, instead of deinit-ing everything, ...
Read more >you might be seeing this error because you're using the wrong ...
An example Django + Docker app You could use this example app as a base for your new project or as a guide...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Still there might be cases where you want to access variables even though they aren’t final. Let’s say I want to access a kernel but I didn’t necessarily define the bias yet. A simple solution would be a helpful reminder to the user. For example variables could be a subclass of FrozenDict that returns a helpful error on KeyError like “Perhaps the variable you are trying to access is not initialized yet…”
Based on @jheek’s comment in #638, how about we do the following:
ErrorHandlers
dataclass, e.g:FrozenDict
to have a_error_handlers
field and add logic to use the handlers in a couple of places.set_error_handlers
method toFrozenDict
as proposed by @jheek which takes in the handlers, only modification I would propose is that it returns a newFrozenDict
to keep the API immutable.We could start with a single handler that solve this specific issue.