forked from tinkoff-ai/CORL
-
Notifications
You must be signed in to change notification settings - Fork 23
/
any_percent_bc.py
420 lines (350 loc) · 13.2 KB
/
any_percent_bc.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
import os
import random
import uuid
from dataclasses import asdict, dataclass
from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple, Union
import d4rl
import gym
import numpy as np
import pyrallis
import torch
import torch.nn as nn
import torch.nn.functional as F
import wandb
TensorBatch = List[torch.Tensor]
@dataclass
class TrainConfig:
# wandb project name
project: str = "CORL"
# wandb group name
group: str = "BC-D4RL"
# wandb run name
name: str = "BC"
# training dataset and evaluation environment
env: str = "halfcheetah-medium-expert-v2"
# total gradient updates during training
max_timesteps: int = int(1e6)
# training batch size
batch_size: int = 256
# maximum size of the replay buffer
buffer_size: int = 2_000_000
# what top fraction of the dataset (sorted by return) to use
frac: float = 0.1
# maximum possible trajectory length
max_traj_len: int = 1000
# whether to normalize states
normalize: bool = True
# discount factor
discount: float = 0.99
# evaluation frequency, will evaluate eval_freq training steps
eval_freq: int = int(5e3)
# number of episodes to run during evaluation
n_episodes: int = 10
# path for checkpoints saving, optional
checkpoints_path: Optional[str] = None
# file name for loading a model, optional
load_model: str = ""
# training random seed
seed: int = 0
# training device
device: str = "cuda"
def __post_init__(self):
self.name = f"{self.name}-{self.env}-{str(uuid.uuid4())[:8]}"
if self.checkpoints_path is not None:
self.checkpoints_path = os.path.join(self.checkpoints_path, self.name)
def soft_update(target: nn.Module, source: nn.Module, tau: float):
for target_param, source_param in zip(target.parameters(), source.parameters()):
target_param.data.copy_((1 - tau) * target_param.data + tau * source_param.data)
def compute_mean_std(states: np.ndarray, eps: float) -> Tuple[np.ndarray, np.ndarray]:
mean = states.mean(0)
std = states.std(0) + eps
return mean, std
def normalize_states(states: np.ndarray, mean: np.ndarray, std: np.ndarray):
return (states - mean) / std
def wrap_env(
env: gym.Env,
state_mean: Union[np.ndarray, float] = 0.0,
state_std: Union[np.ndarray, float] = 1.0,
reward_scale: float = 1.0,
) -> gym.Env:
# PEP 8: E731 do not assign a lambda expression, use a def
def normalize_state(state):
return (
state - state_mean
) / state_std # epsilon should be already added in std.
def scale_reward(reward):
# Please be careful, here reward is multiplied by scale!
return reward_scale * reward
env = gym.wrappers.TransformObservation(env, normalize_state)
if reward_scale != 1.0:
env = gym.wrappers.TransformReward(env, scale_reward)
return env
class ReplayBuffer:
def __init__(
self,
state_dim: int,
action_dim: int,
buffer_size: int,
device: str = "cpu",
):
self._buffer_size = buffer_size
self._pointer = 0
self._size = 0
self._states = torch.zeros(
(buffer_size, state_dim), dtype=torch.float32, device=device
)
self._actions = torch.zeros(
(buffer_size, action_dim), dtype=torch.float32, device=device
)
self._rewards = torch.zeros((buffer_size, 1), dtype=torch.float32, device=device)
self._next_states = torch.zeros(
(buffer_size, state_dim), dtype=torch.float32, device=device
)
self._dones = torch.zeros((buffer_size, 1), dtype=torch.float32, device=device)
self._device = device
def _to_tensor(self, data: np.ndarray) -> torch.Tensor:
return torch.tensor(data, dtype=torch.float32, device=self._device)
# Loads data in d4rl format, i.e. from Dict[str, np.array].
def load_d4rl_dataset(self, data: Dict[str, np.ndarray]):
if self._size != 0:
raise ValueError("Trying to load data into non-empty replay buffer")
n_transitions = data["observations"].shape[0]
if n_transitions > self._buffer_size:
raise ValueError(
"Replay buffer is smaller than the dataset you are trying to load!"
)
self._states[:n_transitions] = self._to_tensor(data["observations"])
self._actions[:n_transitions] = self._to_tensor(data["actions"])
self._rewards[:n_transitions] = self._to_tensor(data["rewards"][..., None])
self._next_states[:n_transitions] = self._to_tensor(data["next_observations"])
self._dones[:n_transitions] = self._to_tensor(data["terminals"][..., None])
self._size += n_transitions
self._pointer = min(self._size, n_transitions)
print(f"Dataset size: {n_transitions}")
def sample(self, batch_size: int) -> TensorBatch:
indices = np.random.randint(0, min(self._size, self._pointer), size=batch_size)
states = self._states[indices]
actions = self._actions[indices]
rewards = self._rewards[indices]
next_states = self._next_states[indices]
dones = self._dones[indices]
return [states, actions, rewards, next_states, dones]
def add_transition(self):
# Use this method to add new data into the replay buffer during fine-tuning.
# I left it unimplemented since now we do not do fine-tuning.
raise NotImplementedError
def set_seed(
seed: int, env: Optional[gym.Env] = None, deterministic_torch: bool = False
):
if env is not None:
env.seed(seed)
env.action_space.seed(seed)
os.environ["PYTHONHASHSEED"] = str(seed)
np.random.seed(seed)
random.seed(seed)
torch.manual_seed(seed)
torch.use_deterministic_algorithms(deterministic_torch)
def wandb_init(config: dict) -> None:
wandb.init(
config=config,
project=config["project"],
group=config["group"],
name=config["name"],
id=str(uuid.uuid4()),
)
wandb.run.save()
@torch.no_grad()
def eval_actor(
env: gym.Env, actor: nn.Module, device: str, n_episodes: int, seed: int
) -> np.ndarray:
env.seed(seed)
actor.eval()
episode_rewards = []
for _ in range(n_episodes):
state, done = env.reset(), False
episode_reward = 0.0
while not done:
action = actor.act(state, device)
state, reward, done, _ = env.step(action)
episode_reward += reward
episode_rewards.append(episode_reward)
actor.train()
return np.asarray(episode_rewards)
def keep_best_trajectories(
dataset: Dict[str, np.ndarray],
frac: float,
discount: float,
max_episode_steps: int = 1000,
):
ids_by_trajectories = []
returns = []
cur_ids = []
cur_return = 0
reward_scale = 1.0
for i, (reward, done) in enumerate(zip(dataset["rewards"], dataset["terminals"])):
cur_return += reward_scale * reward
cur_ids.append(i)
reward_scale *= discount
if done == 1.0 or len(cur_ids) == max_episode_steps:
ids_by_trajectories.append(list(cur_ids))
returns.append(cur_return)
cur_ids = []
cur_return = 0
reward_scale = 1.0
sort_ord = np.argsort(returns, axis=0)[::-1].reshape(-1)
top_trajs = sort_ord[: max(1, int(frac * len(sort_ord)))]
order = []
for i in top_trajs:
order += ids_by_trajectories[i]
order = np.array(order)
dataset["observations"] = dataset["observations"][order]
dataset["actions"] = dataset["actions"][order]
dataset["next_observations"] = dataset["next_observations"][order]
dataset["rewards"] = dataset["rewards"][order]
dataset["terminals"] = dataset["terminals"][order]
class Actor(nn.Module):
def __init__(self, state_dim: int, action_dim: int, max_action: float):
super(Actor, self).__init__()
self.net = nn.Sequential(
nn.Linear(state_dim, 256),
nn.ReLU(),
nn.Linear(256, 256),
nn.ReLU(),
nn.Linear(256, action_dim),
nn.Tanh(),
)
self.max_action = max_action
def forward(self, state: torch.Tensor) -> torch.Tensor:
return self.max_action * self.net(state)
@torch.no_grad()
def act(self, state: np.ndarray, device: str = "cpu") -> np.ndarray:
state = torch.tensor(state.reshape(1, -1), device=device, dtype=torch.float32)
return self(state).cpu().data.numpy().flatten()
class BC:
def __init__(
self,
max_action: np.ndarray,
actor: nn.Module,
actor_optimizer: torch.optim.Optimizer,
discount: float = 0.99,
device: str = "cpu",
):
self.actor = actor
self.actor_optimizer = actor_optimizer
self.max_action = max_action
self.discount = discount
self.total_it = 0
self.device = device
def train(self, batch: TensorBatch) -> Dict[str, float]:
log_dict = {}
self.total_it += 1
state, action, _, _, _ = batch
# Compute actor loss
pi = self.actor(state)
actor_loss = F.mse_loss(pi, action)
log_dict["actor_loss"] = actor_loss.item()
# Optimize the actor
self.actor_optimizer.zero_grad()
actor_loss.backward()
self.actor_optimizer.step()
return log_dict
def state_dict(self) -> Dict[str, Any]:
return {
"actor": self.actor.state_dict(),
"actor_optimizer": self.actor_optimizer.state_dict(),
"total_it": self.total_it,
}
def load_state_dict(self, state_dict: Dict[str, Any]):
self.actor.load_state_dict(state_dict["actor"])
self.actor_optimizer.load_state_dict(state_dict["actor_optimizer"])
self.total_it = state_dict["total_it"]
@pyrallis.wrap()
def train(config: TrainConfig):
env = gym.make(config.env)
state_dim = env.observation_space.shape[0]
action_dim = env.action_space.shape[0]
dataset = d4rl.qlearning_dataset(env)
keep_best_trajectories(dataset, config.frac, config.discount)
if config.normalize:
state_mean, state_std = compute_mean_std(dataset["observations"], eps=1e-3)
else:
state_mean, state_std = 0, 1
dataset["observations"] = normalize_states(
dataset["observations"], state_mean, state_std
)
dataset["next_observations"] = normalize_states(
dataset["next_observations"], state_mean, state_std
)
env = wrap_env(env, state_mean=state_mean, state_std=state_std)
replay_buffer = ReplayBuffer(
state_dim,
action_dim,
config.buffer_size,
config.device,
)
replay_buffer.load_d4rl_dataset(dataset)
if config.checkpoints_path is not None:
print(f"Checkpoints path: {config.checkpoints_path}")
os.makedirs(config.checkpoints_path, exist_ok=True)
with open(os.path.join(config.checkpoints_path, "config.yaml"), "w") as f:
pyrallis.dump(config, f)
max_action = float(env.action_space.high[0])
# Set seeds
seed = config.seed
set_seed(seed, env)
actor = Actor(state_dim, action_dim, max_action).to(config.device)
actor_optimizer = torch.optim.Adam(actor.parameters(), lr=3e-4)
kwargs = {
"max_action": max_action,
"actor": actor,
"actor_optimizer": actor_optimizer,
"discount": config.discount,
"device": config.device,
}
print("---------------------------------------")
print(f"Training BC, Env: {config.env}, Seed: {seed}")
print("---------------------------------------")
# Initialize policy
trainer = BC(**kwargs)
if config.load_model != "":
policy_file = Path(config.load_model)
trainer.load_state_dict(torch.load(policy_file))
actor = trainer.actor
wandb_init(asdict(config))
evaluations = []
for t in range(int(config.max_timesteps)):
batch = replay_buffer.sample(config.batch_size)
batch = [b.to(config.device) for b in batch]
log_dict = trainer.train(batch)
wandb.log(log_dict, step=trainer.total_it)
# Evaluate episode
if (t + 1) % config.eval_freq == 0:
print(f"Time steps: {t + 1}")
eval_scores = eval_actor(
env,
actor,
device=config.device,
n_episodes=config.n_episodes,
seed=config.seed,
)
eval_score = eval_scores.mean()
normalized_eval_score = env.get_normalized_score(eval_score) * 100.0
evaluations.append(normalized_eval_score)
print("---------------------------------------")
print(
f"Evaluation over {config.n_episodes} episodes: "
f"{eval_score:.3f} , D4RL score: {normalized_eval_score:.3f}"
)
print("---------------------------------------")
if config.checkpoints_path is not None:
torch.save(
trainer.state_dict(),
os.path.join(config.checkpoints_path, f"checkpoint_{t}.pt"),
)
wandb.log(
{"d4rl_normalized_score": normalized_eval_score},
step=trainer.total_it,
)
if __name__ == "__main__":
train()