-
Notifications
You must be signed in to change notification settings - Fork 0
/
trainer.py
71 lines (52 loc) · 2.42 KB
/
trainer.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
import random
import os
import numpy as np
from tqdm import tqdm
import torch
from config import prdc_config
from dataset import ReplayBuffer
from modules import DeterministicActor, EnsembledCritic
from prdc import PRDC
import wandb
class PRDCTrainer:
def __init__(self,
cfg=prdc_config) -> None:
self.cfg = cfg
self.device = cfg.device
seed = cfg.seed
random.seed(seed)
os.environ['PYTHONASSEED'] = str(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = True
self.state_dim = 17
self.action_dim = 6
self.batch_size = cfg.batch_size
actor = DeterministicActor(self.state_dim, self.action_dim, cfg.hidden_dim, edac_init=True).to(self.device)
critic = EnsembledCritic(self.state_dim, self.action_dim, cfg.hidden_dim, layer_norm=cfg.critic_ln).to(self.device)
self.buffer = ReplayBuffer(self.state_dim, self.action_dim, cfg.buffer_size)
self.buffer.from_json(cfg.dataset_name)
if cfg.normalize:
_, _ = self.buffer.normalize_states()
states: np.ndarray = self.buffer.states.numpy()
actions: np.ndarray = self.buffer.actions.numpy()
kdtree_data = np.hstack([cfg.beta * states, actions])
self.prdc = PRDC(cfg,
kdtree_data,
actor,
critic)
def fit(self):
print(f"Training starts on {self.cfg.device} 🚀")
with wandb.init(project=self.cfg.project, entity="zzmtsvv", group=self.cfg.group, name=self.cfg.name):
wandb.config.update({k: v for k, v in self.cfg.__dict__.items() if not k.startswith("__")})
for t in tqdm(range(self.cfg.max_timesteps), desc="PRDC steps"):
batch = self.buffer.sample(self.batch_size)
states, actions, rewards, next_states, dones = [x.to(self.device) for x in batch]
logging_dict = self.prdc.train(states,
actions,
rewards,
next_states,
dones)
wandb.log(logging_dict, step=self.prdc.total_iterations)