Spaces:
Running
Running
File size: 6,310 Bytes
cc2f913 009c407 a95ae71 7d4300a a95ae71 cc2f913 7d4300a cc2f913 7d4300a cc2f913 7d4300a cc2f913 4854265 7d4300a 4854265 7d4300a 4854265 7d4300a 8df173b cc2f913 a95ae71 7d4300a a95ae71 8df173b 7d4300a 8df173b a95ae71 5873e72 cc2f913 03d4ec3 8df173b a95ae71 597f1d0 a95ae71 8df173b 7d4300a 4f80ce5 8df173b 597f1d0 8df173b a95ae71 7d4300a 03d4ec3 7d4300a a95ae71 03d4ec3 a95ae71 cc2f913 5873e72 7d4300a 03d4ec3 a95ae71 cc2f913 7d4300a cc2f913 7d4300a cc2f913 7d4300a cc2f913 7d4300a cc2f913 7d4300a 03d4ec3 7d4300a 03d4ec3 cc2f913 7d4300a cc2f913 7d4300a cc2f913 7d4300a cc2f913 7d4300a cc2f913 7d4300a cc2f913 7d4300a cc2f913 7d4300a cc2f913 |
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 |
"""Start a hyperoptimization from a single node"""
import sys
import numpy as np
import pickle as pkl
import hyperopt
from hyperopt import hp, fmin, tpe, Trials
import pysr
import time
import contextlib
@contextlib.contextmanager
def temp_seed(seed):
state = np.random.get_state()
np.random.seed(seed)
try:
yield
finally:
np.random.set_state(state)
# Change the following code to your file
################################################################################
TRIALS_FOLDER = "trials"
NUMBER_TRIALS_PER_RUN = 1
def run_trial(args):
"""Evaluate the model loss using the hyperparams in args
:args: A dictionary containing all hyperparameters
:returns: Dict with status and loss from cross-validation
"""
print("Running on", args)
args["niterations"] = 100
args["npop"] = 100
args["ncyclesperiteration"] = 1000
args["topn"] = 10
args["parsimony"] = 0.0
args["useFrequency"] = True
args["annealing"] = True
if args["npop"] < 20 or args["ncyclesperiteration"] < 3:
print("Bad parameters")
return {"status": "ok", "loss": np.inf}
args["weightDoNothing"] = 1.0
ntrials = 3
with temp_seed(0):
X = np.random.randn(100, 10) * 3
eval_str = [
"np.sign(X[:, 2])*np.abs(X[:, 2])**2.5 + 5*np.cos(X[:, 3]) - 5",
"np.exp(X[:, 0]/2) + 12.0 + np.log(np.abs(X[:, 0])*10 + 1)",
"(np.exp(X[:, 3]) + 3)/(np.abs(X[:, 1]) + np.cos(X[:, 0]) + 1.1)",
"X[:, 0] * np.sin(2*np.pi * (X[:, 1] * X[:, 2] - X[:, 3] / X[:, 4])) + 3.0",
]
print("Starting", str(args))
try:
local_trials = []
for i in range(len(eval_str)):
print(f"Starting test {i}")
for j in range(ntrials):
print(f"Starting trial {j}")
y = eval(eval_str[i])
trial = pysr.pysr(
X,
y,
procs=4,
populations=20,
binary_operators=["plus", "mult", "pow", "div"],
unary_operators=["cos", "exp", "sin", "logm", "abs"],
maxsize=25,
constraints={"pow": (-1, 1)},
**args,
)
if len(trial) == 0:
raise ValueError
local_trials.append(
np.min(trial["MSE"]) ** 0.5 / np.std(eval(eval_str[i - 1]))
)
print(f"Test {i} trial {j} with", str(args), f"got {local_trials[-1]}")
except ValueError:
print("Broken", str(args))
return {"status": "ok", "loss": np.inf} # or 'fail' if nan loss
loss = np.average(local_trials)
print(f"Finished with {loss}", str(args))
return {"status": "ok", "loss": loss} # or 'fail' if nan loss
space = {
"alpha": hp.lognormal("alpha", np.log(10.0), 1.0),
"fractionReplacedHof": hp.lognormal("fractionReplacedHof", np.log(0.1), 1.0),
"fractionReplaced": hp.lognormal("fractionReplaced", np.log(0.1), 1.0),
"perturbationFactor": hp.lognormal("perturbationFactor", np.log(1.0), 1.0),
"weightMutateConstant": hp.lognormal("weightMutateConstant", np.log(4.0), 1.0),
"weightMutateOperator": hp.lognormal("weightMutateOperator", np.log(0.5), 1.0),
"weightAddNode": hp.lognormal("weightAddNode", np.log(0.5), 1.0),
"weightInsertNode": hp.lognormal("weightInsertNode", np.log(0.5), 1.0),
"weightDeleteNode": hp.lognormal("weightDeleteNode", np.log(0.5), 1.0),
"weightSimplify": hp.lognormal("weightSimplify", np.log(0.05), 1.0),
"weightRandomize": hp.lognormal("weightRandomize", np.log(0.25), 1.0),
}
################################################################################
def merge_trials(trials1, trials2_slice):
"""Merge two hyperopt trials objects
:trials1: The primary trials object
:trials2_slice: A slice of the trials object to be merged,
obtained with, e.g., trials2.trials[:10]
:returns: The merged trials object
"""
max_tid = 0
if len(trials1.trials) > 0:
max_tid = max([trial["tid"] for trial in trials1.trials])
for trial in trials2_slice:
tid = trial["tid"] + max_tid + 1
local_hyperopt_trial = Trials().new_trial_docs(
tids=[None], specs=[None], results=[None], miscs=[None]
)
local_hyperopt_trial[0] = trial
local_hyperopt_trial[0]["tid"] = tid
local_hyperopt_trial[0]["misc"]["tid"] = tid
for key in local_hyperopt_trial[0]["misc"]["idxs"].keys():
local_hyperopt_trial[0]["misc"]["idxs"][key] = [tid]
trials1.insert_trial_docs(local_hyperopt_trial)
trials1.refresh()
return trials1
loaded_fnames = []
trials = None
# Run new hyperparameter trials until killed
while True:
np.random.seed()
# Load up all runs:
import glob
path = TRIALS_FOLDER + "/*.pkl"
for fname in glob.glob(path):
if fname in loaded_fnames:
continue
trials_obj = pkl.load(open(fname, "rb"))
n_trials = trials_obj["n"]
trials_obj = trials_obj["trials"]
if len(loaded_fnames) == 0:
trials = trials_obj
else:
print("Merging trials")
trials = merge_trials(trials, trials_obj.trials[-n_trials:])
loaded_fnames.append(fname)
print("Loaded trials", len(loaded_fnames))
if len(loaded_fnames) == 0:
trials = Trials()
n = NUMBER_TRIALS_PER_RUN
try:
best = fmin(
run_trial,
space=space,
algo=tpe.suggest,
max_evals=n + len(trials.trials),
trials=trials,
verbose=1,
rstate=np.random.RandomState(np.random.randint(1, 10 ** 6)),
)
except hyperopt.exceptions.AllTrialsFailed:
continue
print("current best", best)
hyperopt_trial = Trials()
# Merge with empty trials dataset:
save_trials = merge_trials(hyperopt_trial, trials.trials[-n:])
new_fname = (
TRIALS_FOLDER
+ "/"
+ str(np.random.randint(0, sys.maxsize))
+ str(time.time())
+ ".pkl"
)
pkl.dump({"trials": save_trials, "n": n}, open(new_fname, "wb"))
loaded_fnames.append(new_fname)
|