-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbc_trainer.py
133 lines (105 loc) · 4.82 KB
/
bc_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
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
import torch
from torch.nn import MSELoss, CrossEntropyLoss
from qtransformer import QTransformer
from sequence_dataset import SequenceDataset
from torch.utils.data import DataLoader
import hydra
from omegaconf import DictConfig, OmegaConf
import numpy as np
from d4rl_evaluator import load_d4rl_env, batched_eval
from util import soft_update
from schedulers import get_cosine_schedule_with_warmup
from collections import deque
from wandb_logger import WandbLogger
import dotenv
import os
def norm_rewards(r, R_min, R_max):
return (r - R_min) / (R_max - R_min)
@hydra.main(version_base=None, config_path="conf", config_name="config")
def train(cfg : DictConfig) -> None:
dotenv.load_dotenv()
model_config = cfg['model']
seq_len = model_config['seq_len']
dataset_file = cfg['dataset']
train_config = cfg['train']
batch_size = train_config['batch_size']
env_config = cfg['env']
env_name = env_config['id']
action_dim = env_config['action_dim']
action_bins = model_config['action_bins']
use_dueling_head = model_config['dueling']
total_steps = train_config['total_steps']
weight_decay = train_config['weight_decay']
discrete_actions = env_config['discrete_actions']
state_dim = env_config['state_dim']
hidden_dim = model_config['hidden_dim']
gamma = cfg['gamma']
grad_norm = train_config['grad_norm']
seed = train_config['seed'] if 'seed' in train_config else 0
model_folder = train_config['model_folder']
a_min = env_config['action_min']
a_max = env_config['action_max']
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
if not os.path.exists(model_folder):
os.mkdir(model_folder)
print('device: ', device)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
offline_env, data = load_d4rl_env(env_name)
dataset = SequenceDataset.from_d4rl(data, seq_len, action_bins, gamma)
dataloader = DataLoader(dataset, batch_size=batch_size, shuffle=True)
if discrete_actions:
action_transform = lambda x: x[0]
else:
action_transform = lambda x: (x/action_bins) * (a_max - a_min) + a_min
#env = SequenceEnvironmentWrapper(offline_env, num_stack_frames=seq_len, action_dim=action_dim, action_transform=action_transform)
model = QTransformer(state_dim, action_dim, hidden_dim, action_bins, seq_len, dueling=use_dueling_head, device=device)
model.to(device)
orig_model = model
model = torch.compile(model)
optimizer = torch.optim.AdamW(model.parameters(), lr=train_config['lr'], weight_decay=weight_decay)
scheduler = get_cosine_schedule_with_warmup(optimizer, int(total_steps*0.1), total_steps)
loss = CrossEntropyLoss()
best_score = -9999
log_loss_steps = 50
eval_steps = 1000
i = 0
loss_list = deque(maxlen=50)
td_loss_list = deque(maxlen=50)
reg_loss_list = deque(maxlen=50)
eval_episodes=100
cfg['trainer'] = "BC"
logger = WandbLogger(os.environ['WANDB_ENTITY'], os.environ['WANDB_PROJECT'], cfg)
print(OmegaConf.to_yaml(cfg))
# print(batched_eval(env_name, model,eval_episodes, num_stack_frames=seq_len, action_dim=action_dim,action_transform=action_transform))
while i < total_steps:
for batch in dataloader:
states, actions, rewards, returns, terminal, timesteps = batch
states = states.float().to(device)
actions = torch.reshape(actions, (actions.shape[0], seq_len+1, -1)).int().to(device)
timesteps = timesteps.int().to(device)
logits = model(states[:, :-1], actions[:,-2], timesteps[:,:-1])
err = loss(logits, actions[:, -2])
optimizer.zero_grad()
err.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), grad_norm)
loss_list.append(err.item())
optimizer.step()
scheduler.step()
if (i+1) % log_loss_steps == 0:
logger.log({"train_loss": np.mean(loss_list)})
if (i+1) % eval_steps == 0:
model.eval()
score = batched_eval(env_name, model, eval_episodes, num_stack_frames=seq_len, action_dim=action_dim,action_transform=action_transform)
logger.log({"eval_score": offline_env.get_normalized_score(score)})
if score > best_score:
torch.save({'model_state': orig_model.state_dict()}, os.path.join(model_folder, 'best.pt'))
best_score = score
model.train()
i += 1
model.eval()
score = batched_eval(env_name, model, eval_episodes, num_stack_frames=seq_len, action_dim=action_dim,action_transform=action_transform)
logger.log({"eval_score": offline_env.get_normalized_score(score)})
torch.save({'model_state': orig_model.state_dict()}, os.path.join(model_folder,'final.pt'))
if __name__ == "__main__":
train()