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

Recursive Object Creation [Feature Request] #622

Closed
sebascuri opened this issue May 29, 2020 · 3 comments
Closed

Recursive Object Creation [Feature Request] #622

sebascuri opened this issue May 29, 2020 · 3 comments
Labels
enhancement Enhanvement request

Comments

@sebascuri
Copy link

sebascuri commented May 29, 2020

πŸš€ Feature Request

It is useful to have a recursive object initialization.

Motivation

Suppose I have the following structure

β”œ ── conf
|   └── config.yaml
β”‚   └── agent
|        └── agent.yaml
|        └── complex_property
|               └── complex_property_1.yaml
|               └── complex_property_2.yaml
|          
└── my_app.py

The class Agent is defined as follows

class Agent(object):
    def __init__(self, name: str, complex_property: ComplexProperty, seed: int = 0) -> None:
         self.name = name
         self.complex_property = complex_property
         self.seed = seed 

The class ComplexProperty is defined as follows

class ComplexProperty(object):
    def __init__(self, code_number: int, seed: int = 0) -> None:
        self.code_number = code_number
        self.seed = seed

The agent.yaml looks like:

agent:
   class: path_to_agent.Agent
   params:
       name: great_agent
       seed: 0 
       complex_property:
              class: path_to_complex_property.ComplexProperty
              params: 
                   code_number: 1
                   seed: 0

Pitch

Describe the solution you'd like
I'd like to be able to call hydra.utils.instantiate(cfg.agent) and this should recursively initialize the parameters.

I'd also like to be able to default one of the complex_property and be able to override them from the CLI.

I'd also like to be able to default a complex_property that is not necessarily in a sub-directory (by giving a relative path to the .yaml file for instance).

Describe alternatives you've considered
I just initialized the complex_property with hydra.utils.instantiate and then within python initialize the agent.

@sebascuri sebascuri added the enhancement Enhanvement request label May 29, 2020
@sebascuri
Copy link
Author

It is also useful to set a global parameter (in this case the seed) in the config.yaml, such that if you modify it from the CLI, you modify it in all the configs, too.

@omry
Copy link
Collaborator

omry commented May 29, 2020

Please see #566. It is an RFC (Request for comments) for a possible approach.

@omry
Copy link
Collaborator

omry commented May 29, 2020

It is also useful to set a global parameter (in this case the seed) in the config.yaml, such that if you modify it from the CLI, you modify it in all the configs, too.

See interpolations in the OmegaConf documentation.

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

No branches or pull requests

2 participants