-
Notifications
You must be signed in to change notification settings - Fork 2
/
hyperoptimization.py
132 lines (101 loc) · 4.43 KB
/
hyperoptimization.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
import numpy as np
import json
import pickle
import os
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.init as init
import torch.nn.functional as F
from torch.utils.data import Dataset, DataLoader
from hyperopt import fmin, tpe, hp, pyll, Trials
from z24_dataset import z24Dataset
from deep_model import Model
n_epochs = 50
batch_size = 1000
max_evals = 5
window_size = 200
class Initializer:
# to apply xavier_uniform:
#Initializer.initialize(model=net, initialization=init.xavier_uniform_, gain=init.calculate_gain('relu'))
# or maybe normal distribution:
#Initializer.initialize(model=net, initialization=init.normal_, mean=0, std=0.2)
def __init__(self):
pass
@staticmethod
def initialize(model, initialization, **kwargs):
def weights_init(m):
if isinstance(m, nn.Linear):
initialization(m.weight.data, **kwargs)
try:
initialization(m.bias.data)
except:
pass
model.apply(weights_init)
training_dataset = z24Dataset(mode='training', window_size=window_size, normalize=True)
training_dataloader = DataLoader(training_dataset, batch_size=batch_size, shuffle=True, num_workers=4)
validating_dataset = z24Dataset(mode='validating', window_size=window_size, normalize=True)
validating_dataloader = DataLoader(validating_dataset, batch_size=batch_size, shuffle=False, num_workers=4)
hyperspace = {'learning_rate': hp.uniform('learningrate', low=0.00005, high=0.001),
'dropout_p': hp.uniform('dropout', low=0.05, high=0.3),
'hidden_size1': hp.quniform('hidden_size1', low=256,high=512,q=1),
'hidden_size2': hp.quniform('hidden_size2', low=128,high=256,q=1),
'z_size': hp.quniform('z_size', low=64,high=128,q=1),
'weight_init': hp.choice('init', [init.xavier_normal_,
init.xavier_uniform_,
init.kaiming_normal_,
init.kaiming_uniform_,
init.orthogonal_])}
def f(hyperspace):
print(str(hyperspace))
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
#define model
model = Model(input_size=7*window_size+53,
output_size=7*window_size,
hidden_size1=int(hyperspace['hidden_size1']),
hidden_size2=int(hyperspace['hidden_size2']),
z_size=int(hyperspace['z_size']),
dropout_p=hyperspace['dropout_p']).to(device)
print(model)
Initializer.initialize(model=model, initialization=hyperspace['weight_init'])
optimizer = torch.optim.Adam(model.parameters(),
lr=hyperspace['learning_rate'],
betas=(0.9, 0.99),
eps=1e-08,
weight_decay=0,
amsgrad=True)
loss_criterion = torch.nn.MSELoss()
#training
train_loss = []
for epoch in range(n_epochs):
batchloss_train = []
for X_train, Y_train in training_dataloader:
X_train_tensor = X_train.float().to(device)
Y_train_tensor = Y_train.float().to(device)
optimizer.zero_grad() # zero the gradient buffer
predicted_Y = model(X_train_tensor)
batch_train_loss = loss_criterion(predicted_Y, Y_train_tensor)
batch_train_loss.backward()
optimizer.step()
batchloss_train.append(batch_train_loss.item())
train_loss.append(np.mean(batchloss_train))
print('Train loss: {}'.format(np.mean(batchloss_train)))
val_loss = []
with torch.no_grad():
for X_val, Y_val in validating_dataloader:
X_val_tensor = X_val.float().to(device)
Y_val_tensor = Y_val.float().to(device)
predicted_Y = model(X_val_tensor)
batch_val_loss = loss_criterion(predicted_Y, Y_val_tensor)
val_loss.append(batch_val_loss.item())
final_val_loss = np.mean(val_loss)
print('valiation loss: {}'.format(final_val_loss))
return final_val_loss
trials = Trials()
best = fmin(
fn=f,
space=hyperspace,
algo=tpe.suggest,
max_evals = max_evals)
print('###Best model###')
print(best)