question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[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:closed
  • Created 3 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
omrycommented, Apr 1, 2020
import hydra
from omegaconf import OmegaConf


class BaseModel:
    def __init__(self, cfg):
        self.cfg = cfg

    def shape(self):
        print("cfg.name:", self.cfg.name)


if __name__ == "__main__":
    cfg = OmegaConf.create(
        {"name": "mnist", "model": {"class": "__main__.BaseModel", "params": {}}}
    )

    model = hydra.utils.instantiate(cfg.model, cfg)
    model.shape()

1reaction
omrycommented, Apr 1, 2020

The cfg here is the cfg: DictConfig in main()

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:

instantiate(cfg.model, cfg)

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found