Add CrabNetSurrogateModel class to surrogate.py
Browse files- surrogate.py +44 -0
surrogate.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from joblib import load
|
2 |
+
import pandas as pd
|
3 |
+
import random
|
4 |
+
|
5 |
+
|
6 |
+
class CrabNetSurrogateModel(object):
|
7 |
+
def __init__(self):
|
8 |
+
self.models = load("surrogate_models.pkl")
|
9 |
+
|
10 |
+
def prepare_params_for_eval(self, raw_params):
|
11 |
+
raw_params["bias"] = int(raw_params["bias"])
|
12 |
+
raw_params["use_RobustL1"] = raw_params["criterion"] == "RobustL1"
|
13 |
+
raw_params.pop("criterion")
|
14 |
+
|
15 |
+
raw_params.pop("losscurve")
|
16 |
+
raw_params.pop("learningcurve")
|
17 |
+
|
18 |
+
# raw_params["train_frac"] = random.uniform(0.01, 1)
|
19 |
+
|
20 |
+
elem_prop = raw_params["elem_prop"]
|
21 |
+
raw_params["elem_prop_magpie"] = 0
|
22 |
+
raw_params["elem_prop_mat2vec"] = 0
|
23 |
+
raw_params["elem_prop_onehot"] = 0
|
24 |
+
raw_params[f"elem_prop_{elem_prop}"] = 1
|
25 |
+
raw_params.pop("elem_prop")
|
26 |
+
|
27 |
+
return raw_params
|
28 |
+
|
29 |
+
def surrogate_evaluate(self, params):
|
30 |
+
|
31 |
+
parameters = self.prepare_params_for_eval(params)
|
32 |
+
parameters = pd.DataFrame([parameters])
|
33 |
+
|
34 |
+
percentile = random.uniform(0, 1) # generate random percentile
|
35 |
+
|
36 |
+
# TODO: should percentile be different for each objective? (I guess depends on what is meant to be correlated vs. not)
|
37 |
+
mae = self.models["mae"].predict(parameters.assign(mae_rank=[percentile]))
|
38 |
+
rmse = self.models["rmse"].predict(parameters.assign(rmse_rank=[percentile]))
|
39 |
+
runtime = self.models["runtime"].predict(
|
40 |
+
parameters.assign(runtime_rank=[percentile])
|
41 |
+
)
|
42 |
+
model_size = self.models["model_size"].predict(parameters)
|
43 |
+
|
44 |
+
return mae, rmse, runtime, model_size
|