-
Notifications
You must be signed in to change notification settings - Fork 2
/
HyperparameterTesting.py
159 lines (130 loc) · 5.77 KB
/
HyperparameterTesting.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
##############################################
# (c) Copyright 2018-2019 Kenza Tazi and Thomas Zhu
# This software is distributed under the terms of the GNU General Public
# Licence version 3 (GPLv3)
##############################################
from FFNtest import FFN
from tqdm import tqdm
import DataPreparation as dp
import tensorflow as tf
import ModelEvaluation as me
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
from scipy.interpolate import interp2d
import numpy as np
# This is a script to get some visulisation of unbias hyperparameter testing.
df = dp.PixelLoader('./SatelliteData/SLSTR/Pixels3')
tdata, vdata, ttruth, vtruth = df.dp.get_ffn_training_data(21, seed=2553149187)
'''
# for layers and neurons
epochs = [1]
neurons = [32, 16, 64, 128, 254, 512, 1024]
LRs = [1e-3] # , 1e-1, 1e-2, 1e-4]
hidden_layers = [2, 6, 10, 12, 14]
batch_size = [64] # 16, 32, 128]
dropout = [0.8] # , 0.2, 0.4, 0.6]
hyp_df = pd.DataFrame(columns=['epochs', 'neurons', 'hidden_layers', 'batch_size', 'LR', 'dropout', 'val_acc'])
for e in epochs:
for bs in batch_size:
for lr in tqdm(LRs):
for do in dropout:
for hl in tqdm(hidden_layers):
for n in neurons:
model = FFN(str(e) + '_' + str(bs) + '_' + str(lr) + '_' +
str(do) + '_' + str(hl) + '_' + str(n), 'TestNetwork', 21, LR=lr,
neuron_num=n, hidden_layers=hl, batch_size=bs,
epochs=e, dout=do)
model.Train(tdata, ttruth, vdata, vtruth)
val_acc = me.get_accuracy(model.model, vdata, vtruth)
hyp_df = hyp_df.append({'epochs': e, 'neurons': n,
'hidden_layers': hl, 'batch_size': bs,
'LR': lr, 'dropout': do, 'val_acc': val_acc},
ignore_index=True)
model.Save()
tf.reset_default_graph()
hyp_df.to_pickle('layers_neurons_test')
# for learning rate and dropout
epochs = [1]
neurons = [128]
LRs = [ 1e-1, 0.05, 1e-2, 0.005, 1e-3, 0.0005, 1e-4]
hidden_layers = [8]
batch_size = [64] # 16, 32, 128]
dropout = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]
hyp_df = pd.DataFrame(columns=['epochs', 'neurons', 'hidden_layers', 'batch_size', 'LR', 'dropout', 'val_acc'])
for e in epochs:
for bs in batch_size:
for lr in tqdm(LRs):
for do in dropout:
for hl in tqdm(hidden_layers):
for n in neurons:
model = FFN(str(e) + '_' + str(bs) + '_' + str(lr) + '_' +
str(do) + '_' + str(hl) + '_' + str(n), 'TestNetwork', 21, LR=lr,
neuron_num=n, hidden_layers=hl, batch_size=bs,
epochs=e, dout=do)
model.Train(tdata, ttruth, vdata, vtruth)
val_acc = me.get_accuracy(model.model, vdata, vtruth)
hyp_df = hyp_df.append({'epochs': e, 'neurons': n,
'hidden_layers': hl, 'batch_size': bs,
'LR': lr, 'dropout': do, 'val_acc': val_acc},
ignore_index=True)
model.Save()
tf.reset_default_graph()
'''
epochs = [1, 50, 100, 150, 200]
neurons = [128]
LRs = [1e-4] # [ 1e-1, 0.05, 1e-2, 0.005, 1e-3, 0.0005, 1e-4]
hidden_layers = [8]
batch_size = [16, 32, 64, 128, 256, 512, 1024]
dropout = [0.9] #[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]
hyp_df = pd.DataFrame(columns=['epochs', 'neurons', 'hidden_layers', 'batch_size', 'LR', 'dropout', 'val_acc'])
for e in epochs:
for bs in batch_size:
for lr in tqdm(LRs):
for do in dropout:
for hl in tqdm(hidden_layers):
for n in neurons:
model = FFN(str(e) + '_' + str(bs) + '_' + str(lr) + '_' +
str(do) + '_' + str(hl) + '_' + str(n), 'TestNetwork', 21, LR=lr,
neuron_num=n, hidden_layers=hl, batch_size=bs,
epochs=e, dout=do)
model.Train(tdata, ttruth, vdata, vtruth)
val_acc = me.get_accuracy(model.model, vdata, vtruth)
hyp_df = hyp_df.append({'epochs': e, 'neurons': n,
'hidden_layers': hl, 'batch_size': bs,
'LR': lr, 'dropout': do, 'val_acc': val_acc},
ignore_index=True)
model.Save()
tf.reset_default_graph()
hyp_df.to_pickle('bs_e_test.pkl')
'''
hyp_df = pd.read_pickle('lr_dropout_test.pkl')
# Fig 2
fig = plt.figure()
ax = fig.gca(projection='3d')
# Make data.
X = LRs
Y = dropout
XX, YY = np.meshgrid(X, Y)
# fined_hyp_df = hyp_df[hyp_df['neurons'] == X]
# refined_hyp_df = fined_hyp_df[fined_hyp_df['hidden_layers'] == Y]
Z = hyp_df['val_acc'].values
XX = XX
YY = YY[:3]
ZZ = Z.reshape(9, 7)
f = interp2d(X, Y, ZZ, kind='linear')
Xnew = np.geomspace(1e-4, 1e-1, 50)
Ynew = np.linspace(0.05, 1, 50)
XXnew, YYnew = np.meshgrid(Xnew, Ynew)
Znew = f(Xnew, Ynew)
# Plot the surface.
surf = ax.plot_surface(XXnew, YYnew, Znew, cmap=cm.coolwarm,
linewidth=0, antialiased=False)
ax.set_xlabel('Learning rate')
ax.set_ylabel('Dropout')
ax.set_zlabel('Accuracy')
# Add a color bar which maps values to colors.
fig.colorbar(surf)
plt.show()
'''