-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSTM08gpu_MLP_STM_corpus.py
331 lines (282 loc) · 11.9 KB
/
STM08gpu_MLP_STM_corpus.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Apr 26 21:50:55 2024
"""
import numpy as np
import pandas as pd
import keras
from keras import layers
import keras_tuner as kt
import datetime
import os
import tensorflow as tf
import sys
import gc
import scipy.io
import subprocess
from prepData import prepData_STM as prepData # if this does not work, uncomment the sections below
os.environ["KERAS_BACKEND"] = "tensorflow"
def auto_git_push(path):
subprocess.run(['git', 'add', path], check=True)
subprocess.run(['git', 'commit', '-m', 'auto_sync: '+path], check=True)
subprocess.run(['git', 'push'], check=True)
# %% build model
# n_feat = 2420
# n_target = 6
class hyperModel_drop(kt.HyperModel):
def build(self, hp):
# tf.keras.backend.clear_session()
gc.collect()
model = keras.Sequential()
model.add(keras.Input(shape=(n_feat,)))
# Tune the number of layers.
for i in range(hp.Int("num_layers", 1, 4)):
model.add(
layers.Dense(
# Tune number of units separately.
units=hp.Int(f"units_{i}", min_value=32, max_value=512, step=32),
activation=hp.Choice("activation", ["relu"]),
kernel_regularizer=keras.regularizers.L1(l1=hp.Float(f"L1_{i}", min_value=1e-12, max_value=1e-6, sampling="log"))
)
)
model.add(
layers.Dropout(
rate=hp.Float(f"drop_{i}", min_value=0, max_value=0.1, sampling="linear")
)
)
model.add(layers.Dense(n_target, activation="softmax"))
ROC_AUC = keras.metrics.AUC(
num_thresholds=200,
curve="ROC",
summation_method="interpolation",
name=None,
dtype=None,
thresholds=None,
multi_label=False, # only set to True when dealing with music genres
label_weights=None,
from_logits=False,
)
macroF1 = keras.metrics.F1Score(average="macro", threshold=None, name="macro_f1_score", dtype=None)
learning_rate = hp.Float("lr", min_value=1e-7, max_value=1e-4, sampling="log")
model.compile(
optimizer=keras.optimizers.Adam(
learning_rate=learning_rate,
gradient_accumulation_steps=8
),
loss="categorical_focal_crossentropy",
metrics=[ROC_AUC, macroF1],
)
return model
def fit(self, hp, model, dataset, validation_data=None, **kwargs):
return model.fit(
dataset,
shuffle=True,
validation_data=validation_data,
**kwargs,
)
class hyperModel_LN(kt.HyperModel):
def build(self, hp):
# tf.keras.backend.clear_session()
gc.collect()
model = keras.Sequential()
model.add(keras.Input(shape=(n_feat,)))
# Tune the number of layers.
for i in range(hp.Int("num_layers", 1, 4)):
model.add(
layers.Dense(
# Tune number of units separately.
units=hp.Int(f"units_{i}", min_value=32, max_value=512, step=32),
activation=hp.Choice("activation", ["relu"]),
kernel_regularizer=keras.regularizers.L1(l1=hp.Float(f"L1_{i}", min_value=1e-12, max_value=1e-6, sampling="log"))
)
)
model.add(layers.LayerNormalization())
model.add(layers.Dense(n_target, activation="softmax"))
ROC_AUC = keras.metrics.AUC(
num_thresholds=200,
curve="ROC",
summation_method="interpolation",
name=None,
dtype=None,
thresholds=None,
multi_label=False, # only set to True when dealing with music genres
label_weights=None,
from_logits=False,
)
macroF1 = keras.metrics.F1Score(average="macro", threshold=None, name="macro_f1_score", dtype=None)
learning_rate = hp.Float("lr", min_value=1e-7, max_value=1e-4, sampling="log")
model.compile(
optimizer=keras.optimizers.Adam(
learning_rate=learning_rate,
gradient_accumulation_steps=8
),
loss="categorical_focal_crossentropy",
metrics=[ROC_AUC, macroF1],
)
return model
def fit(self, hp, model, dataset, validation_data=None, **kwargs):
return model.fit(
dataset,
shuffle=True,
validation_data=validation_data,
**kwargs,
)
# %% Run the script
if __name__ == "__main__":
ablation_model = 'drop' # 'drop' or 'LN'
# % set the tuner
if sys.argv[1]=='0':
train_dataset, val_dataset, test_dataset, n_feat, n_target = prepData(n_pca=1024)
hm = hyperModel_drop()
directory = "model/STM/MLP_corpora_categories/PCA/Dropout/ROC-AUC"
objective="val_auc"
early_stop = "val_auc"
elif sys.argv[1]=='1':
train_dataset, val_dataset, test_dataset, n_feat, n_target = prepData(n_pca=1024)
hm = hyperModel_LN()
directory = "model/STM/MLP_corpora_categories/PCA/LayerNormalization/ROC-AUC"
objective="val_auc"
early_stop = "val_auc"
elif sys.argv[1]=='2':
train_dataset, val_dataset, test_dataset, n_feat, n_target = prepData(n_pca=1024)
hm = hyperModel_drop()
directory = "model/STM/MLP_corpora_categories/PCA/Dropout/macroF1"
objective = kt.Objective("val_macro_f1_score", direction="max")
early_stop = "val_f1_score"
elif sys.argv[1]=='3':
train_dataset, val_dataset, test_dataset, n_feat, n_target = prepData(n_pca=1024)
hm = hyperModel_LN()
directory = "model/STM/MLP_corpora_categories/PCA/LayerNormalization/macroF1"
objective = kt.Objective("val_macro_f1_score", direction="max")
early_stop = "val_f1_score"
elif sys.argv[1]=='4': # downsample nontonal speech
train_dataset, val_dataset, test_dataset, n_feat, n_target = prepData(ds_nontonal_speech=True, n_pca=1024)
hm = hyperModel_drop()
directory = "model/STM/MLP_corpora_categories/PCA/Dropout/ROC-AUC/downsample"
objective="val_auc"
early_stop = "val_auc"
elif sys.argv[1]=='5': # downsample nontonal speech
train_dataset, val_dataset, test_dataset, n_feat, n_target = prepData(ds_nontonal_speech=True, n_pca=1024)
hm = hyperModel_LN()
directory = "model/STM/MLP_corpora_categories/PCA/LayerNormalization/ROC-AUC/downsample"
objective="val_auc"
early_stop = "val_auc"
elif sys.argv[1]=='6': # downsample nontonal speech
train_dataset, val_dataset, test_dataset, n_feat, n_target = prepData(ds_nontonal_speech=True, n_pca=1024)
hm = hyperModel_drop()
directory = "model/STM/MLP_corpora_categories/PCA/Dropout/macroF1/downsample"
objective = kt.Objective("val_macro_f1_score", direction="max")
early_stop = "val_f1_score"
elif sys.argv[1]=='7': # downsample nontonal speech
train_dataset, val_dataset, test_dataset, n_feat, n_target = prepData(ds_nontonal_speech=True, n_pca=1024)
hm = hyperModel_LN()
directory = "model/STM/MLP_corpora_categories/PCA/LayerNormalization/macroF1/downsample"
objective = kt.Objective("val_macro_f1_score", direction="max")
early_stop = "val_f1_score"
elif 8<=int(sys.argv[1])<=56:
c_index = int(sys.argv[1])-8
n_xcut = 7
n_ycut = 7
cond_shape = (n_xcut, n_ycut)
total_models = np.prod(cond_shape)
# Calculate the indices for each dimension
i1 = (c_index % cond_shape[1])
c_index //= cond_shape[1]
i0 = c_index
ablation_params = {
'x_lowcutoff': None,
'x_highcutoff': i0,
'y_lowcutoff': None,
'y_highcutoff': i1,
}
train_dataset, val_dataset, test_dataset, n_feat, n_target = prepData(ablation_params = ablation_params, n_pca=None) # the low-pass will have a much lower number of features so PCA is not necessary
if ablation_model == 'drop':
hm = hyperModel_drop()
directory = "model/STM/MLP_corpora_categories/PCA/Dropout/macroF1/ablation/xhighcutoff"+str(i0)+"_yhighcutoff"+str(i1)
elif ablation_model == 'LN':
hm = hyperModel_LN()
directory = "model/STM/MLP_corpora_categories/PCA/LayerNormalization/macroF1/ablation/xhighcutoff"+str(i0)+"_yhighcutoff"+str(i1)
objective = kt.Objective("val_macro_f1_score", direction="max")
early_stop = "val_f1_score"
elif 57<=int(sys.argv[1])<=105:
c_index = int(sys.argv[1])-57
n_xcut = 7
n_ycut = 7
cond_shape = (n_xcut, n_ycut)
total_models = np.prod(cond_shape)
# Calculate the indices for each dimension
i1 = (c_index % cond_shape[1])
c_index //= cond_shape[1]
i0 = c_index
ablation_params = {
'x_lowcutoff': i0,
'x_highcutoff': None,
'y_lowcutoff': i1,
'y_highcutoff': None,
}
train_dataset, val_dataset, test_dataset, n_feat, n_target = prepData(ablation_params = ablation_params, n_pca=None)
if ablation_model == 'drop':
hm = hyperModel_drop()
directory = "model/STM/MLP_corpora_categories/PCA/Dropout/macroF1/ablation/xlowcutoff"+str(i0)+"_ylowcutoff"+str(i1)
elif ablation_model == 'LN':
hm = hyperModel_LN()
directory = "model/STM/MLP_corpora_categories/PCA/LayerNormalization/macroF1/ablation/xlowcutoff"+str(i0)+"_ylowcutoff"+str(i1)
objective = kt.Objective("val_macro_f1_score", direction="max")
early_stop = "val_f1_score"
else:
print("Index out of range of models. Nothing executed.")
sys.exit(0)
time_stamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M")
# Prepare a directory to store all the checkpoints.
checkpoint_dir = directory+"/ckpt/"+time_stamp
if not os.path.exists(checkpoint_dir):
os.makedirs(checkpoint_dir)
## Disable GPU (not enough usage)
# Create a MirroredStrategy.
# strategy = tf.distribute.MirroredStrategy()
# print('Number of devices: {}'.format(strategy.num_replicas_in_sync))
# with strategy.scope():
tuner = kt.BayesianOptimization(
hypermodel=hm,
objective=objective,
num_initial_points=10,
max_trials=40,
executions_per_trial=3,
seed=23,
max_retries_per_trial=0,
max_consecutive_failed_trials=3,
overwrite=True,
directory=directory,
project_name="MLP_"+time_stamp,
)
tuner.search_space_summary()
tuner.search(
train_dataset,
epochs=2,
validation_data=val_dataset,
# callbacks=[
# keras.callbacks.EarlyStopping(
# monitor=objective,
# mode="max",
# patience=5,
# verbose=1,
# ),
# keras.callbacks.ModelCheckpoint(
# filepath=checkpoint_dir + "/ckpt-{epoch}.keras",
# save_freq="epoch",
# ),
# ]
)
# retrain the best model
tf.keras.backend.clear_session()
gc.collect()
retrain_dataset = train_dataset.concatenate(val_dataset)
n_best_model = 3
best_hps = tuner.get_best_hyperparameters(n_best_model)
for n in range(n_best_model):
best_model = hm.build(best_hps[n])
best_model.fit(retrain_dataset)
saving_path = directory+"/"+"MLP_"+time_stamp+"/best_model"+str(n)+".keras"
best_model.save(saving_path)
# auto_git_push(directory)