-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluate_model.py
140 lines (102 loc) · 3.63 KB
/
evaluate_model.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
import numpy as np
import pandas as pd
import nn
import numpy.random as rng
import json
import sys
import tensorflow as tf
import rm
import itertools
from sklearn.model_selection import KFold
import null_model
import rbfm
from multiprocessing import Pool
def main(dataset_path, path, split_path, output_path, n_processes=10):
with open(path, 'r') as f:
cfg = json.load(f)
df = pd.read_csv(dataset_path)
X = np.array(df[['r1_temp', 'r2_temp', 'r1_pressure', 'r2_pressure']])
Y = np.array(df[cfg['output_cols']])
splits = np.load(split_path)
with Pool(n_processes) as p:
results = p.map(eval_model, [(cfg, X, Y, splits, s) for s in range(splits.shape[0])])
split_ae = np.array([r[1] for r in results])
model_cfgs = [r[0] for r in results]
np.savez(output_path, cfg=cfg, model_cfgs=model_cfgs, split_ae=split_ae)
def eval_model(packed):
cfg, X, Y, splits, s = packed
split = splits[s,:]
train_ix = split == 0
valid_ix = split == 1
test_ix = split == 2
# hyperparameter optimization on the training portion
combined_ix = train_ix | valid_ix
best_model_cfg = hyperparam_opt(cfg, X[combined_ix,:],Y[combined_ix,:])
Xtrain = X[train_ix,:]
Ytrain = Y[train_ix,:]
Xvalid = X[valid_ix,:]
Yvalid = Y[valid_ix,:]
Xtest = X[test_ix,:]
Ytest = Y[test_ix,:]
if cfg['model']['module'] == 'nn':
model = nn.FeedforwardNN(best_model_cfg)
elif cfg['model']['module'] == 'rm':
model = rm.RegressionModel(best_model_cfg)
elif cfg['model']['module'] == 'null':
model = null_model.NullModel(best_model_cfg)
elif cfg['model']['module'] == 'rbfm':
model = rbfm.RbfModel(best_model_cfg)
model.train(Xtrain, Ytrain, Xvalid, Yvalid)
yhat = model.predict(Xtest)
ae = np.abs(yhat - Ytest)
return (best_model_cfg, ae)
def hyperparam_opt(cfg, X, Y):
P_VALID = 0.2
cfgs = get_cfg_combinations(cfg)
if len(cfgs) == 1:
return cfgs[0]
module = cfg['model']['module']
kf = KFold(n_splits=cfg['folds'], shuffle=True)
for train_index, test_index in kf.split(X):
rng.shuffle(train_index)
n_valid = int(P_VALID * len(train_index))
valid_index = train_index[:n_valid]
train_index = train_index[n_valid:]
Xtrain = X[train_index,:]
Ytrain = Y[train_index,:]
Xvalid = X[valid_index,:]
Yvalid = Y[valid_index,:]
Xtest = X[test_index,:]
Ytest = Y[test_index,:]
for cfg in cfgs:
if module == 'nn':
model = nn.FeedforwardNN(cfg)
elif module == 'rm':
model = rm.RegressionModel(cfg)
# train
model.train(Xtrain, Ytrain, Xvalid, Yvalid)
# test
loss = model.evaluate(Xtest, Ytest)
cfg['results'].append(float(loss))
print("[%s] Loss: %f" % (cfg['comb'],loss))
best_cfg = min(cfgs, key=lambda c: np.mean(c['results']))
#print(best_cfg['comb'])
print("Best: %s" % (best_cfg['comb'],))
return best_cfg
def get_cfg_combinations(cfg):
keys = list(cfg['hyperparams'].keys())
search_space = [cfg['hyperparams'][k] for k in keys]
cfgs = []
base_cfg = cfg['model']
for comb in itertools.product(*search_space):
new_cfg = dict(base_cfg)
new_cfg.update({ k: v for k,v in zip(keys, comb ) })
new_cfg['results'] = []
new_cfg['comb'] = comb
cfgs.append(new_cfg)
return cfgs
if __name__ == "__main__":
path = sys.argv[1]
split_path = sys.argv[2]
output_path = sys.argv[3]
main(path, split_path, output_path)