<s> from hyperopt import fmin <EOL> from . ensemble import VotingModel <EOL> import os <EOL> import inspect <EOL> import re <EOL> import sys <EOL> sys . path . append ( "<STR_LIT:.>" ) <EOL> def minimize ( model , data , algo , max_evals , trials , rseed = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> best_run = base_minimizer ( model , data , algo , max_evals , trials , rseed ) <EOL> best_model = None <EOL> for trial in trials : <EOL> vals = trial . get ( '<STR_LIT>' ) . get ( '<STR_LIT>' ) <EOL> for key in vals . keys ( ) : <EOL> vals [ key ] = vals [ key ] [ <NUM_LIT:0> ] <EOL> if trial . get ( '<STR_LIT>' ) . get ( '<STR_LIT>' ) == best_run and '<STR_LIT>' in trial . get ( '<STR_LIT:result>' ) . keys ( ) : <EOL> best_model = trial . get ( '<STR_LIT:result>' ) . get ( '<STR_LIT>' ) <EOL> return best_run , best_model <EOL> def best_ensemble ( nb_ensemble_models , model , data , algo , max_evals , trials , voting = '<STR_LIT>' , weights = None ) : <EOL> model_list = best_models ( nb_models = nb_ensemble_models , model = model , <EOL> data = data , algo = algo , max_evals = max_evals , trials = trials ) <EOL> return VotingModel ( model_list , voting , weights ) <EOL> def best_models ( nb_models , model , data , algo , max_evals , trials ) : <EOL> base_minimizer ( model , data , algo , max_evals , trials ) <EOL> if len ( trials ) < nb_models : <EOL> nb_models = len ( trials ) <EOL> scores = [ trial . get ( '<STR_LIT:result>' ) . get ( '<STR_LIT>' ) for trial in trials ] <EOL> cut_off = sorted ( scores , reverse = True ) [ nb_models - <NUM_LIT:1> ] <EOL> model_list = [ trial . get ( '<STR_LIT:result>' ) . get ( '<STR_LIT>' ) for trial in trials if trial . get ( '<STR_LIT:result>' ) . get ( '<STR_LIT>' ) >= cut_off ] <EOL> return model_list <EOL> def get_hyperopt_model_string ( model , data ) : <EOL> model_string = inspect . getsource ( model ) <EOL> lines = model_string . split ( "<STR_LIT:\n>" ) <EOL> lines = [ line for line in lines if not line . strip ( ) . startswith ( '<STR_LIT:#>' ) ] <EOL> calling_script_file = os . path . abspath ( inspect . stack ( ) [ - <NUM_LIT:1> ] [ <NUM_LIT:1> ] ) <EOL> with open ( calling_script_file , '<STR_LIT:r>' ) as f : <EOL> calling_lines = f . read ( ) . split ( '<STR_LIT:\n>' ) <EOL> raw_imports = [ line . strip ( ) + "<STR_LIT:\n>" for line in calling_lines if "<STR_LIT>" in line ] <EOL> imports = '<STR_LIT>' . join ( raw_imports ) <EOL> model_string = [ line + "<STR_LIT:\n>" for line in lines if "<STR_LIT>" not in line ] <EOL> model_string = '<STR_LIT>' . join ( model_string ) <EOL> parts = hyperparameter_names ( model_string ) <EOL> aug_parts = augmented_names ( parts ) <EOL> hyperopt_params = get_hyperparameters ( model_string ) <EOL> space = get_hyperopt_space ( parts , hyperopt_params ) <EOL> data_string = retrieve_data_string ( data ) <EOL> model = hyperopt_keras_model ( model_string , parts , aug_parts ) <EOL> temp_str = temp_string ( imports , model , data_string , space ) <EOL> return temp_str <EOL> def base_minimizer ( model , data , algo , max_evals , trials , rseed = <NUM_LIT> , full_model_string = None ) : <EOL> if full_model_string is not None : <EOL> model_str = full_model_string <EOL> else : <EOL> model_str = get_hyperopt_model_string ( model , data ) <EOL> write_temp_files ( model_str ) <EOL> try : <EOL> from temp_model import keras_fmin_fnct , get_space <EOL> except : <EOL> print ( "<STR_LIT>" . format ( sys . exc_info ( ) [ <NUM_LIT:0> ] ) ) <EOL> raise <EOL> try : <EOL> os . remove ( '<STR_LIT>' ) <EOL> os . remove ( '<STR_LIT>' ) <EOL> except OSError : <EOL> pass <EOL> best_run = fmin ( keras_fmin_fnct , <EOL> space = get_space ( ) , <EOL> algo = algo , <EOL> max_evals = max_evals , <EOL> trials = trials , <EOL> rseed = rseed ) <EOL> return best_run <EOL> def get_hyperopt_space ( parts , hyperopt_params ) : <EOL> space = "<STR_LIT>" <EOL> for name , param in zip ( parts , hyperopt_params ) : <EOL> param = re . sub ( r"<STR_LIT>" , "<STR_LIT>" + name + "<STR_LIT>" , param , <NUM_LIT:1> ) <EOL> space += "<STR_LIT>" + name + "<STR_LIT>" + param + "<STR_LIT>" <EOL> space = space [ : - <NUM_LIT:1> ] <EOL> space += "<STR_LIT>" <EOL> print ( '<STR_LIT>' ) <EOL> print ( space ) <EOL> return space <EOL> def retrieve_data_string ( data ) : <EOL> '''<STR_LIT>''' <EOL> data_string = inspect . getsource ( data ) <EOL> first_line = data_string . split ( "<STR_LIT:\n>" ) [ <NUM_LIT:0> ] <EOL> data_string = data_string . replace ( first_line , "<STR_LIT>" ) <EOL> data_string = re . sub ( r"<STR_LIT>" , "<STR_LIT>" , data_string ) <EOL> split_data = data_string . split ( "<STR_LIT:\n>" ) <EOL> for i , line in enumerate ( split_data ) : <EOL> split_data [ i ] = line [ <NUM_LIT:4> : ] + "<STR_LIT:\n>" <EOL> data_string = '<STR_LIT>' . join ( split_data ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( data_string ) <EOL> return data_string <EOL> def hyperparameter_names ( model_string ) : <EOL> parts = [ ] <EOL> params = re . findall ( r"<STR_LIT>" , model_string ) <EOL> for param in params : <EOL> name = re . findall ( r"<STR_LIT>" + re . escape ( param ) + r"<STR_LIT>" , model_string ) <EOL> if len ( name ) > <NUM_LIT:0> : <EOL> parts . append ( name [ <NUM_LIT:0> ] ) <EOL> else : <EOL> parts . append ( parts [ - <NUM_LIT:1> ] ) <EOL> print ( "<STR_LIT>" ) <EOL> for part in parts : <EOL> print ( part ) <EOL> part_dict = { } <EOL> for i , part in enumerate ( parts ) : <EOL> if part in part_dict . keys ( ) : <EOL> part_dict [ part ] += <NUM_LIT:1> <EOL> parts [ i ] = part + "<STR_LIT:_>" + str ( part_dict [ part ] ) <EOL> else : <EOL> part_dict [ part ] = <NUM_LIT:0> <EOL> return parts <EOL> def get_hyperparameters ( model_string ) : <EOL> hyperopt_params = re . findall ( r"<STR_LIT>" , model_string ) <EOL> for i , param in enumerate ( hyperopt_params ) : <EOL> hyperopt_params [ i ] = re . sub ( r"<STR_LIT>" , '<STR_LIT>' , param ) <EOL> return hyperopt_params <EOL> def augmented_names ( parts ) : <EOL> aug_parts = [ ] <EOL> for i , part in enumerate ( parts ) : <EOL> aug_parts . append ( "<STR_LIT>" + part + "<STR_LIT>" ) <EOL> return aug_parts <EOL> def hyperopt_keras_model ( model_string , parts , aug_parts ) : <EOL> first_line = model_string . split ( "<STR_LIT:\n>" ) [ <NUM_LIT:0> ] <EOL> model_string = model_string . replace ( first_line , "<STR_LIT>" ) <EOL> result = re . sub ( r"<STR_LIT>" , lambda match : aug_parts . pop ( <NUM_LIT:0> ) , model_string , count = len ( parts ) ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( result ) <EOL> return result <EOL> def temp_string ( imports , model , data , space ) : <EOL> temp = ( imports + "<STR_LIT>" + <EOL> "<STR_LIT>" + <EOL> data + model + "<STR_LIT:\n>" + space ) <EOL> return temp <EOL> def write_temp_files ( tmp_str , path = '<STR_LIT>' ) : <EOL> with open ( path , '<STR_LIT:w>' ) as f : <EOL> f . write ( tmp_str ) <EOL> f . close ( ) <EOL> return </s>
