-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun_offline.py
executable file
·170 lines (138 loc) · 5.3 KB
/
run_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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import argparse
import copy
import math
import random
import hydra
import numpy as np
import omegaconf
import tqdm
from dotmap import DotMap
from scale_rl.agents import create_agent
from scale_rl.buffers import create_buffer
from scale_rl.common import WandbTrainerLogger
from scale_rl.envs import create_dataset, create_envs, get_normalized_score
from scale_rl.evaluation import evaluate, record_video
def run(args):
###############################
# configs
###############################
args = DotMap(args)
config_path = args.config_path
config_name = args.config_name
overrides = args.overrides
hydra.initialize(version_base=None, config_path=config_path)
cfg = hydra.compose(config_name=config_name, overrides=overrides)
def eval_resolver(s: str):
return eval(s)
omegaconf.OmegaConf.register_new_resolver("eval", eval_resolver)
omegaconf.OmegaConf.resolve(cfg)
np.random.seed(cfg.seed)
random.seed(cfg.seed)
#############################
# envs
#############################
train_env, eval_env = create_envs(**cfg.env)
observation_space = train_env.observation_space
action_space = train_env.action_space
dataset = create_dataset(cfg.env.env_type, cfg.env.env_name)
#############################
# buffer
#############################
cfg.buffer.max_length = len(dataset)
buffer = create_buffer(
observation_space=observation_space, action_space=action_space, **cfg.buffer
)
buffer.reset()
#############################
# fill buffer
#############################
for i, timestep in tqdm.tqdm(
list(enumerate(dataset)), desc="Filling buffer with dataset"
):
buffer.add(timestep)
#############################
# agent
#############################
batch_size = cfg.buffer.sample_batch_size
cfg.num_interaction_steps = int((len(dataset) / batch_size) * cfg.num_epochs)
cfg.save_checkpoint_per_interaction_step = cfg.num_interaction_steps
cfg.agent.learning_rate_decay_step = int(
cfg.agent.learning_rate_decay_rate
* cfg.num_interaction_steps
* cfg.updates_per_interaction_step
)
agent = create_agent(
observation_space=observation_space,
action_space=action_space,
cfg=cfg.agent,
)
# iterate over buffer to update normalizers
num_batches = int(np.floor(len(dataset) / batch_size))
for batch_num in tqdm.tqdm(range(num_batches), desc="updating normalizers"):
start_idx = batch_num * batch_size
end_idx = start_idx + batch_size
batch_indices = np.arange(start_idx, end_idx)
batch = buffer.sample(sample_idxs=batch_indices)
# update normalizers
agent.sample_actions(i, copy.deepcopy(batch), training=True)
#############################
# train offline
#############################
logger = WandbTrainerLogger(cfg)
# initial evaluation
eval_info = evaluate(agent, eval_env, cfg.num_eval_episodes)
eval_info["avg_normalized_return"] = get_normalized_score(
cfg.env.env_type, cfg.env.env_name, eval_info["avg_return"]
)
logger.update_metric(**eval_info)
logger.log_metric(step=0)
logger.reset()
# start training
update_step = 0
for interaction_step in tqdm.tqdm(
range(1, int(cfg.num_interaction_steps + 1)), smoothing=0.1
):
# update network
batch = buffer.sample()
update_info = agent.update(update_step, batch)
logger.update_metric(**update_info)
update_step += 1
# evaluation
if interaction_step % cfg.evaluation_per_interaction_step == 0:
eval_info = evaluate(agent, eval_env, cfg.num_eval_episodes)
eval_info["avg_normalized_return"] = get_normalized_score(
cfg.env.env_type, cfg.env.env_name, eval_info["avg_return"]
)
logger.update_metric(**eval_info)
# metrics
if interaction_step % cfg.metrics_per_interaction_step == 0:
batch = buffer.sample()
metrics_info = agent.get_metrics(batch, update_info)
if metrics_info:
logger.update_metric(**metrics_info)
# TODO Support video recording
# # video recording
# if offline_step % cfg.offline.recording_per_offline_step == 0:
# video_info = record_video(agent, eval_env, cfg.num_record_episodes)
# logger.update_metric(**video_info)
# logging
if interaction_step % cfg.logging_per_interaction_step == 0:
logger.log_metric(step=interaction_step)
logger.reset()
# final evaluation
eval_info = evaluate(agent, eval_env, cfg.num_eval_episodes)
eval_info["avg_normalized_return"] = get_normalized_score(
cfg.env.env_type, cfg.env.env_name, eval_info["avg_return"]
)
logger.update_metric(**eval_info)
logger.log_metric(step=interaction_step)
logger.reset()
train_env.close()
eval_env.close()
if __name__ == "__main__":
parser = argparse.ArgumentParser(allow_abbrev=False)
parser.add_argument("--config_path", type=str, default="./configs")
parser.add_argument("--config_name", type=str, default="offline_rl")
parser.add_argument("--overrides", action="append", default=[])
args = parser.parse_args()
run(vars(args))