Spaces:
Sleeping
Sleeping
File size: 6,719 Bytes
cc2f913 009c407 a95ae71 cc2f913 4854265 a95ae71 cc2f913 c4d7c3f 35b5720 a95ae71 c4d7c3f a95ae71 4854265 cc2f913 a95ae71 35b5720 a95ae71 eb8a07c cc2f913 a95ae71 cc2f913 35b5720 a95ae71 597f1d0 a95ae71 009c407 597f1d0 4f80ce5 597f1d0 a95ae71 597f1d0 a95ae71 597f1d0 a95ae71 cc2f913 a95ae71 cc2f913 a95ae71 cc2f913 a95ae71 35b5720 a95ae71 2e104cc a95ae71 cc2f913 a95ae71 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 201 202 203 204 205 206 207 208 209 210 |
"""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
import numpy as np
@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)
for key in 'niterations npop'.split(' '):
args[key] = int(args[key])
total_steps = 10*100*1000
niterations = args['niterations']
npop = args['npop']
if niterations == 0 or npop == 0:
print("Bad parameters")
return {'status': 'ok', 'loss': np.inf}
args['ncyclesperiteration'] = int(total_steps / (niterations * npop))
args['topn'] = 10
args['parsimony'] = 1e-3
args['annealing'] = True
if args['npop'] < 20 or args['ncyclesperiteration'] < 3:
print("Bad parameters")
return {'status': 'ok', 'loss': np.inf}
args['weightDoNothing'] = 1.0
maxTime = 30
ntrials = 2
equation_file = f'.hall_of_fame_{np.random.rand():f}.csv'
with temp_seed(0):
X = np.random.randn(100, 5)*3
eval_str = ["np.sign(X[:, 2])*np.abs(X[:, 2])**2.5 + 5*np.cos(X[:, 3]) - 5",
"np.sign(X[:, 2])*np.abs(X[:, 2])**3.5 + 1/(np.abs(X[:, 0])+1)",
"np.exp(X[:, 0]/2) + 12.0 + np.log(np.abs(X[:, 0])*10 + 1)",
"1.0 + 3*X[:, 0]**2 - 0.5*X[:, 0]**3 + 0.1*X[:, 0]**4",
"(np.exp(X[:, 3]) + 3)/(np.abs(X[:, 1]) + np.cos(X[:, 0]) + 1.1)"]
print(f"Starting", str(args))
try:
trials = []
for i in range(3, 6):
print(f"Starting test {i}")
for j in range(ntrials):
print(f"Starting trial {j}")
trial = pysr.pysr(
test=f"simple{i}",
procs=4,
binary_operators=["plus", "mult", "pow", "div"],
unary_operators=["cos", "exp", "sin", "loga", "abs"],
equation_file=equation_file,
timeout=maxTime,
maxsize=25,
verbosity=0,
**args)
if len(trial) == 0: raise ValueError
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 {trials[-1]}")
except ValueError:
print(f"Broken", str(args))
return {
'status': 'ok', # or 'fail' if nan loss
'loss': np.inf
}
loss = np.average(trials)
print(f"Finished with {loss}", str(args))
return {
'status': 'ok', # or 'fail' if nan loss
'loss': loss
}
space = {
'niterations': hp.qlognormal('niterations', np.log(10), 1.0, 1),
'npop': hp.qlognormal('npop', np.log(100), 1.0, 1),
'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
hyperopt_trial = Trials().new_trial_docs(
tids=[None],
specs=[None],
results=[None],
miscs=[None])
hyperopt_trial[0] = trial
hyperopt_trial[0]['tid'] = tid
hyperopt_trial[0]['misc']['tid'] = tid
for key in hyperopt_trial[0]['misc']['idxs'].keys():
hyperopt_trial[0]['misc']['idxs'][key] = [tid]
trials1.insert_trial_docs(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)
|