-
Notifications
You must be signed in to change notification settings - Fork 1
/
workbench.py
145 lines (118 loc) · 4.98 KB
/
workbench.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
import os
import tensorflow as tf # ! Tensorflow 2.0 !
import numpy as np
import time
import gc
from sklearn.metrics import f1_score, accuracy_score
from parameters import Parameters
from dataset import Dataset
from model import Model
from metric_writer import MetricWriter
# Just disables the warning, doesn't enable AVX/FMA
# https://stackoverflow.com/questions/47068709/your-cpu-supports-instructions-that-this-tensorflow-binary-was-not-compiled-to-u
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
class Workbench:
def __init__(self):
self.dataset = Dataset()
self.parameters = None
self.best_parameters = None
self.best_accuracy = None
self.model = None
self.metric_writer = MetricWriter()
if tf.test.is_gpu_available(cuda_only=True):
print("CUDA GPU acceleration was found !")
else:
print("Could not find any CUDA GPU acceleration.")
def random_search(self, iterations):
for i in range(iterations):
print("Random search : iteration", i+1, "/", iterations)
# Generate random hyperparameters
self.parameters = Parameters()
# Initialize the model
graph = Model(tuple(list(self.dataset.train_set_x.shape)[1:]), 1, self.parameters)
self.model = graph.build()
# Run the bloody thing
self.train_n_test()
# Clear up the mess
#del self.model
#gc.collect()
def testing_run(self):
print("Running test run...")
# Generate random hyperparameters
self.parameters = Parameters.from_file()
# Initialize the model
graph = Model(tuple(list(self.dataset.train_set_x.shape)[1:]), 1, self.parameters)
self.model = graph.build()
# Run the bloody thing
metrics = {}
tf.keras.callbacks.EarlyStopping(monitor='val_loss', mode='auto', min_delta=0.01, patience=10)
# Train
train_start = time.time()
history = self.model.fit(
self.dataset.train_set_x, self.dataset.train_set_y,
batch_size=self.parameters.batch_size,
epochs=self.parameters.epochs,
shuffle=True,
verbose=1
)
train_stop = time.time()
# Test
test_start = time.time()
predictions = self.model.predict(
self.dataset.test_set_x,
batch_size=self.parameters.batch_size,
verbose=1
)
test_end = time.time()
# Record important stuff
metrics["train_acc"] = history.history["accuracy"]
metrics["predictions"] = [x[0] for x in predictions]
metrics["test_acc"] = accuracy_score(self.dataset.test_set_y.flatten().tolist(),
[np.round(x) for x in metrics["predictions"]])
metrics["train_time"] = train_stop - train_start
metrics["test_time"] = test_end - test_start
metrics["f1_score"] = f1_score(self.dataset.test_set_y.flatten().tolist(),
[np.round(x) for x in metrics["predictions"]])
print(metrics)
# Clear up the mess
del self.model
gc.collect()
def train_n_test(self):
metrics = {}
tf.keras.callbacks.EarlyStopping(monitor='val_loss', mode='auto', min_delta=0.01, patience=10)
# Train
train_start = time.time()
history = self.model.fit(
self.dataset.train_set_x, self.dataset.train_set_y,
batch_size=self.parameters.batch_size,
epochs=self.parameters.epochs,
shuffle=True,
verbose=0
)
train_stop = time.time()
# Test
test_start = time.time()
predictions = self.model.predict(
self.dataset.val_set_x,
batch_size=self.parameters.batch_size,
verbose=0
)
test_end = time.time()
# Record important stuff
metrics["train_acc"] = history.history["accuracy"]
metrics["predictions"] = [x[0] for x in predictions]
metrics["test_acc"] = accuracy_score(self.dataset.val_set_y.flatten().tolist(), [np.round(x) for x in metrics["predictions"]])
print(" ---> achieved", metrics["test_acc"], "test accuracy")
metrics["train_time"] = train_stop - train_start
metrics["test_time"] = test_end - test_start
metrics["f1_score"] = f1_score(self.dataset.val_set_y.flatten().tolist(), [np.round(x) for x in metrics["predictions"]])
self.metric_writer.write(metrics, self.parameters)
# Compare hyperparameters to best performance
if self.best_accuracy is None or metrics["test_acc"] > self.best_accuracy:
self.best_accuracy = metrics["test_acc"]
self.best_parameters = self.parameters
# Save best hyperparameters to disk
self.best_parameters.to_file()
print(" ---> new accuracy record !")
else:
print(" ---> best:", self.best_accuracy)