blocks icon indicating copy to clipboard operation
blocks copied to clipboard

self.params should support name based access

Open janchorowski opened this issue 9 years ago • 8 comments

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.

janchorowski avatar Jun 30 '15 17:06 janchorowski

@dwf, in the context of our recent discussion this might sounds interesting to you.

rizar avatar Jun 30 '15 17:06 rizar

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.

rizar avatar Jul 01 '15 11:07 rizar

The implementation you propose should fall back on super(Parameters, self).__getattr__(name) after the loop rather than the raise statement. Otherwise SGTM.

dwf avatar Jul 02 '15 21:07 dwf

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 avatar Jul 02 '15 21:07 dwf

@dwf , I am confused, MLP does not have its own parameters at all, does it?

rizar avatar Jul 03 '15 06:07 rizar

I guess I my be thinking of brick ownership in the variable filter...

dwf avatar Jul 03 '15 07:07 dwf

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.

janchorowski avatar Jul 04 '15 07:07 janchorowski

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.

rizar avatar Jul 04 '15 16:07 rizar