-
Notifications
You must be signed in to change notification settings - Fork 11
/
main_hydra.py
55 lines (35 loc) · 1.57 KB
/
main_hydra.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# Copyright 2022 Twitter, Inc.
# SPDX-License-Identifier: Apache-2.0
import torch as th
import hydra
import logger
def log_dict(dictionary):
for k, v in dictionary.items():
logger.log_key_val(k, v)
def make_models(cfg):
if th.cuda.is_available() and not cfg.disable_cuda:
cfg.device = 'cuda'
else:
cfg.device = 'cpu'
print('Running experiment on {}'.format(cfg.device))
env = hydra.utils.instantiate(cfg.env)
cfg.obs_dims = env.ob_space['rgb'].shape[::-1]
cfg.n_actions = env.ac_space.eltype.n
cfg.act_dims = []
preprocessor = hydra.utils.instantiate(cfg.preprocessor)
tester = hydra.utils.instantiate(cfg.tester, preprocessor=preprocessor, _recursive_=False)
buffer = hydra.utils.instantiate(cfg.buffer, preprocessor=preprocessor)
agent = hydra.utils.instantiate(cfg.agent, _recursive_=False)
return agent, buffer, env, tester, preprocessor
def train(cfg, agent, buffer, env, tester, preprocessor):
hydra.utils.call(cfg.training_fn, agent=agent, buffer=buffer, env=env,
tester=tester, preprocessor=preprocessor)
return agent, buffer, env, tester, preprocessor
@hydra.main(config_path='cfgs', config_name='config')
def main(cfg):
agent, buffer, env, tester, preprocessor = make_models(cfg)
hydra.utils.call(cfg.training_fn, agent=agent, buffer=buffer, env=env,
tester=tester, preprocessor=preprocessor) #run_training(agent, buffer, env, tester, preprocessor, cfg)
return agent, buffer, env, tester, preprocessor
if __name__ == '__main__':
main()