-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtrain_offline.py
127 lines (94 loc) · 3.87 KB
/
train_offline.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import warnings
warnings.filterwarnings('ignore', category=DeprecationWarning)
import os
os.environ['MKL_SERVICE_FORCE_INTEL'] = '1'
os.environ['MUJOCO_GL'] = 'egl'
from pathlib import Path
import hydra
import numpy as np
import torch
from dm_env import specs
import dmc
import utils
from logger import Logger
from replay_buffer import make_replay_loader
from video import VideoRecorder
torch.backends.cudnn.benchmark = True
def get_domain(task):
if task.startswith('point_mass_maze'):
return 'point_mass_maze'
return task.split('_', 1)[0]
def get_data_seed(seed, num_data_seeds):
return (seed - 1) % num_data_seeds + 1
def eval(global_step, agent, env, logger, num_eval_episodes, video_recorder):
step, episode, total_reward = 0, 0, 0
eval_until_episode = utils.Until(num_eval_episodes)
while eval_until_episode(episode):
time_step = env.reset()
video_recorder.init(env, enabled=(episode == 0))
while not time_step.last():
with torch.no_grad(), utils.eval_mode(agent):
action = agent.act(time_step.observation,
global_step,
eval_mode=True)
time_step = env.step(action)
video_recorder.record(env)
total_reward += time_step.reward
step += 1
episode += 1
video_recorder.save(f'{global_step}.mp4')
with logger.log_and_dump_ctx(global_step, ty='eval') as log:
log('episode_reward', total_reward / episode)
log('episode_length', step / episode)
log('step', global_step)
@hydra.main(config_path='.', config_name='config')
def main(cfg):
work_dir = Path.cwd()
print(f'workspace: {work_dir}')
utils.set_seed_everywhere(cfg.seed)
device = torch.device(cfg.device)
# create logger
logger = Logger(work_dir, use_tb=cfg.use_tb)
# create envs
env = dmc.make(cfg.task, seed=cfg.seed)
# create agent
agent = hydra.utils.instantiate(cfg.agent,
obs_shape=env.observation_spec().shape,
action_shape=env.action_spec().shape)
# create replay buffer
data_specs = (env.observation_spec(), env.action_spec(), env.reward_spec(),
env.discount_spec())
# create data storage
domain = get_domain(cfg.task)
datasets_dir = work_dir / cfg.replay_buffer_dir
replay_dir = datasets_dir.resolve() / domain / cfg.expl_agent / 'buffer'
print(f'replay dir: {replay_dir}')
replay_loader = make_replay_loader(env, replay_dir, cfg.replay_buffer_size,
cfg.batch_size,
cfg.replay_buffer_num_workers,
cfg.discount)
replay_iter = iter(replay_loader)
# create video recorders
video_recorder = VideoRecorder(work_dir if cfg.save_video else None)
timer = utils.Timer()
global_step = 0
train_until_step = utils.Until(cfg.num_grad_steps)
eval_every_step = utils.Every(cfg.eval_every_steps)
log_every_step = utils.Every(cfg.log_every_steps)
while train_until_step(global_step):
# try to evaluate
if eval_every_step(global_step):
logger.log('eval_total_time', timer.total_time(), global_step)
eval(global_step, agent, env, logger, cfg.num_eval_episodes,
video_recorder)
metrics = agent.update(replay_iter, global_step)
logger.log_metrics(metrics, global_step, ty='train')
if log_every_step(global_step):
elapsed_time, total_time = timer.reset()
with logger.log_and_dump_ctx(global_step, ty='train') as log:
log('fps', cfg.log_every_steps / elapsed_time)
log('total_time', total_time)
log('step', global_step)
global_step += 1
if __name__ == '__main__':
main()