blocks
blocks copied to clipboard
self.params should support name based access
Since all parametets have names, we can easily add name-based access to self.params:
class Parameters(AnnotatingList):
"""Adds the PARAMETER role to parameters automatically."""
...
def __getattr__(self, name):
for l in self:
if getattr(object, 'name', None) == name:
return l
raise AttributeError
then we can write self.params.weights instead of declaring a property or remember the parameter's order.
Searching through a list shouldn't matter speed-wise for the small number of parameters bricks typically have.
@dwf, in the context of our recent discussion this might sounds interesting to you.
It seems to me that adding this to Blocks and getting read of all parameter getting properties is really simple and uncontroversial, and even does not require any documentation changes.
The implementation you propose should fall back on super(Parameters, self).__getattr__(name)
after the loop rather than the raise statement. Otherwise SGTM.
Oh, one complication: names are not unique on nontrivial Bricks like MLP... so this functionality could be a source of hard-to-find bugs. We could maybe address this by adding a flag to the Parameters
class that enforces this uniqueness and refusing to do attribute-style lookup if it is not set.
@dwf , I am confused, MLP
does not have its own parameters at all, does it?
I guess I my be thinking of brick ownership in the variable filter...
I implemented a test version of this and with a fix for pickling it works. Checking for name uniqueness is a good idea, I guess other code such as Model assumes name uniqueness.
We definitely assume in many places of Blocks code that parameter names of all bricks are unique. That indeed could be explicitly checked by the Parameters
object.