-
Notifications
You must be signed in to change notification settings - Fork 0
/
sklearn_krr_mlp.py
445 lines (364 loc) · 16.9 KB
/
sklearn_krr_mlp.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
import numpy as np
from scipy.sparse import load_npz
from sklearn import datasets, linear_model,metrics
from sklearn.kernel_ridge import KernelRidge
from sklearn.preprocessing import StandardScaler
from sklearn.neural_network import MLPRegressor
from sklearn.model_selection import GridSearchCV, train_test_split
import time, sys
import argparse
### DEFINE and INPUT ###
def load_data(x_datafile, y_datafile, ids, testfiles):
# check if matrix is sparse by file ending
if "npz" in descfile:
is_sparse = True
else:
is_sparse = False
ids_train = None
ids_test = None
x_test = None
y_test = None
# Load all the data
# training and test is split by ID
if ids:
ids_train = np.load(ids[0])
ids_test = np.load(ids[1])
# training and test is split by file
elif testfiles:
if is_sparse:
x_test = load_npz(testfiles[0])
is_mean = False
else:
x_test = np.load(testfiles[0])
is_mean = True
y_test = np.load(testfiles[1])
# discard other columns except first
if len(y_test.shape) > 1:
y_test = y_test[:, 0]
# create pythonic ids
ids_test = np.arange(len(y_test))
if is_sparse:
x_data = load_npz(x_datafile)
is_mean = False
else:
x_data = np.load(x_datafile)
is_mean = True
y_data = np.load(y_datafile)
# discard other columns except first
if len(y_data.shape) > 1:
y_data = y_data[:, 0]
# create pythonic ids
ids = np.arange(len(y_data))
return x_data, y_data, ids, is_mean, is_sparse, ids_train, ids_test, x_test, y_test
def split_data(x_data, y_data, ids_data, sample_size, is_mean, is_sparse, ids, ids_train, ids_test):
# split set
if ids:
if is_sparse:
x_data = x_data.tolil()
x_train = x_data[ids_train]
x_test = x_data[ids_test]
y_train = y_data[ids_train]
y_test = y_data[ids_test]
if is_sparse:
x_train = x_train.tocsr()
x_test = x_test.tocsr()
else:
x_train, x_test, y_train, y_test, ids_train, ids_test = train_test_split(x_data, y_data, ids_data, test_size = 1 - sample_size)
return x_train, x_test, y_train, y_test, ids_train, ids_test
def scale_data(is_mean, x_train, x_test):
# Scale
#scaler = StandardScaler(with_mean=is_mean)
# fit only on training data
#scaler.fit(x_train)
#x_train = scaler.transform(x_train)
# apply same transformation to test data
#x_test = scaler.transform(x_test)
return x_train, x_test
def load_split_scale_data(x_datafile, y_datafile, ids, testfiles,
sample_size):
# load
x_data, y_data, ids_data, is_mean, is_sparse, ids_train, ids_test, x_test, y_test = load_data(
x_datafile, y_datafile, ids, testfiles)
# split
if testfiles:
x_train = x_data
y_train = y_data
x_test = x_test
y_test = y_test
ids_train = ids_data
ids_test = ids_test
else:
x_train, x_test, y_train, y_test, ids_train, ids_test = split_data(
x_data, y_data, ids_data, sample_size, is_mean, is_sparse, ids, ids_train, ids_test)
# scale
x_train, x_test = scale_data(is_mean, x_train, x_test)
return x_train, x_test, y_train, y_test, ids_train, ids_test
def predict_and_error(learner, x_test, x_train, y_test):
y_pred = learner.predict(x_test)
# run also on training set
train_y_pred = learner.predict(x_train)
# errors
mae = np.absolute(y_pred - y_test)
mse = mae ** 2
mae = np.mean(mae)
mse = np.mean(mse)
return mae, mse, y_pred, train_y_pred, learner
def write_output(learner, sample_size, ml_method, mae, mse, runtype, ids_test, y_test, y_pred, ids_train, y_train, train_y_pred):
### OUTPUT ###
# y_test vs y_predict
y_tmp = np.array([ids_test, y_test, y_pred])
y_compare = np.transpose(y_tmp)
np.savetxt(ml_method + str("_") + runtype + "_size" + str(sample_size) + ".predictions", y_compare,
header = "###ids_test y_test y_pred")
# also y_train vs. y_pred_train
y_tmp = np.array([ids_train, y_train, train_y_pred])
y_compare = np.transpose(y_tmp)
np.savetxt(ml_method + str("_") + runtype + "_size" + str(sample_size) + ".trainset_predictions", y_compare,
header = "###ids_train y_train train_y_pred")
with open(ml_method + str("_") + runtype + "_size" + str(sample_size) + ".out", "w") as f:
f.write("MAE " + str(mae) + "\n")
f.write("MSE " + str(mse) + "\n")
f.write("\n")
if runtype == "param":
f.write("Best parameters of " + ml_method + ": \n")
f.write(str(learner.best_params_) + "\n")
f.write("Errors of best parameters: \n")
f.write("Grid scores on validation set:" + "\n")
f.write("\n")
means = learner.cv_results_['mean_test_score']
stds = learner.cv_results_['std_test_score']
for mean, std, params in zip(means, stds, learner.cv_results_['params']):
f.write("%0.4f (+/-%0.04f) for %r"
% (mean, std * 2, params) + "\n")
f.write("\n")
f.write("Grid scores on train set:" + "\n")
f.write("\n")
means = learner.cv_results_['mean_train_score']
stds = learner.cv_results_['std_train_score']
for mean, std, params in zip(means, stds, learner.cv_results_['params']):
f.write("%0.4f (+/-%0.04f) for %r"
% (mean, std * 2, params) + "\n")
return None
def ml_param_scan(x_datafile, y_datafile, ids, testfiles,
alpha_list= np.logspace(-1, -8, 8),
gamma_list = np.logspace(-2, -10, 9), kernel_list = ['rbf'],
layer_list = [(40,40,40)], learning_rate_list = [0.001],
sample_size=0.1, ml_method = "krr"):
print('model ' + ml_method)
# load, split and scale data
x_train, x_test, y_train, y_test, ids_train, ids_test = load_split_scale_data(x_datafile, y_datafile, ids, testfiles,
sample_size)
if ml_method == "krr":
# Create kernel linear ridge regression object
learner = GridSearchCV(KernelRidge(kernel='rbf'), n_jobs = 8, cv=5,
param_grid={"alpha": alpha_list, "gamma": gamma_list,
"kernel": kernel_list}, scoring = 'neg_mean_absolute_error')
elif ml_method == "mlp":
# Create Multi-Layer Perceptron object
learner = GridSearchCV(MLPRegressor(hidden_layer_sizes=(40,40,40), max_iter=1600,
alpha= 0.001, learning_rate_init= 0.001), n_jobs = 8, cv=5,
param_grid={"alpha": alpha_list, "learning_rate_init": learning_rate_list,
"hidden_layer_sizes": layer_list}, scoring = 'neg_mean_absolute_error')
else:
print("ML method unknown. Exiting.")
exit(1)
t_ml0 = time.time()
learner.fit(x_train, y_train)
t_ml1 = time.time()
print("ml time", str(t_ml1 - t_ml0))
# getting best parameters
learner_best = learner.best_estimator_
mae, mse, y_pred, train_y_pred, learner_best = predict_and_error(learner_best, x_test, x_train, y_test)
### OUTPUT ###
write_output(learner, sample_size, ml_method, mae, mse, "param", ids_test, y_test, y_pred, ids_train, y_train, train_y_pred)
return learner.best_params_
def ml_run(x_datafile, y_datafile, ids, testfiles,
alpha0=1, gamma0=1, kernel0 = 'rbf', learning_rate_init0 = 0.001,
hidden_layer_sizes0=(80, 80, 80),
sample_size=0.1, ml_method = "krr"):
print('model ' + ml_method)
# load, split and scale data
x_train, x_test, y_train, y_test, ids_train, ids_test = load_split_scale_data(x_datafile, y_datafile, ids, testfiles,
sample_size)
if ml_method == "krr":
# Create kernel linear ridge regression object
learner = KernelRidge(alpha = alpha0, coef0=1, degree=3,
gamma= gamma0, kernel = kernel0, kernel_params=None)
elif ml_method == "mlp":
# Create Multi-Layer Perceptron object
learner = MLPRegressor(hidden_layer_sizes=hidden_layer_sizes0, max_iter=1600,
alpha= alpha0, learning_rate_init= learning_rate_init0)
else:
print("ML method unknown. Exiting.")
exit(1)
t_ml0 = time.time()
learner.fit(x_train, y_train)
t_ml1 = time.time()
print("ml time", str(t_ml1 - t_ml0))
mae, mse, y_pred, train_y_pred, learner = predict_and_error(learner, x_test, x_train, y_test)
### OUTPUT ###
write_output(learner, sample_size, ml_method, mae, mse, "run", ids_test, y_test, y_pred, ids_train, y_train, train_y_pred)
return None
def ml_size_scan(x_datafile, y_datafile, ids, testfiles,
alpha0=1, gamma0=1,
kernel0 = 'rbf', learning_rate_init0 = 0.001,
hidden_layer_sizes0=(80, 80, 80),
sample_size_list = [0.005, 0.01, 0.03, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9],
ml_method = "krr"):
max_sample_size = max(sample_size_list)
ax_train, x_test, ay_train, y_test, aids_train, ids_test = load_split_scale_data(x_datafile, y_datafile, ids, testfiles,
max_sample_size)
for sample_size in sample_size_list:
ratio = float(sample_size) / float(max_sample_size)
if ratio > 0.999:
x_train = ax_train
y_train = ay_train
ids_train = aids_train
else:
# reduce set
x_dump, x_train, y_dump, y_train, ids_dump, ids_train = train_test_split(ax_train, ay_train, aids_train, test_size = ratio,)
if ml_method == "krr":
# Create kernel linear ridge regression object
learner = KernelRidge(alpha = alpha0, coef0=1, degree=3,
gamma= gamma0, kernel = kernel0, kernel_params=None)
elif ml_method == "mlp":
# Create Multi-Layer Perceptron object
learner = MLPRegressor(hidden_layer_sizes=hidden_layer_sizes0, max_iter=1600,
alpha= alpha0, learning_rate_init= learning_rate_init0)
else:
print("ML method unknown. Exiting.")
exit(1)
t_ml0 = time.time()
learner.fit(x_train, y_train)
t_ml1 = time.time()
print("ml time", str(t_ml1 - t_ml0))
mae, mse, y_pred, train_y_pred, learner = predict_and_error(learner, x_test, x_train, y_test)
### OUTPUT ###
write_output(learner, sample_size, ml_method, mae, mse, "size", ids_test, y_test, y_pred, ids_train, y_train, train_y_pred)
return None
def ml_param_size_scan(x_datafile, y_datafile, ids, testfiles,
alpha_list= np.logspace(-1, -8, 8),
gamma_list = np.logspace(-2, -10, 9), kernel_list = ['rbf'],
layer_list = [(40,40,40)], learning_rate_list = [0.001],
sample_size_list = [0.005, 0.01, 0.03, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9],
ml_method = "krr", paramset_size=0.1):
print('model ' + ml_method)
max_sample_size = max(sample_size_list)
# load, split and scale data
ax_train, ax_test, ay_train, ay_test, aids_train, aids_test = load_split_scale_data(x_datafile, y_datafile, ids, testfiles,
max_sample_size)
# search for optimal learner parameters
# reduce set
x_train, x_test, y_train, y_test, ids_train, ids_test = train_test_split(ax_train, ay_train, aids_train, test_size = 1 - paramset_size,)
if ml_method == "krr":
# Create kernel linear ridge regression object
learner = GridSearchCV(KernelRidge(kernel='rbf'), n_jobs = 8, cv=5,
param_grid={"alpha": alpha_list, "gamma": gamma_list,
"kernel": kernel_list}, scoring = 'neg_mean_absolute_error')
elif ml_method == "mlp":
# Create Multi-Layer Perceptron object
learner = GridSearchCV(MLPRegressor(hidden_layer_sizes=(40,40,40), max_iter=1600,
alpha= 0.001, learning_rate_init= 0.001), n_jobs = 8, cv=5,
param_grid={"alpha": alpha_list, "learning_rate_init": learning_rate_list,
"hidden_layer_sizes": layer_list}, scoring = 'neg_mean_absolute_error')
else:
print("ML method unknown. Exiting.")
exit(1)
t_ml0 = time.time()
learner.fit(x_train, y_train)
t_ml1 = time.time()
print("ml time", str(t_ml1 - t_ml0))
# getting best parameters
learner_best = learner.best_estimator_
mae, mse, y_pred, train_y_pred, learner_best = predict_and_error(learner_best, x_test, x_train, y_test)
### OUTPUT ###
write_output(learner, max_sample_size * paramset_size, ml_method, mae, mse, "param", ids_test, y_test, y_pred, ids_train, y_train, train_y_pred)
# use above found best parameters
x_test = ax_test
y_test = ay_test
ids_test = aids_test
paramlearner = learner
for sample_size in sample_size_list:
ratio = float(sample_size) / float(max_sample_size)
if ratio > 0.999:
x_train = ax_train
y_train = ay_train
ids_train = aids_train
else:
# reduce set ("test set" is the part of the training set that is used)
x_dump, x_train, y_dump, y_train, ids_dump, ids_train = train_test_split(ax_train, ay_train, aids_train, test_size = ratio,)
if ml_method == "krr":
# Create kernel linear ridge regression object
alpha0 = paramlearner.best_params_["alpha"]
gamma0 = paramlearner.best_params_["gamma"]
kernel0 = paramlearner.best_params_["kernel"]
learner = KernelRidge(alpha = alpha0, coef0=1, degree=3,
gamma= gamma0, kernel = kernel0, kernel_params=None)
elif ml_method == "mlp":
# Create Multi-Layer Perceptron object
hidden_layer_sizes0 = paramlearner.best_params_["hidden_layer_sizes"]
alpha0 = paramlearner.best_params_["alpha"]
learning_rate_init0 = paramlearner.best_params_["learning_rate_init"]
learner = MLPRegressor(hidden_layer_sizes=hidden_layer_sizes0, max_iter=1600,
alpha= alpha0, learning_rate_init= learning_rate_init0)
else:
print("ML method unknown. Exiting.")
exit(1)
t_ml0 = time.time()
learner.fit(x_train, y_train)
t_ml1 = time.time()
print("ml time", str(t_ml1 - t_ml0))
mae, mse, y_pred, train_y_pred, learner = predict_and_error(learner, x_test, x_train, y_test)
### OUTPUT ###
write_output(learner, sample_size, ml_method, mae, mse, "psize", ids_test, y_test, y_pred, ids_train, y_train, train_y_pred)
return None
### INPUT ###
parser = argparse.ArgumentParser(description='Process runtype and filenames.')
parser.add_argument(dest='positionargs', metavar='cla', type=str, nargs='+',
help='[Runtype] [descriptor] [predictor] [krr or mlp]')
parser.add_argument('--ids', dest='ids', nargs=2, help='path to numpy arrays with indices to use as [training set] [test set]')
parser.add_argument('--testfiles', dest='testfiles', nargs=2, help='path to test set features and labels. features and labels for training set are given by positional argument')
args = parser.parse_args()
print("Arguments passed:")
print(args.positionargs)
if args.ids:
print('ids for training set', args.ids[0])
print('ids for test set', args.ids[1])
if args.testfiles:
print('optional test set files', args.testfiles[0], args.testfiles[1])
runtype = args.positionargs[0]
descfile = args.positionargs[1]
predfile = args.positionargs[2]
ML_METHOD = args.positionargs[3]
ids = args.ids
testfiles = args.testfiles
### PROCESS ###
if runtype == "param":
ml_param_scan(descfile, predfile, ids, testfiles,
alpha_list= np.logspace(-1, -9, 9),
gamma_list = np.logspace(-1, -9, 9), kernel_list = ['rbf'],
layer_list = [(40,40,40)], learning_rate_list = [0.001],
sample_size=0.1, ml_method = ML_METHOD)
elif runtype == "run":
ml_run(descfile, predfile, ids, testfiles,
alpha0=1e-4, gamma0=1e-03, kernel0 = 'rbf',
learning_rate_init0 = 0.001, hidden_layer_sizes0=(80, 80, 80),
sample_size=0.1, ml_method = ML_METHOD)
elif runtype == "size":
ml_size_scan(descfile, predfile, ids, testfiles,
alpha0=1e-9, gamma0=1e-10, kernel0 = 'rbf',
learning_rate_init0 = 0.001, hidden_layer_sizes0=(80, 80, 80),
sample_size_list = [0.005, 0.01, 0.03, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9],
ml_method = ML_METHOD)
elif runtype == "psize":
ml_param_size_scan(descfile, predfile, ids, testfiles,
alpha_list= np.logspace(-1, -8, 8),
gamma_list = np.logspace(-2, -10, 9), kernel_list = ['rbf'],
layer_list = [(40,40,40)], learning_rate_list = [0.001],
sample_size_list = [0.005, 0.01, 0.03, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9,],
ml_method = ML_METHOD, paramset_size=0.11)
else:
print("First argument not understood:")
print("Usage: python3 SCRIPTNAME [param, run, size or psize] [features] [labels] [krr or mlp] ")
exit(1)
### OUTPUT ###