-
Notifications
You must be signed in to change notification settings - Fork 1
/
train_rl_regressor.py
203 lines (159 loc) · 6.95 KB
/
train_rl_regressor.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
"""
RL approximator training loop.
Works on DMC with states and pixel observations.
"""
import warnings
warnings.filterwarnings('ignore', category=DeprecationWarning)
import os
import platform
import logging
import math
if platform.system() == 'Linux':
os.environ['MKL_SERVICE_FORCE_INTEL'] = '1'
os.environ['MUJOCO_GL'] = 'egl'
from pathlib import Path
import hydra
import omegaconf
import torch
from hydra.core.hydra_config import HydraConfig
from torch.utils.tensorboard import SummaryWriter
import utils.utils as utils
from utils.dataset import RLSolutionDataset, RLSolutionMetaDataset
from utils.dataloader import FastTensorDataLoader, FastTensorMetaDataLoader
torch.backends.cudnn.benchmark = True
# If using multirun, set the GPUs here:
AVAILABLE_GPUS = [1, 2, 3, 4, 0]
def make_approximator(input_dim, state_dim, action_dim, cfg, device=None):
cfg.input_dim = input_dim
cfg.state_dim = state_dim
cfg.action_dim = action_dim
if device is not None:
cfg.device = device
return hydra.utils.instantiate(cfg)
class Workspace:
def __init__(self, cfg):
self.work_dir = Path.cwd()
self.cfg = cfg
utils.set_seed_everywhere(cfg.seed)
self.device = torch.device(cfg.device)
# hacked up way to see if we are using MAML or not
self.is_meta_learning = True if 'meta' in self.cfg.approximator_name else False
self.setup()
if cfg.input_to_model == 'rew':
input_dim = self.dataset.reward_param_dim
elif cfg.input_to_model == 'dyn':
input_dim = self.dataset.dynamic_param_dim
elif cfg.input_to_model == 'rew_dyn':
input_dim = self.dataset.reward_dynamic_param_dim
else:
raise NotImplementedError
self.approximator = make_approximator(input_dim,
self.dataset.state_dim,
self.dataset.action_dim,
self.cfg.approximator)
self.timer = utils.Timer()
self._global_epoch = 0
self._global_episode = 0
def setup(self):
# create logger
self.logger = SummaryWriter(str(self.work_dir))
self.model_dir = self.work_dir / 'models'
self.model_dir.mkdir(exist_ok=True)
self.rollout_dir = Path(self.cfg.rollout_dir).expanduser().joinpath(self.cfg.domain_task)
# load dataset
self.load_dataset()
# save cfg and git sha
utils.save_cfg(self.cfg, self.work_dir)
utils.save_git_sha(self.work_dir)
def load_dataset(self):
dataset_fn = RLSolutionMetaDataset if self.is_meta_learning else RLSolutionDataset
dataloader_fn = FastTensorMetaDataLoader if self.is_meta_learning else FastTensorDataLoader
self.dataset = dataset_fn(
self.rollout_dir,
self.cfg.domain_task,
self.cfg.input_to_model,
self.cfg.seed,
self.device,
)
if self.is_meta_learning:
batch_size = int(self.dataset.n_tasks * self.cfg.k_shot * 2)
else:
batch_size = self.cfg.batch_size
self.train_loader = dataloader_fn(*self.dataset.train_dataset[:], device=self.device,
batch_size=batch_size, shuffle=True)
self.test_loader = dataloader_fn(*self.dataset.test_dataset[:], device=self.device,
batch_size=batch_size, shuffle=True)
@property
def global_epoch(self):
return self._global_epoch
def train(self):
# predicates
train_until_epoch = utils.Until(self.cfg.num_train_epochs)
save_every_epoch = utils.Every(self.cfg.save_every_frames)
metrics = dict()
best_valid_total_loss = math.inf
best_valid_value_loss = math.inf
best_valid_action_loss = math.inf
best_valid_td_loss = math.inf
while train_until_epoch(self.global_epoch):
metrics.update()
if self.is_meta_learning:
self.train_loader.shuffle_indices()
self.test_loader.shuffle_indices()
metrics.update(self.approximator.update(self.train_loader))
metrics.update(self.approximator.eval(self.test_loader))
# Log metrics
print(f"Epoch {self.global_epoch + 1} "
f"\t Train loss {metrics['train/loss_total']:.3f} "
f"\t Valid loss {metrics['valid/loss_total']:.3f}")
for k, v in metrics.items():
self.logger.add_scalar(k, v, self.global_epoch + 1)
utils.dump_dict(f"{self.work_dir}/train_valid.csv", metrics)
# Save the model
if metrics['valid/loss_total'] <= best_valid_total_loss:
best_valid_total_loss = metrics['valid/loss_total']
self.approximator.save(self.model_dir, 'best_total')
if metrics['valid/loss_action_pred'] <= best_valid_action_loss:
best_valid_action_loss = metrics['valid/loss_action_pred']
self.approximator.save(self.model_dir, 'best_action')
if 'valid/loss_value_pred' in metrics:
if metrics['valid/loss_value_pred'] <= best_valid_value_loss:
best_valid_value_loss = metrics['valid/loss_value_pred']
self.approximator.save(self.model_dir, 'best_value')
if 'valid/loss_td' in metrics:
if metrics['valid/loss_td'] <= best_valid_td_loss:
best_valid_td_loss = metrics['valid/loss_td']
self.approximator.save(self.model_dir, 'best_td')
if save_every_epoch(self.global_epoch + 1):
self.approximator.save(self.model_dir, self.global_epoch + 1)
self._global_epoch += 1
def save_snapshot(self):
snapshot = self.work_dir / 'snapshot.pt'
keys_to_save = ['agent', 'timer', '_global_step', '_global_episode']
payload = {k: self.__dict__[k] for k in keys_to_save}
with snapshot.open('wb') as f:
torch.save(payload, f)
def load_snapshot(self):
snapshot = self.work_dir / 'snapshot.pt'
with snapshot.open('rb') as f:
payload = torch.load(f)
for k, v in payload.items():
self.__dict__[k] = v
@hydra.main(version_base=None, config_path='cfgs', config_name='config_rl_approximator')
def main(cfg):
log = logging.getLogger(__name__)
try:
device_id = AVAILABLE_GPUS[HydraConfig.get().job.num % len(AVAILABLE_GPUS)]
cfg.device = f"{cfg.device}:{device_id}"
log.info(f"Total number of GPUs is {AVAILABLE_GPUS}, running on {cfg.device}.")
except omegaconf.errors.MissingMandatoryValue:
pass
root_dir = Path.cwd()
workspace = Workspace(cfg)
snapshot = root_dir / 'snapshot.pt'
if snapshot.exists():
print(f'resuming: {snapshot}')
workspace.load_snapshot()
workspace.train()
if __name__ == '__main__':
main()