def base_minimizer(model, data, functions, algo, max_evals, trials, rseed=1337, full_model_string=None, notebook_name=None, verbose=True, stack=3, keep_temp=False, data_args=None): if full_model_string is not None: model_str = full_model_string else: model_str = get_hyperopt_model_string(model, data, functions, notebook_name, verbose, stack, data_args=data_args) temp_file = './temp_model.py' write_temp_files(model_str, temp_file) if 'temp_model' in sys.modules: del sys.modules["temp_model"] try: from temp_model import keras_fmin_fnct, get_space except: print("Unexpected error: {}".format(sys.exc_info()[0])) raise try: if not keep_temp: os.remove(temp_file) os.remove(temp_file + 'c') except OSError: pass try: # for backward compatibility. return (fmin(keras_fmin_fnct, space=get_space(), algo=algo, max_evals=max_evals, trials=trials, rseed=rseed, return_argmin=True), get_space()) except TypeError: pass return (fmin(keras_fmin_fnct, space=get_space(), algo=algo, max_evals=max_evals, trials=trials, rstate=np.random.RandomState(rseed), return_argmin=True), get_space())
def base_minimizer(model, data, algo, max_evals, trials, rseed=1337, full_model_string=None): if full_model_string is not None: model_str = full_model_string else: model_str = get_hyperopt_model_string(model, data) write_temp_files(model_str) try: from temp_model import keras_fmin_fnct, get_space except: print("Unexpected error: {}".format(sys.exc_info()[0])) raise try: os.remove('./temp_model.py') os.remove('./temp_model.pyc') except OSError: pass try: # for backward compatibility. best_run = fmin(keras_fmin_fnct, space=get_space(), algo=algo, max_evals=max_evals, trials=trials, rseed=rseed) except TypeError: best_run = fmin(keras_fmin_fnct, space=get_space(), algo=algo, max_evals=max_evals, trials=trials, rstate=np.random.RandomState(rseed)) return best_run
def base_minimizer(model, data, functions, algo, max_evals, trials, rseed=1337, full_model_string=None, notebook_name=None, verbose=True, stack=3, keep_temp=False): if full_model_string is not None: model_str = full_model_string else: model_str = get_hyperopt_model_string(model, data, functions, notebook_name, verbose, stack) temp_file = './temp_model.py' write_temp_files(model_str, temp_file) if 'temp_model' in sys.modules: del sys.modules["temp_model"] try: from temp_model import keras_fmin_fnct, get_space except: print("Unexpected error: {}".format(sys.exc_info()[0])) raise try: if not keep_temp: os.remove(temp_file) os.remove(temp_file + 'c') except OSError: pass try: # for backward compatibility. return ( fmin(keras_fmin_fnct, space=get_space(), algo=algo, max_evals=max_evals, trials=trials, rseed=rseed, return_argmin=True), get_space() ) except TypeError: pass return ( fmin(keras_fmin_fnct, space=get_space(), algo=algo, max_evals=max_evals, trials=trials, rstate=np.random.RandomState(rseed), return_argmin=True), get_space() )
def base_minimizer(model, data, algo, max_evals, trials, rseed=1337, full_model_string=None): if full_model_string is not None: model_str = full_model_string else: model_str = get_hyperopt_model_string(model, data) write_temp_files(model_str) try: from temp_model import keras_fmin_fnct, get_space except: print("Unexpected error: {}".format(sys.exc_info()[0])) raise try: os.remove('./temp_model.py') os.remove('./temp_model.pyc') except OSError: pass best_run = fmin(keras_fmin_fnct, space=get_space(), algo=algo, max_evals=max_evals, trials=trials, rseed=rseed) return best_run
def base_minimizer(model, data, algo, max_evals, trials, rseed=1337, full_model_string=None, notebook_name=None, verbose=True, stack=3): if full_model_string is not None: model_str = full_model_string else: model_str = get_hyperopt_model_string(model, data, notebook_name, verbose, stack) temp_file = './temp_model.py' write_temp_files(model_str, temp_file) try: from temp_model import keras_fmin_fnct, get_space except: print("Unexpected error: {}".format(sys.exc_info()[0])) raise try: os.remove(temp_file) os.remove(temp_file + 'c') except OSError: pass try: # for backward compatibility. return fmin(keras_fmin_fnct, space=get_space(), algo=algo, max_evals=max_evals, trials=trials, rseed=rseed) except TypeError: pass return fmin(keras_fmin_fnct, space=get_space(), algo=algo, max_evals=max_evals, trials=trials, rstate=np.random.RandomState(rseed))
def minimize(model, data, algo, max_evals, trials): import inspect import re model_string = inspect.getsource(model) data_string = inspect.getsource(data) parts = re.findall(r"(\w+(?=\s*[\=\(]\s*\{\{[^}]+}\}))", model_string) part_dict = {} for i, part in enumerate(parts): if part in part_dict.keys(): part_dict[part] += 1 parts[i] = part + "_" + str(part_dict[part]) else: part_dict[part] = 0 aug_parts = [] for (i, part) in enumerate(parts): aug_parts.append("space['" + part + "']") hyperopt_params = re.findall(r"(\{\{[^}]+}\})", model_string) for (i, param) in enumerate(hyperopt_params): hyperopt_params[i] = re.sub(r"[\{\}]", '', param) space = "def get_space(): \n return {\n" for name, param in zip(parts, hyperopt_params): param = re.sub(r"\(", "('" + name + "', ", param) space += "'" + name + "': hp." + param + "," space = space[:-1] space += "\n}" print('>>> Hyperas search space:\n') print(space) first_line = model_string.split("\n")[0] model_string = model_string.replace(first_line, "def keras_fmin_fnct(space):\n") # model_string = re.sub(r"def \s*\w*\s*\(", "def keras_fmin_fnct(space, ", model_string) result = re.sub(r"(\{\{[^}]+}\})", lambda match: aug_parts.pop(0), model_string, count=len(parts)) print('>>> Resulting replaced keras model:\n') print(result) first_line = data_string.split("\n")[0] data_string = data_string.replace(first_line, "") data_string = re.sub(r"return.*", "", data_string) split_data = data_string.split("\n") for i, line in enumerate(split_data): split_data[i] = line.strip() + "\n" data_string = ''.join(split_data) print(">>> Data") print(data_string) with open('./temp_model.py', 'w') as f: f.write("from hyperopt import fmin, tpe, hp, STATUS_OK, Trials\n") f.write(data_string) f.write("\n\n") f.write(result) f.write("\n\n") f.write(space) f.close() import sys sys.path.append(".") from temp_model import keras_fmin_fnct, get_space try: os.remove('./temp_model.py') os.remove('./temp_model.pyc') except OSError: pass best = fmin(keras_fmin_fnct, space=get_space(), algo=algo, max_evals=max_evals, trials=trials) return best