-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtrain.py
More file actions
197 lines (164 loc) · 5.34 KB
/
train.py
File metadata and controls
197 lines (164 loc) · 5.34 KB
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
import torch
import torch.multiprocessing as mp
if mp.get_start_method(allow_none=True) is None:
mp.set_start_method('spawn', force=True) # or 'forkserver'
import argparse
import os
import time
import numpy as np
import common_args
import random
import pytorch_lightning as pl
from pytorch_lightning.strategies.ddp import DDPStrategy
from lightning.pytorch import loggers as pl_loggers
from dataset import Dataset
from net import Transformer
from utils import (
build_prices_data_filename,
build_prices_model_filename,
build_run_name
)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
def collate_fn(batch):
max_length = max(len(item['context_actions']) for item in batch)
padded_batch = {
'context_actions': [],
'context_rewards': [],
'optimal_actions': [],
'zeros': [],
}
for item in batch:
context_actions = item['context_actions']
context_rewards = item['context_rewards']
optimal_actions = item['optimal_actions']
zeros = item['zeros']
# Pad sequences
pad_length = max_length - len(context_actions)
action_pad = torch.zeros(pad_length, context_actions.shape[1]).to(device)
reward_pad = torch.zeros(pad_length, context_rewards.shape[1]).to(device)
padded_context_actions = torch.cat([context_actions, action_pad], dim=0)
padded_context_rewards = torch.cat([context_rewards, reward_pad], dim=0)
padded_batch['context_actions'].append(padded_context_actions)
padded_batch['context_rewards'].append(padded_context_rewards)
padded_batch['optimal_actions'].append(optimal_actions)
padded_batch['zeros'].append(zeros)
# Stack tensors
for key in padded_batch:
padded_batch[key] = torch.stack(padded_batch[key])
return padded_batch
if __name__ == '__main__':
parser = argparse.ArgumentParser()
common_args.add_run_args(parser)
common_args.add_train_args(parser)
args = vars(parser.parse_args())
env = args['env']
envs = args['envs']
n_samples = args['samples']
horizon = args['H']
dim = args['dim']
state_dim = dim
action_dim = dim
embd = args['embd']
head = args['head']
layer = args['layer']
lr = args['lr']
shuffle = args['shuffle']
dropout = args['dropout']
var = args['var']
cov = args['cov']
num_epochs = args['num_epochs']
seed = args['seed']
lin_d = args['lin_d']
# Set seeds
tmp_seed = seed
if seed == -1:
tmp_seed = 0
torch.manual_seed(tmp_seed)
if torch.cuda.is_available():
torch.cuda.manual_seed(tmp_seed)
torch.cuda.manual_seed_all(tmp_seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
np.random.seed(tmp_seed)
random.seed(tmp_seed)
run_config = {
'shuffle' : shuffle,
'n_samples': n_samples,
'H': horizon,
'dim': dim,
'var': var,
'cov': cov,
'type': 'uniform',
'store_gpu': True,
'truncate': True,
'state_dim': state_dim,
'action_dim': action_dim,
'layer': layer,
'embd': embd,
'head': head,
'dropout': dropout,
'lr': lr,
'seed': seed,
'envs': envs,
'test': False,
}
# dataset_config = {
# 'shuffle' : shuffle,
# 'action_dim': action_dim,
# 'n_samples': n_samples,
# 'horizon': horizon,
# 'dim': dim,
# 'var': var,
# 'cov': cov,
# 'type': 'uniform',
# 'store_gpu': True,
# 'truncate': True
# }
# model_config = {
# 'horizon': horizon,
# 'state_dim': state_dim,
# 'action_dim': action_dim,
# 'n_layer': n_layer,
# 'n_embd': n_embd,
# 'n_head': n_head,
# 'shuffle': shuffle,
# 'dropout': dropout,
# 'lr': lr,
# 'dim': dim,
# 'seed': seed,
# 'n_envs': n_envs,
# 'test': False,
# 'store_gpu': True,
# }
train_filename = build_prices_data_filename(
envs, run_config, mode=0)
test_filename = build_prices_data_filename(
envs, run_config, mode=1)
run_name = build_run_name(run_config)
model = Transformer(run_config).to(device)
params = {
'batch_size': 64,
'shuffle': shuffle,
}
# Build wandb run name
wandb_run_name = [
run_name,
time.strftime('%Y%m%d-%H%M%S')
]
wandb_run_name = " ".join(wandb_run_name)
logger = pl_loggers.WandbLogger(project='pricing_transformer', name=wandb_run_name)
trainer = pl.Trainer(
strategy=DDPStrategy(find_unused_parameters=True),
accelerator="gpu",
devices=[0], # List of GPU IDs to use for training
max_epochs=num_epochs,
logger=logger,
enable_checkpointing=False
)
# Load the dataset
train_dataset = Dataset(f'runs/{run_name}/'+train_filename, run_config)
test_dataset = Dataset(f'runs/{run_name}/'+test_filename, run_config)
train_loader = torch.utils.data.DataLoader(train_dataset, collate_fn=collate_fn, **params)
test_loader = torch.utils.data.DataLoader(test_dataset, collate_fn=collate_fn, **params)
# Train the model
trainer.fit(model, train_dataloaders=train_loader, val_dataloaders=test_loader)