question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Linen: Consider raising an error when reading variables from submodule before initialization?

See original GitHub issue

Within 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:open
  • Created 3 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jheekcommented, Nov 12, 2020

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…”

0reactions
cgarciaecommented, Sep 5, 2022

Based on @jheek’s comment in #638, how about we do the following:

  1. Create a simple ErrorHandlers dataclass, e.g:
@dataclass(frozen=True)
class ErrorHandlers:
  missing_key: Optional[Callable[[str], Exception]] = None
  set_item: Optional[Callable[[str, Any], Exception]] = None
  ...
  1. Update FrozenDict to have a _error_handlers field and add logic to use the handlers in a couple of places.
  2. Add a set_error_handlers method to FrozenDict as proposed by @jheek which takes in the handlers, only modification I would propose is that it returns a new FrozenDict to keep the API immutable.
xs = FrozenDict(variables)
xs = xs.set_error_handlers(missing_key=lambda key: Error(...), set_item=lambda key, value: Error(...)

We could start with a single handler that solve this specific issue.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found