-
-
Notifications
You must be signed in to change notification settings - Fork 644
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
276 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
trainer: | ||
_target_: my_app.Trainer | ||
optimizer: | ||
_target_: my_app.Optimizer | ||
algo: SGD | ||
lr: 0.01 | ||
dataset: | ||
_target_: my_app.Dataset | ||
name: Imagenet | ||
path: /datasets/imagenet |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved | ||
|
||
import hydra | ||
from hydra.utils import instantiate | ||
from omegaconf import DictConfig | ||
|
||
|
||
class Optimizer: | ||
algo: str | ||
lr: float | ||
|
||
def __init__(self, algo: str, lr: float): | ||
self.algo = algo | ||
self.lr = lr | ||
|
||
def __repr__(self): | ||
return f"Optimizer(algo={self.algo},lr={self.lr})" | ||
|
||
|
||
class Dataset: | ||
name: str | ||
path: str | ||
|
||
def __init__(self, name: str, path: str): | ||
self.name = name | ||
self.path = path | ||
|
||
def __repr__(self): | ||
return f"Dataset(name={self.name}, path={self.path})" | ||
|
||
|
||
class Trainer: | ||
def __init__(self, optimizer: Optimizer, dataset: Dataset): | ||
self.optimizer = optimizer | ||
self.dataset = dataset | ||
|
||
def __repr__(self): | ||
return f"Trainer(\n optimizer={self.optimizer},\n dataset={self.dataset}\n)" | ||
|
||
|
||
@hydra.main(config_name="config") | ||
def my_app(cfg: DictConfig) -> None: | ||
optimizer = instantiate(cfg.trainer.optimizer) | ||
print(optimizer) | ||
# Optimizer(algo=SGD,lr=0.01) | ||
|
||
# override parameters on the call-site | ||
optimizer = instantiate(cfg.trainer.optimizer, lr=0.2) | ||
print(optimizer) | ||
# Optimizer(algo=SGD,lr=0.2) | ||
|
||
# recursive instantiation | ||
trainer = instantiate(cfg.trainer) | ||
print(trainer) | ||
# Trainer( | ||
# optimizer=Optimizer(algo=SGD,lr=0.01), | ||
# dataset=Dataset(name=Imagenet, path=/datasets/imagenet) | ||
# ) | ||
|
||
# override nested parameters from the call-site | ||
trainer = instantiate( | ||
cfg.trainer, | ||
optimizer={"lr": 0.3}, | ||
dataset={"name": "cifar10", "path": "/datasets/cifar10"}, | ||
) | ||
print(trainer) | ||
# Trainer( | ||
# optimizer=Optimizer(algo=SGD,lr=0.3), | ||
# dataset=Dataset(name=cifar10, path=/datasets/cifar10) | ||
# ) | ||
|
||
# non recursive instantiation | ||
optimizer = instantiate(cfg.trainer, _recursive_=False) | ||
print(optimizer) | ||
# Trainer( | ||
# optimizer={'_target_': 'my_app.Optimizer', 'algo': 'SGD', 'lr': 0.01}, | ||
# dataset={'_target_': 'my_app.Dataset', 'name': 'Imagenet', 'path': '/datasets/imagenet'} | ||
# ) | ||
|
||
|
||
if __name__ == "__main__": | ||
my_app() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.