Skip to content
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

Make ComponentField and PartialComponent support self argument in lambda #135

Open
jneeven opened this issue Apr 14, 2020 · 1 comment
Open
Labels
feature New feature or request

Comments

@jneeven
Copy link
Contributor

jneeven commented Apr 14, 2020

Feature motivation

Let's assume I have an extended model that takes an existing model (both models are zookeeper factories) and adds a bunch of layers on top. The most straightforward way to do this would be as follows:

    extended_model: tf.keras.models.Model = ComponentField(
        ExtendedModel, base_model=lambda self: self.model
    )

However, this raises

TypeError: Keyword arguments passed to `PartialComponent` must be either:
- An immutable value (int, float, bool, string, or None).
- A function or lambda accepting no arguments and returning the 
  value that should be passed to the component upon instantiation.
- Another `PartialComponent`.Wrapping non-immutable values in a function / lambda allows the values to be lazily evaluated; they won't be created at all if the partial component is never instantiated.

because the lambda function passed to base_model is not allowed to have any arguments. This is problematic, because it simply needs to access the existing model, which is an attribute of the surrounding task. It is also not possible to define this as a property, because ComponentField cannot be used to decorate properties.

For now, I simply have to make the base model a ComponentField in the ExtendModel class, and rely on zookeeper to properly configure it from the surrounding Experiment class, but it looks a bit confusing.

Feature description

It would be very useful if ComponentField detects whether the argument to the lambda function is self, and passes the correct value if this is the case.

@jneeven jneeven added the feature New feature or request label Apr 14, 2020
@jneeven
Copy link
Contributor Author

jneeven commented Jul 19, 2022

Just giving this issue a kick because I think this might actually improve the clarity of our experiment code a lot. We currently rely heavily on "field inheritance", but it makes tracing the origins of values very difficult. The above shouldn't be very hard to implement.

It is also not possible to define this as a property, because ComponentField cannot be used to decorate properties.
I think this might be nice to change as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant