-
Notifications
You must be signed in to change notification settings - Fork 10
/
training.py
76 lines (56 loc) · 2.15 KB
/
training.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
from datetime import datetime
import torch
import numpy as np
import pytorch_lightning as pl
from pytorch_lightning import loggers as pl_loggers
from pytorch_lightning.callbacks.early_stopping import EarlyStopping
from pytorch_lightning.callbacks import ModelCheckpoint
from data import JaneStreetDataModule
from trend_net import TrendClassifier
from utility_net import UtilityMaximizer
def fit_model(model, percent_rows=None, num_rows=None, split='mixed10', batch_size=4096):
data = JaneStreetDataModule(model=model, split=split, batch_size=batch_size, num_rows=num_rows, percent_rows=percent_rows)
model_width = data.train_ds.X.shape[1]
print('model_width',model_width)
if model == TrendClassifier:
monitor = 'val_auc'
model = TrendClassifier(model_width)
filename = 'trend-'+split+'-{epoch}-{val_auc:.4f}-{val_u:.4f}'
dirpath='./weights/'
elif model == UtilityMaximizer:
monitor = 'val_u'
model = UtilityMaximizer(model_width)
filename = 'utility-'+split+'-{epoch}-{val_auc:.2f}-{val_u:.4f}'
dirpath='./weights/'
else:
raise NotImplementedError()
print('model:',model)
print('monitoring:',monitor)
print('time start:',datetime.now().strftime("%H:%M:%S"))
early_stop_callback = EarlyStopping(
monitor=monitor,
patience=7,
verbose=True,
mode='max'
)
checkpoint_callback = ModelCheckpoint(
dirpath=dirpath,
filename=filename,
save_top_k=1,
verbose=True,
monitor=monitor,
mode='max'
)
trainer = pl.Trainer( logger=pl_loggers.TensorBoardLogger('./logs/'),
gpus=1,
max_epochs=1000,
checkpoint_callback=checkpoint_callback,
callbacks=[early_stop_callback] )
torch.manual_seed(0)
np.random.seed(0)
trainer.fit(model, data)
print('time end:',datetime.now().strftime("%H:%M:%S"))
if __name__ == '__main__':
for model in TrendClassifier, UtilityMaximizer:
for i in np.arange(4):
fit_model(model, split='CV4'+str(i))