<s> import numpy as np <EOL> import scipy . stats <EOL> import random <EOL> from deap import base <EOL> from deap import creator <EOL> from deap import tools <EOL> import tf_functions <EOL> def rmse ( p , t ) : <EOL> '''<STR_LIT>''' <EOL> return np . sqrt ( np . average ( np . square ( np . subtract ( p , t ) ) ) ) <EOL> def r_squared ( p , t ) : <EOL> '''<STR_LIT>''' <EOL> if len ( t . shape ) == <NUM_LIT:2> : <EOL> t = [ i [ <NUM_LIT:0> ] for i in t ] <EOL> if len ( p . shape ) == <NUM_LIT:2> : <EOL> p = [ i [ <NUM_LIT:0> ] for i in p ] <EOL> slope , intercept , r_value , p_value , std_err = scipy . stats . linregress ( p , t ) <EOL> return r_value ** <NUM_LIT:2> <EOL> def accuracy ( p , t ) : <EOL> '''<STR_LIT>''' <EOL> return sum ( [ <NUM_LIT:1> for i , j in zip ( np . argmax ( p , axis = <NUM_LIT:1> ) , np . argmax ( t , axis = <NUM_LIT:1> ) ) if i == j ] ) / float ( len ( p ) ) <EOL> def specificity ( p , t ) : <EOL> '''<STR_LIT>''' <EOL> num = sum ( [ <NUM_LIT:1> for i , j in zip ( np . argmax ( p , axis = <NUM_LIT:1> ) , np . argmax ( t , axis = <NUM_LIT:1> ) ) if i == <NUM_LIT:0> and j == <NUM_LIT:0> ] ) <EOL> denom = sum ( [ <NUM_LIT:1> for i in np . argmax ( t , axis = <NUM_LIT:1> ) if i == <NUM_LIT:0> ] ) <EOL> return num / float ( denom ) <EOL> def sensitivity ( p , t ) : <EOL> '''<STR_LIT>''' <EOL> num = sum ( [ <NUM_LIT:1> for i , j in zip ( np . argmax ( p , axis = <NUM_LIT:1> ) , np . argmax ( t , axis = <NUM_LIT:1> ) ) if i == <NUM_LIT:1> and j == <NUM_LIT:1> ] ) <EOL> denom = sum ( [ <NUM_LIT:1> for i in np . argmax ( t , axis = <NUM_LIT:1> ) if i == <NUM_LIT:1> ] ) <EOL> return num / float ( denom ) <EOL> def test_train_split ( X , y , num_test = <NUM_LIT:0> ) : <EOL> '''<STR_LIT>''' <EOL> if num_test == <NUM_LIT:0> : <EOL> num_test = y . shape [ <NUM_LIT:0> ] / <NUM_LIT:5> <EOL> if len ( y . shape ) == <NUM_LIT:1> : <EOL> y = y . reshape ( len ( y ) , <NUM_LIT:1> ) <EOL> output_features = y . shape [ <NUM_LIT:1> ] <EOL> all_vals = np . append ( X , y , <NUM_LIT:1> ) <EOL> np . random . shuffle ( all_vals ) <EOL> teX , teY = all_vals [ : num_test , : - output_features ] , all_vals [ : num_test , - output_features : ] <EOL> trX , trY = all_vals [ num_test : , : - output_features ] , all_vals [ num_test : , - output_features : ] <EOL> return trX , trY , teX , teY <EOL> def evolve ( predict_type , fitness_measure , trX , trY , <EOL> max_layers = <NUM_LIT:5> , num_gens = <NUM_LIT:10> , gen_size = <NUM_LIT> , teX = [ ] , teY = [ ] , <EOL> layer_types = [ '<STR_LIT:relu>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:none>' , '<STR_LIT>' ] , <EOL> layer_sizes = [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:10> , <NUM_LIT:50> , <NUM_LIT:100> , <NUM_LIT:200> , <NUM_LIT> , <NUM_LIT:1000> ] , <EOL> end_types = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:none>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> train_types = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> cross_prob = <NUM_LIT> , mut_prob = <NUM_LIT> , tourn_size = <NUM_LIT:5> , train_iters = <NUM_LIT:5> , squash_errors = True ) : <EOL> '''<STR_LIT>''' <EOL> if not len ( layer_types ) == len ( layer_sizes ) == len ( end_types ) == len ( train_types ) : <EOL> print ( '<STR_LIT>' ) <EOL> return None <EOL> if predict_type == '<STR_LIT>' : <EOL> predictor = tf_functions . Regresser <EOL> elif predict_type == '<STR_LIT>' : <EOL> predictor = tf_functions . Classifier <EOL> end_types = [ '<STR_LIT>' ] * len ( layer_types ) <EOL> if fitness_measure == '<STR_LIT>' : <EOL> measure = rmse <EOL> creator . create ( "<STR_LIT>" , base . Fitness , weights = ( - <NUM_LIT:1.0> , ) ) <EOL> creator . create ( "<STR_LIT>" , list , fitness = creator . FitnessMin ) <EOL> fail_val = np . inf <EOL> elif fitness_measure == '<STR_LIT>' : <EOL> measure = r_squared <EOL> creator . create ( "<STR_LIT>" , base . Fitness , weights = ( <NUM_LIT:1.0> , ) ) <EOL> creator . create ( "<STR_LIT>" , list , fitness = creator . FitnessMax ) <EOL> fail_val = <NUM_LIT:0> <EOL> elif fitness_measure == '<STR_LIT>' : <EOL> measure = accuracy <EOL> creator . create ( "<STR_LIT>" , base . Fitness , weights = ( <NUM_LIT:1.0> , ) ) <EOL> creator . create ( "<STR_LIT>" , list , fitness = creator . FitnessMax ) <EOL> fail_val = <NUM_LIT:0> <EOL> elif fitness_measure == '<STR_LIT>' : <EOL> measure = specificity <EOL> creator . create ( "<STR_LIT>" , base . Fitness , weights = ( <NUM_LIT:1.0> , ) ) <EOL> creator . create ( "<STR_LIT>" , list , fitness = creator . FitnessMax ) <EOL> fail_val = <NUM_LIT:0> <EOL> elif fitness_measure == '<STR_LIT>' : <EOL> measure = sensitivity <EOL> creator . create ( "<STR_LIT>" , base . Fitness , weights = ( <NUM_LIT:1.0> , ) ) <EOL> creator . create ( "<STR_LIT>" , list , fitness = creator . FitnessMax ) <EOL> fail_val = <NUM_LIT:0> <EOL> toolbox = base . Toolbox ( ) <EOL> toolbox . register ( "<STR_LIT>" , random . randint , <NUM_LIT:0> , len ( layer_types ) - <NUM_LIT:1> ) <EOL> toolbox . register ( "<STR_LIT>" , tools . initRepeat , creator . Individual , toolbox . attr_ints , n = ( max_layers * <NUM_LIT:2> ) + <NUM_LIT:2> ) <EOL> toolbox . register ( "<STR_LIT>" , tools . initRepeat , list , toolbox . individual ) <EOL> toolbox . register ( "<STR_LIT>" , tools . cxTwoPoint ) <EOL> toolbox . register ( "<STR_LIT>" , tools . mutUniformInt , low = <NUM_LIT:0> , up = len ( layer_types ) - <NUM_LIT:1> , indpb = <NUM_LIT:0.5> ) <EOL> toolbox . register ( "<STR_LIT>" , tools . selTournament , tournsize = tourn_size ) <EOL> pop = toolbox . population ( n = gen_size ) <EOL> for ind in pop : <EOL> net_type = [ ] <EOL> for i in range ( max_layers ) : <EOL> net_type . append ( layer_types [ ind [ i * <NUM_LIT:2> ] ] ) <EOL> net_type . append ( layer_sizes [ ind [ i * <NUM_LIT:2> + <NUM_LIT:1> ] ] ) <EOL> net_type . append ( end_types [ ind [ - <NUM_LIT:2> ] ] ) <EOL> if teX == [ ] : <EOL> ttrX , ttrY , tteX , tteY = test_train_split ( trX , trY ) <EOL> else : <EOL> ttrX , ttrY , tteX , tteY = trX , trY , teX , teY <EOL> if squash_errors : <EOL> try : <EOL> ind_predictor = predictor ( net_type , optimizer = train_types [ ind [ - <NUM_LIT:1> ] ] ) <EOL> ind_predictor . train ( ttrX , ttrY , train_iters ) <EOL> p = ind_predictor . predict ( tteX ) <EOL> m = measure ( p , tteY ) <EOL> ind_predictor . close ( ) <EOL> except : <EOL> m = fail_val <EOL> if np . isnan ( m ) : <EOL> m = fail_val <EOL> else : <EOL> ind_predictor = predictor ( net_type , optimizer = train_types [ ind [ - <NUM_LIT:1> ] ] ) <EOL> ind_predictor . train ( ttrX , ttrY , train_iters ) <EOL> p = ind_predictor . predict ( tteX ) <EOL> m = measure ( p , tteY ) <EOL> ind_predictor . close ( ) <EOL> ind . fitness . values = ( m , ) <EOL> for g in range ( num_gens ) : <EOL> offspring = toolbox . select ( pop , len ( pop ) ) <EOL> offspring = list ( map ( toolbox . clone , offspring ) ) <EOL> for child1 , child2 in zip ( offspring [ : : <NUM_LIT:2> ] , offspring [ <NUM_LIT:1> : : <NUM_LIT:2> ] ) : <EOL> if random . random ( ) < cross_prob : <EOL> toolbox . mate ( child1 , child2 ) <EOL> del child1 . fitness . values <EOL> del child2 . fitness . values <EOL> for mutant in offspring : <EOL> if random . random ( ) < mut_prob : <EOL> toolbox . mutate ( mutant ) <EOL> del mutant . fitness . values <EOL> invalid_ind = [ ind for ind in offspring if not ind . fitness . valid ] <EOL> for ind in invalid_ind : <EOL> net_type = [ ] <EOL> for i in range ( max_layers ) : <EOL> net_type . append ( layer_types [ ind [ i * <NUM_LIT:2> ] ] ) <EOL> net_type . append ( layer_sizes [ ind [ i * <NUM_LIT:2> + <NUM_LIT:1> ] ] ) <EOL> net_type . append ( end_types [ ind [ - <NUM_LIT:2> ] ] ) <EOL> if squash_errors : <EOL> try : <EOL> ind_predictor = predictor ( net_type , optimizer = train_types [ ind [ - <NUM_LIT:1> ] ] ) <EOL> ind_predictor . train ( ttrX , ttrY , train_iters ) <EOL> p = ind_predictor . predict ( tteX ) <EOL> m = measure ( p , tteY ) <EOL> ind_predictor . close ( ) <EOL> except : <EOL> m = fail_val <EOL> if np . isnan ( m ) : <EOL> m = fail_val <EOL> else : <EOL> ind_predictor = predictor ( net_type , optimizer = train_types [ ind [ - <NUM_LIT:1> ] ] ) <EOL> ind_predictor . train ( ttrX , ttrY , train_iters ) <EOL> p = ind_predictor . predict ( tteX ) <EOL> m = measure ( p , tteY ) <EOL> ind_predictor . close ( ) <EOL> ind . fitness . values = ( m , ) <EOL> pop [ : ] = offspring <EOL> best_ind = tools . selBest ( pop , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> net_type = [ ] <EOL> for i in range ( max_layers ) : <EOL> net_type . append ( layer_types [ best_ind [ i * <NUM_LIT:2> ] ] ) <EOL> net_type . append ( layer_sizes [ best_ind [ i * <NUM_LIT:2> + <NUM_LIT:1> ] ] ) <EOL> net_type . append ( end_types [ best_ind [ - <NUM_LIT:2> ] ] ) <EOL> optimizer = train_types [ best_ind [ - <NUM_LIT:1> ] ] <EOL> return net_type , optimizer , best_ind . fitness . values </s>
