-
Notifications
You must be signed in to change notification settings - Fork 1
/
cup.py
172 lines (154 loc) · 6.17 KB
/
cup.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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use('Agg')
from tqdm import tqdm
import pandas as pd
import numpy as np
import datetime
import neural_network.activation_functions as activations
import neural_network.regularizers as regularizers
import neural_network.error_functions as errors
import neural_network.loss_functions as losses
import neural_network.optimizers as optimizers
import neural_network.neural_network as nn
# load data
dataset = pd.read_csv('datasets/cup/ML-CUP19-TR.csv', header=None)
dataset_test = pd.read_csv('datasets/cup/ML-CUP19-TS.csv', header=None)
training_set = dataset.iloc[:1300, :].values
test_set = dataset.iloc[1300:, :].values
blind_test_set = dataset_test.iloc[:, :].values
dataset = dataset.iloc[:, :].values
# model selection
# grid search
grid = nn.get_grid_search(
[0.8], # learning rates
[800], # epochs
[0], # momentum alphas
[0], # momentum betas (moving average)
[1e-07], # lambdas
[20], # hidden units
[50], # mini-batches
[5], # number of folds
[activations.Sigmoid()] # activation functions
)
now = datetime.datetime.now()
with tqdm(total=int(len(grid)), position=0, leave=True) as progress_bar:
for i, g in enumerate(grid):
folder = "{0}_{1}".format(now.strftime('%Y%m%d_%H%M%S'), i+1)
grid_tr_errors = []
grid_vl_errors = []
n_outputs = 2
# hyperparameters
lr = g["lr"]
epochs = g["epochs"]
alpha = g["alpha"]
beta = g["beta"]
n_hidden = g["nhidden"]
mb = g["mb"]
n_folds = g["nfolds"]
activation = g["activation"]
lmbda = g["lambda"]
# building the model
model = nn.Sequential(
error=errors.MeanEuclideanError(),
loss=losses.MeanSquaredError(),
regularizer=regularizers.L2(lmbda),
# optimizer=optimizers.SGD(lr, epochs, mb, alpha, beta)
# optimizer=optimizers.Nadam(lr, epochs, mb, alpha, lr_decay=True)
optimizer=optimizers.Adam(lr, epochs, mb, lr_decay=True)
)
model.add(nn.Dense(dim=(training_set.shape[1] - n_outputs, n_hidden), activation=activation))
model.add(nn.Dense(dim=(n_hidden, n_hidden), activation=activation))
model.add(nn.Dense(dim=(n_hidden, n_hidden), activation=activation))
model.add(nn.Dense(dim=(n_hidden, n_outputs), activation=activations.Linear(), is_output=True))
start_time = datetime.datetime.now()
# k-fold cross validation
for TR, VL in nn.k_fold_cross_validation(X=training_set, K=n_folds, shuffle=True):
tr_errors, vl_errors, _, _ = model.fit(TR, VL)
grid_tr_errors.append(tr_errors)
grid_vl_errors.append(vl_errors)
end_time = datetime.datetime.now()
time = end_time - start_time
# mean the i-th elements of the list of k-folds
tr_errors = [0] * epochs
vl_errors = [0] * epochs
for lst in grid_tr_errors:
for i, e in enumerate(lst):
tr_errors[i] += e
for lst in grid_vl_errors:
for i, e in enumerate(lst):
vl_errors[i] += e
tr_errors = [x/n_folds for x in tr_errors]
vl_errors = [x/n_folds for x in vl_errors]
_, MEE_inner_test_set = model.validate(test_set)
variance = np.var(grid_tr_errors)
# plot learning curve
learning_img, plt1 = plt.subplots()
plt1.plot(tr_errors)
plt1.plot(vl_errors)
plt1.set_title("Learning curve")
plt1.set_xlabel("Epochs")
plt1.set_ylabel("Error")
plt1.legend(['train', 'validation'], loc='upper right')
plt.close()
g["optimizer"] = type(model.optimizer).__name__
g["regularizer"] = type(model.regularizer).__name__
g["activation"] = type(activation).__name__
g["loss"] = type(model.loss).__name__
desc = str(g) \
+ "\nMEE TR: {0}".format(tr_errors[-1]) \
+ "\nMEE VL: {0}".format(vl_errors[-1]) \
+ "\nMEE TS (inner): {0}".format(MEE_inner_test_set) \
+ "\nVariance TR: {0}".format(variance) \
+ "\nTrained in {0} seconds".format(str(time.total_seconds()))
model.save(folder, desc, learning_img)
progress_bar.update(1)
# extract and order the models w.r.t MEE VL
import os
runs_dir = 'runs/'
models_mee = []
for folder in os.listdir(runs_dir):
file = open(os.path.join(runs_dir, folder, 'description'))
for i, line in enumerate(file):
if i == 2:
mee = float(line.split(': ')[1])
models_mee.append({'name': folder, 'mee': mee})
models_mee = sorted(models_mee, key=lambda i: i["mee"])
print(models_mee)
"""
# model assessment
n_outputs = 2
n_hidden = 20
activation = activations.Sigmoid()
model = nn.Sequential(
error=errors.MeanEuclideanError(),
loss=losses.MeanSquaredError(),
regularizer=regularizers.L2(lmbda=1e-07),
optimizer=optimizers.Adam(lr=0.8, epochs=800, mb=50, lr_decay=True)
#optimizer=optimizers.SGD(lr=0.09, epochs=500, mb=25, alpha=0.9, beta=0.9)
)
model.add(nn.Dense(dim=(dataset.shape[1] - n_outputs, n_hidden), activation=activation))
model.add(nn.Dense(dim=(n_hidden, n_hidden), activation=activation))
model.add(nn.Dense(dim=(n_hidden, n_hidden), activation=activation))
model.add(nn.Dense(dim=(n_hidden, n_outputs), activation=activations.Linear(), is_output=True))
"""
"""
tr_errors, _, _, _ = model.fit(dataset, dataset, verbose=True)
# plot learning curve
learning_img, plt1 = plt.subplots()
plt1.plot(tr_errors)
plt1.set_title("Model assessment")
plt1.set_xlabel("Epochs")
plt1.set_ylabel("Error")
plt1.legend(['dataset'], loc='upper right')
learning_img.show()
learning_img.savefig('learning_curve.png')
_, ts_error = model.validate(test_set)
print('Training error:', tr_errors[-1])
print('Test error:', ts_error)
model.save('final_model')
model = nn.Sequential().load('models/cup/20200302_205321_101/final_model.pkl')
model.predict(blind_test_set, save_csv=True)
"""