Spaces:
Sleeping
Sleeping
Commit
·
0a3d3e9
1
Parent(s):
03ba6dc
Fix hyperparameter optimization script
Browse files- benchmarks/hyperparamopt.py +135 -49
benchmarks/hyperparamopt.py
CHANGED
|
@@ -5,22 +5,25 @@ import pickle as pkl
|
|
| 5 |
from pysr import PySRRegressor
|
| 6 |
import hyperopt
|
| 7 |
from hyperopt import hp, fmin, tpe, Trials
|
|
|
|
| 8 |
|
| 9 |
# Change the following code to your file
|
| 10 |
################################################################################
|
| 11 |
TRIALS_FOLDER = "trials"
|
| 12 |
NUMBER_TRIALS_PER_RUN = 1
|
| 13 |
-
|
| 14 |
|
| 15 |
# Test run to compile everything:
|
| 16 |
binary_operators = ["*", "/", "+", "-"]
|
| 17 |
unary_operators = ["sin", "cos", "exp", "log"]
|
| 18 |
julia_project = None
|
|
|
|
| 19 |
model = PySRRegressor(
|
| 20 |
binary_operators=binary_operators,
|
| 21 |
unary_operators=unary_operators,
|
| 22 |
timeout_in_seconds=30,
|
| 23 |
julia_project=julia_project,
|
|
|
|
| 24 |
)
|
| 25 |
model.fit(np.random.randn(100, 3), np.random.randn(100))
|
| 26 |
|
|
@@ -56,40 +59,54 @@ def run_trial(args):
|
|
| 56 |
if invalid:
|
| 57 |
return dict(status="fail", loss=float("inf"))
|
| 58 |
|
| 59 |
-
args["timeout_in_seconds"] =
|
| 60 |
args["julia_project"] = julia_project
|
| 61 |
-
args["procs"] =
|
|
|
|
|
|
|
| 62 |
|
| 63 |
# Create the dataset:
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
y = np.cos(2.3 * X[:, 0]) * np.sin(2.3 * X[:, 0] * X[:, 1] * X[:, 2])
|
| 67 |
|
| 68 |
# Old datasets:
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
|
|
|
| 84 |
|
| 85 |
-
|
| 86 |
-
cur_loss = float(model.get_best()["loss"])
|
| 87 |
-
losses.append(cur_loss)
|
| 88 |
|
| 89 |
-
loss = np.
|
| 90 |
print(f"Finished with {loss}", str(args))
|
| 91 |
|
| 92 |
-
return
|
| 93 |
|
| 94 |
|
| 95 |
space = dict(
|
|
@@ -163,6 +180,61 @@ space = dict(
|
|
| 163 |
tournament_selection_p=hp.uniform("tournament_selection_p", 0.0, 1.0),
|
| 164 |
)
|
| 165 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
################################################################################
|
| 167 |
|
| 168 |
|
|
@@ -195,7 +267,10 @@ def merge_trials(trials1, trials2_slice):
|
|
| 195 |
|
| 196 |
|
| 197 |
loaded_fnames = []
|
| 198 |
-
trials =
|
|
|
|
|
|
|
|
|
|
| 199 |
# Run new hyperparameter trials until killed
|
| 200 |
while True:
|
| 201 |
np.random.seed()
|
|
@@ -203,39 +278,48 @@ while True:
|
|
| 203 |
# Load up all runs:
|
| 204 |
import glob
|
| 205 |
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
trials_obj = pkl.load(open(fname, "rb"))
|
| 212 |
-
n_trials = trials_obj["n"]
|
| 213 |
-
trials_obj = trials_obj["trials"]
|
| 214 |
-
if len(loaded_fnames) == 0:
|
| 215 |
-
trials = trials_obj
|
| 216 |
-
else:
|
| 217 |
-
print("Merging trials")
|
| 218 |
-
trials = merge_trials(trials, trials_obj.trials[-n_trials:])
|
| 219 |
|
| 220 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
|
| 222 |
-
|
| 223 |
-
if len(loaded_fnames) == 0:
|
| 224 |
-
trials = Trials()
|
| 225 |
|
| 226 |
-
|
| 227 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
best = fmin(
|
| 229 |
run_trial,
|
| 230 |
space=space,
|
| 231 |
algo=tpe.suggest,
|
| 232 |
-
max_evals=
|
| 233 |
trials=trials,
|
| 234 |
-
|
| 235 |
-
rstate=np.random.default_rng(np.random.randint(1, 10**6)),
|
| 236 |
)
|
| 237 |
-
except hyperopt.exceptions.AllTrialsFailed:
|
| 238 |
-
continue
|
| 239 |
|
| 240 |
print("current best", best)
|
| 241 |
hyperopt_trial = Trials()
|
|
@@ -245,3 +329,5 @@ while True:
|
|
| 245 |
new_fname = TRIALS_FOLDER + "/" + str(np.random.randint(0, sys.maxsize)) + ".pkl"
|
| 246 |
pkl.dump({"trials": save_trials, "n": n}, open(new_fname, "wb"))
|
| 247 |
loaded_fnames.append(new_fname)
|
|
|
|
|
|
|
|
|
| 5 |
from pysr import PySRRegressor
|
| 6 |
import hyperopt
|
| 7 |
from hyperopt import hp, fmin, tpe, Trials
|
| 8 |
+
from hyperopt.fmin import generate_trials_to_calculate
|
| 9 |
|
| 10 |
# Change the following code to your file
|
| 11 |
################################################################################
|
| 12 |
TRIALS_FOLDER = "trials"
|
| 13 |
NUMBER_TRIALS_PER_RUN = 1
|
| 14 |
+
timeout_in_minutes = 5
|
| 15 |
|
| 16 |
# Test run to compile everything:
|
| 17 |
binary_operators = ["*", "/", "+", "-"]
|
| 18 |
unary_operators = ["sin", "cos", "exp", "log"]
|
| 19 |
julia_project = None
|
| 20 |
+
procs = 4
|
| 21 |
model = PySRRegressor(
|
| 22 |
binary_operators=binary_operators,
|
| 23 |
unary_operators=unary_operators,
|
| 24 |
timeout_in_seconds=30,
|
| 25 |
julia_project=julia_project,
|
| 26 |
+
procs=procs,
|
| 27 |
)
|
| 28 |
model.fit(np.random.randn(100, 3), np.random.randn(100))
|
| 29 |
|
|
|
|
| 59 |
if invalid:
|
| 60 |
return dict(status="fail", loss=float("inf"))
|
| 61 |
|
| 62 |
+
args["timeout_in_seconds"] = timeout_in_minutes * 60
|
| 63 |
args["julia_project"] = julia_project
|
| 64 |
+
args["procs"] = procs
|
| 65 |
+
|
| 66 |
+
print(f"Running trial with args: {args}")
|
| 67 |
|
| 68 |
# Create the dataset:
|
| 69 |
+
ntrials = 3
|
| 70 |
+
losses = []
|
|
|
|
| 71 |
|
| 72 |
# Old datasets:
|
| 73 |
+
eval_str = [
|
| 74 |
+
"np.cos(2.3 * X[:, 0]) * np.sin(2.3 * X[:, 0] * X[:, 1] * X[:, 2]) - 10.0",
|
| 75 |
+
"(np.exp(X[:, 3]*0.3) + 3)/(np.exp(X[:, 1]*0.2) + np.cos(X[:, 0]) + 1.1)",
|
| 76 |
+
# "np.sign(X[:, 2])*np.abs(X[:, 2])**2.5 + 5*np.cos(X[:, 3]) - 5",
|
| 77 |
+
# "np.exp(X[:, 0]/2) + 12.0 + np.log(np.abs(X[:, 0])*10 + 1)",
|
| 78 |
+
# "X[:, 0] * np.sin(2*np.pi * (X[:, 1] * X[:, 2] - X[:, 3] / X[:, 4])) + 3.0",
|
| 79 |
+
]
|
| 80 |
|
| 81 |
+
for expression in eval_str:
|
| 82 |
+
expression_losses = []
|
| 83 |
+
for i in range(ntrials):
|
| 84 |
+
rstate = np.random.RandomState(i)
|
| 85 |
+
X = 3 * rstate.randn(200, 5)
|
| 86 |
+
y = eval(expression)
|
| 87 |
+
|
| 88 |
+
# Normalize y so that losses are fair:
|
| 89 |
+
y = (y - np.average(y)) / np.std(y)
|
| 90 |
+
|
| 91 |
+
# Create the model:
|
| 92 |
+
model = PySRRegressor(**args)
|
| 93 |
+
|
| 94 |
+
# Run the model:
|
| 95 |
+
try:
|
| 96 |
+
model.fit(X, y)
|
| 97 |
+
except RuntimeError:
|
| 98 |
+
return dict(status="fail", loss=float("inf"))
|
| 99 |
|
| 100 |
+
# Compute loss:
|
| 101 |
+
cur_loss = float(model.get_best()["loss"])
|
| 102 |
+
expression_losses.append(cur_loss)
|
| 103 |
|
| 104 |
+
losses.append(np.median(expression_losses))
|
|
|
|
|
|
|
| 105 |
|
| 106 |
+
loss = np.average(losses)
|
| 107 |
print(f"Finished with {loss}", str(args))
|
| 108 |
|
| 109 |
+
return dict(status="ok", loss=loss)
|
| 110 |
|
| 111 |
|
| 112 |
space = dict(
|
|
|
|
| 180 |
tournament_selection_p=hp.uniform("tournament_selection_p", 0.0, 1.0),
|
| 181 |
)
|
| 182 |
|
| 183 |
+
init_vals = [
|
| 184 |
+
dict(
|
| 185 |
+
model_selection=0, # 0 means first choice
|
| 186 |
+
binary_operators=0,
|
| 187 |
+
unary_operators=0,
|
| 188 |
+
populations=100.0,
|
| 189 |
+
niterations=0,
|
| 190 |
+
ncyclesperiteration=100.0,
|
| 191 |
+
alpha=0.1,
|
| 192 |
+
annealing=0,
|
| 193 |
+
# fractionReplaced=0.01,
|
| 194 |
+
fractionReplaced=0.01,
|
| 195 |
+
# fractionReplacedHof=0.005,
|
| 196 |
+
fractionReplacedHof=0.005,
|
| 197 |
+
# npop=100,
|
| 198 |
+
npop=100.0,
|
| 199 |
+
# parsimony=1e-4,
|
| 200 |
+
parsimony=1e-4,
|
| 201 |
+
# topn=10,
|
| 202 |
+
topn=10.0,
|
| 203 |
+
# weightAddNode=1,
|
| 204 |
+
weightAddNode=1.0,
|
| 205 |
+
# weightInsertNode=3,
|
| 206 |
+
weightInsertNode=3.0,
|
| 207 |
+
# weightDeleteNode=3,
|
| 208 |
+
weightDeleteNode=3.0,
|
| 209 |
+
# weightDoNothing=1,
|
| 210 |
+
weightDoNothing=1.0,
|
| 211 |
+
# weightMutateConstant=10,
|
| 212 |
+
weightMutateConstant=10.0,
|
| 213 |
+
# weightMutateOperator=1,
|
| 214 |
+
weightMutateOperator=1.0,
|
| 215 |
+
# weightRandomize=1,
|
| 216 |
+
weightRandomize=1.0,
|
| 217 |
+
# weightSimplify=0.002,
|
| 218 |
+
weightSimplify=0, # One of these is fixed.
|
| 219 |
+
# perturbationFactor=1.0,
|
| 220 |
+
perturbationFactor=1.0,
|
| 221 |
+
# maxsize=20,
|
| 222 |
+
maxsize=0,
|
| 223 |
+
# warmupMaxsizeBy=0.0,
|
| 224 |
+
warmupMaxsizeBy=0.0,
|
| 225 |
+
# useFrequency=True,
|
| 226 |
+
useFrequency=1,
|
| 227 |
+
# optimizer_nrestarts=3,
|
| 228 |
+
optimizer_nrestarts=3.0,
|
| 229 |
+
# optimize_probability=1.0,
|
| 230 |
+
optimize_probability=1.0,
|
| 231 |
+
# optimizer_iterations=10,
|
| 232 |
+
optimizer_iterations=10.0,
|
| 233 |
+
# tournament_selection_p=1.0,
|
| 234 |
+
tournament_selection_p=0.999,
|
| 235 |
+
)
|
| 236 |
+
]
|
| 237 |
+
|
| 238 |
################################################################################
|
| 239 |
|
| 240 |
|
|
|
|
| 267 |
|
| 268 |
|
| 269 |
loaded_fnames = []
|
| 270 |
+
trials = generate_trials_to_calculate(init_vals)
|
| 271 |
+
i = 0
|
| 272 |
+
n = NUMBER_TRIALS_PER_RUN
|
| 273 |
+
|
| 274 |
# Run new hyperparameter trials until killed
|
| 275 |
while True:
|
| 276 |
np.random.seed()
|
|
|
|
| 278 |
# Load up all runs:
|
| 279 |
import glob
|
| 280 |
|
| 281 |
+
if i > 0:
|
| 282 |
+
path = TRIALS_FOLDER + "/*.pkl"
|
| 283 |
+
for fname in glob.glob(path):
|
| 284 |
+
if fname in loaded_fnames:
|
| 285 |
+
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 286 |
|
| 287 |
+
trials_obj = pkl.load(open(fname, "rb"))
|
| 288 |
+
n_trials = trials_obj["n"]
|
| 289 |
+
trials_obj = trials_obj["trials"]
|
| 290 |
+
if len(loaded_fnames) == 0:
|
| 291 |
+
trials = trials_obj
|
| 292 |
+
else:
|
| 293 |
+
print("Merging trials")
|
| 294 |
+
trials = merge_trials(trials, trials_obj.trials[-n_trials:])
|
| 295 |
|
| 296 |
+
loaded_fnames.append(fname)
|
|
|
|
|
|
|
| 297 |
|
| 298 |
+
print("Loaded trials", len(loaded_fnames))
|
| 299 |
+
if len(loaded_fnames) == 0:
|
| 300 |
+
trials = Trials()
|
| 301 |
+
|
| 302 |
+
try:
|
| 303 |
+
best = fmin(
|
| 304 |
+
run_trial,
|
| 305 |
+
space=space,
|
| 306 |
+
algo=tpe.suggest,
|
| 307 |
+
max_evals=n + len(trials.trials),
|
| 308 |
+
trials=trials,
|
| 309 |
+
verbose=1,
|
| 310 |
+
rstate=np.random.default_rng(np.random.randint(1, 10**6)),
|
| 311 |
+
)
|
| 312 |
+
except hyperopt.exceptions.AllTrialsFailed:
|
| 313 |
+
continue
|
| 314 |
+
else:
|
| 315 |
best = fmin(
|
| 316 |
run_trial,
|
| 317 |
space=space,
|
| 318 |
algo=tpe.suggest,
|
| 319 |
+
max_evals=2,
|
| 320 |
trials=trials,
|
| 321 |
+
points_to_evaluate=init_vals,
|
|
|
|
| 322 |
)
|
|
|
|
|
|
|
| 323 |
|
| 324 |
print("current best", best)
|
| 325 |
hyperopt_trial = Trials()
|
|
|
|
| 329 |
new_fname = TRIALS_FOLDER + "/" + str(np.random.randint(0, sys.maxsize)) + ".pkl"
|
| 330 |
pkl.dump({"trials": save_trials, "n": n}, open(new_fname, "wb"))
|
| 331 |
loaded_fnames.append(new_fname)
|
| 332 |
+
|
| 333 |
+
i += 1
|