[Feature Request] How to give cfg to class __init__ by using hydra.utils.instantiate ?
See original GitHub issue🚀 Feature Request
Motivation
From this tutorial, I can give defined params (host, user, password, database) to my class. But what if I want to give cfg
to my class ?
Pitch
Describe the solution you’d like
config.yaml:
defaults:
- dataset: mnist_dataset
- model: mnist_model
mnist_model.yaml:
model:
name: mnist
class: BaseModel # The path may wrong. But in general, I will load class BaseModel
params:
# I don't know how to define cfg in this params
class BaseModel:
def __init__(self, cfg):
self.cfg= cfg
self.logger = cfg.log
def shape():
print(self.cfg.cur_dir)
print(self.cfg.prj_dir )
@hydra.main(config_path="configs/train.yaml", strict=False)
def main(cfg: DictConfig) -> None:
# A logger for this file
cfg.log = logging.getLogger(__name__)
cfg.cur_dir = os.getcwd()
cfg.prj_dir = hydra.utils.get_original_cwd()
log.info(f"\n{cfg.pretty()}")
if cfg.model.name=="mnist":
mnist = hydra.utils.instantiate(cfg.model) # Loading BaseModel but how to give cfg to BaseModel ?
mnist.shape()
p/s: I know that we can add ${dataset}
to BaseModel
model:
name: mnist
class: BaseModel # The path may wrong. But in general, I will load class BaseModel
params:
cfg: ${dataset}
or
mnist = BaseModel(cfg)
But I just wonder if we can give cfg
to class BaseModel
by using hydra.utils.instantiate ?
Sorry for my silly question. You can close if it’s not correct.
Issue Analytics
- State:
- Created 3 years ago
- Comments:10 (6 by maintainers)
Top Results From Across the Web
Instantiating objects with Hydra
One of the best ways to drive different behavior in an application is to instantiate different implementations of an interface.
Read more >Tutorial: Learning Hydra for configuring ML experiments
I overview a number of topics, including how to instantiate classes, run sweeps over parameters, and validate the configuration at run-time.
Read more >define a value dynamically using hydra for yaml files
My issue is that in order to instanciate my class B, I need an attribute from the class A object but this attribute...
Read more >Luca's literate Emacs config
Prefer :init to :custom . Prefer multiple setq expressions to one. Default to :defer t , use :demand to force loading; When packages...
Read more >Bug listing with status RESOLVED with resolution OBSOLETE ...
status:RESOLVED resolution:OBSOLETE severity:enhancement ... Bug:94439 - "Request for jboss dependency: dev-java/gpl-util" status:RESOLVED ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
No. it’s not and can’t be. Why do you think the cfg in the model params should be the top level config? this would create a circular reference in the config and makes no sense at all.
If your BaseModel needs access to the whole config, you can pass pass it directly when you instantiate:
Keep in mind that this is an indication of problematic design. you are breaking encapsulation here. You want to allow the minimal access to BaseModel to what it needs. I am sure it doesn’t really need the whole config.