-
Notifications
You must be signed in to change notification settings - Fork 328
/
Copy pathutils.py
252 lines (205 loc) · 6.92 KB
/
utils.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import torch.nn
import torch.optim
from tensordict.nn import TensorDictModule
from tensordict.nn.distributions import NormalParamExtractor
from torchrl.collectors import SyncDataCollector
from torchrl.data import (
LazyMemmapStorage,
TensorDictPrioritizedReplayBuffer,
TensorDictReplayBuffer,
)
from torchrl.data.datasets.d4rl import D4RLExperienceReplay
from torchrl.data.replay_buffers import SamplerWithoutReplacement
from torchrl.envs import (
Compose,
DoubleToFloat,
EnvCreator,
ParallelEnv,
RewardScaling,
TransformedEnv,
)
from torchrl.envs.libs.gym import GymEnv
from torchrl.envs.utils import ExplorationType, set_exploration_type
from torchrl.modules import MLP, ProbabilisticActor, TanhNormal, ValueOperator
from torchrl.objectives import CQLLoss, SoftUpdate
from torchrl.trainers.helpers.models import ACTIVATIONS
# ====================================================================
# Environment utils
# -----------------
def env_maker(task, frame_skip=1, device="cpu", from_pixels=False):
return GymEnv(task, device=device, frame_skip=frame_skip, from_pixels=from_pixels)
def apply_env_transforms(env, reward_scaling=1.0):
transformed_env = TransformedEnv(
env,
Compose(
RewardScaling(loc=0.0, scale=reward_scaling),
DoubleToFloat(),
),
)
return transformed_env
def make_environment(cfg, num_envs=1):
"""Make environments for training and evaluation."""
parallel_env = ParallelEnv(
num_envs,
EnvCreator(lambda: env_maker(task=cfg.env.name)),
)
parallel_env.set_seed(cfg.env.seed)
train_env = apply_env_transforms(parallel_env)
eval_env = TransformedEnv(
ParallelEnv(
num_envs,
EnvCreator(lambda: env_maker(task=cfg.env.name)),
),
train_env.transform.clone(),
)
return train_env, eval_env
# ====================================================================
# Collector and replay buffer
# ---------------------------
def make_collector(cfg, train_env, actor_model_explore):
"""Make collector."""
collector = SyncDataCollector(
train_env,
actor_model_explore,
frames_per_batch=cfg.collector.frames_per_batch,
max_frames_per_traj=cfg.collector.max_frames_per_traj,
total_frames=cfg.collector.total_frames,
device=cfg.collector.collector_device,
)
collector.set_seed(cfg.env.seed)
return collector
def make_replay_buffer(
batch_size,
prb=False,
buffer_size=1000000,
buffer_scratch_dir="/tmp/",
device="cpu",
prefetch=3,
):
if prb:
replay_buffer = TensorDictPrioritizedReplayBuffer(
alpha=0.7,
beta=0.5,
pin_memory=False,
prefetch=prefetch,
storage=LazyMemmapStorage(
buffer_size,
scratch_dir=buffer_scratch_dir,
device=device,
),
batch_size=batch_size,
)
else:
replay_buffer = TensorDictReplayBuffer(
pin_memory=False,
prefetch=prefetch,
storage=LazyMemmapStorage(
buffer_size,
scratch_dir=buffer_scratch_dir,
device=device,
),
batch_size=batch_size,
)
return replay_buffer
def make_offline_replay_buffer(rb_cfg):
data = D4RLExperienceReplay(
rb_cfg.dataset,
split_trajs=False,
batch_size=rb_cfg.batch_size,
sampler=SamplerWithoutReplacement(drop_last=False),
)
data.append_transform(DoubleToFloat())
return data
# ====================================================================
# Model
# -----
#
# We give one version of the model for learning from pixels, and one for state.
# TorchRL comes in handy at this point, as the high-level interactions with
# these models is unchanged, regardless of the modality.
#
def make_cql_model(cfg, train_env, eval_env, device="cpu"):
model_cfg = cfg.model
action_spec = train_env.action_spec
actor_net, q_net = make_cql_modules_state(model_cfg, eval_env)
in_keys = ["observation"]
out_keys = ["loc", "scale"]
actor_module = TensorDictModule(actor_net, in_keys=in_keys, out_keys=out_keys)
# We use a ProbabilisticActor to make sure that we map the
# network output to the right space using a TanhDelta
# distribution.
actor = ProbabilisticActor(
module=actor_module,
in_keys=["loc", "scale"],
spec=action_spec,
distribution_class=TanhNormal,
distribution_kwargs={
"min": action_spec.space.minimum,
"max": action_spec.space.maximum,
"tanh_loc": False,
},
default_interaction_type=ExplorationType.RANDOM,
)
in_keys = ["observation", "action"]
out_keys = ["state_action_value"]
qvalue = ValueOperator(
in_keys=in_keys,
out_keys=out_keys,
module=q_net,
)
model = torch.nn.ModuleList([actor, qvalue]).to(device)
# init nets
with torch.no_grad(), set_exploration_type(ExplorationType.RANDOM):
td = eval_env.reset()
td = td.to(device)
for net in model:
net(td)
del td
eval_env.close()
return model
def make_cql_modules_state(model_cfg, proof_environment):
action_spec = proof_environment.action_spec
actor_net_kwargs = {
"num_cells": model_cfg.hidden_sizes,
"out_features": 2 * action_spec.shape[-1],
"activation_class": ACTIVATIONS[model_cfg.activation],
}
actor_net = MLP(**actor_net_kwargs)
actor_extractor = NormalParamExtractor(
scale_mapping=f"biased_softplus_{model_cfg.default_policy_scale}",
scale_lb=model_cfg.scale_lb,
)
actor_net = torch.nn.Sequential(actor_net, actor_extractor)
qvalue_net_kwargs = {
"num_cells": model_cfg.hidden_sizes,
"out_features": 1,
"activation_class": ACTIVATIONS[model_cfg.activation],
}
q_net = MLP(**qvalue_net_kwargs)
return actor_net, q_net
# ====================================================================
# CQL Loss
# ---------
def make_loss(loss_cfg, model):
loss_module = CQLLoss(
model[0],
model[1],
loss_function=loss_cfg.loss_function,
temperature=loss_cfg.temperature,
min_q_weight=loss_cfg.min_q_weight,
max_q_backup=loss_cfg.max_q_backup,
deterministic_backup=loss_cfg.deterministic_backup,
num_random=loss_cfg.num_random,
with_lagrange=loss_cfg.with_lagrange,
lagrange_thresh=loss_cfg.lagrange_thresh,
)
loss_module.make_value_estimator(gamma=loss_cfg.gamma)
target_net_updater = SoftUpdate(loss_module, tau=loss_cfg.tau)
return loss_module, target_net_updater
def make_cql_optimizer(optim_cfg, loss_module):
optim = torch.optim.Adam(
loss_module.parameters(),
lr=optim_cfg.lr,
weight_decay=optim_cfg.weight_decay,
)
return optim