-
Notifications
You must be signed in to change notification settings - Fork 661
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
Requesting support for arbitrary pytrees of Module params #705
Comments
A workaround for lists is to define a list of name strings, and then iterate over it, assigning params directly to the Module. This seems to work but obviously is quite cumbersome. class Bar(nn.Module):
def setup(self):
param_names = [f'param_list_{i}' for i in range(4)]
for n in param_names:
self.__setattr__(
n, self.param(n, nn.initializers.zeros, (1, 2, 3)))
self.param_names = param_names
def __call__(self):
return self.param_list_0
bar = Bar().init({'params': random.PRNGKey(0)}) |
Yes, this exposes a problem with our current design for Here's the gist of the problem: At what point should parameters (or, in general, variables) be bound (meaning -- have a parent and a name -- giving them a location in the variable tree)? Our current solution says "give it a parent and a name during the call to Instead, we can entirely remove the bad heuristic, and say: parameters defined in setup should not use I can't commit to a timeline for this change as there are many details to get right... For now, maybe we can find a reasonable intermediate solution: Perhaps we simply shouldn't apply our "test that the attribute name matches the defined parameter name" for nested parameters... Seems like our heuristic can only be wrong in this case, so we might as well let people define their names like you've been doing. |
We currently try to enforce that a user can't do: ```py class MyModule(nn.Module): def setup(self): self.name1 = self.param('name2', ...) ``` But the check is brittle and entirely disallows assigning dicts or lists of parameters. This check relaxes our heuristic such that it doesn't run at all for lists or dicts of parameters assigned to attributes during setup. See google#705 (comment) for a bit more context.
Yes it does, thanks! |
As mentioned in the docs,
Module.__setattr__
allows for assigning arbitrary pytrees of submodules insetup
. I would like to assign a list ofparam
s during setup. The following fails:with
Setting the names of each list element manually also fails:
this gives a different error though:
Would it be possible for an appropriate name string to be automatically inferred rather than being supplied by the user? I'm sure there's a good reason that it is the way it is, apologies if this has already been brought up elsewhere and I've missed it, just curious.
The text was updated successfully, but these errors were encountered: