Esempio n. 1
0
 def try_with_params(**max_args):
     best_params, best_score, score_results = maximize(func,
                                                       param_grid,
                                                       args,
                                                       verbose=True,
                                                       **max_args)
     print("Score Results:\n{}".format(score_results))
     self.assertEqual(best_params, {'x': 0., 'y': 0., 'z': True})
     self.assertEqual(best_score, 2.)
Esempio n. 2
0
    def test_optimize(self):
        """ Simple hill climbing optimization with some twists. """

        param_grid = {
            'x': [-1., 0., 1.],
            'y': [-1., 0., 1.],
            'z': [True, False]
        }
        args = {'m': 1.}

        best_params, best_score, score_results, _, _ = maximize(func,
                                                                param_grid,
                                                                args,
                                                                verbose=True)
        print("Score Results:\n{}".format(score_results))
        self.assertEqual(best_params, {'x': 0., 'y': 0., 'z': True})
        self.assertEqual(best_score, 2.)
Esempio n. 3
0
    def test_optimize(self):
        """ Simple hill climbing optimization with some twists. """

        param_grid = {
            "x": [-1.0, 0.0, 1.0],
            "y": [-1.0, 0.0, 1.0],
            "z": [True, False]
        }
        args = {"m": 1.0}

        best_params, best_score, score_results, _, _ = maximize(func,
                                                                param_grid,
                                                                args,
                                                                verbose=True)
        print("Score Results:\n{}".format(score_results))
        self.assertEqual(best_params, {"x": 0.0, "y": 0.0, "z": True})
        self.assertEqual(best_score, 2.0)
Esempio n. 4
0
import numpy as np
from evolutionary_search import maximize


def func(x, y, m=1., z=False):
    return m * (np.exp(-(x**2 + y**2)) + float(z))


param_grid = {'x': [-1., 0., 1.], 'y': [-1., 0., 1.], 'z': [True, False]}
args = {'m': 1.}
best_params, best_score, score_results, _, _ = maximize(func,
                                                        param_grid,
                                                        args,
                                                        verbose=False)

print("best_params = %s" % best_params)
print("best_score = %.2f" % best_score)
print(score_results)
Esempio n. 5
0
    if distance >= max(y):
        os.system('scp distance.p distance_max.p')
    sys.stdout.flush()
    with open('out.txt','r') as f:
        generation_lines = [line for line in f.read().split("--- Evolve in 0 possible combinations ---")[-1].splitlines(False) if line]
    with open('generation_lines.txt','w') as f:
        f.write('\n'.join(generation_lines))
    brushes.append(pg.mkBrush(color=colors[len(generation_lines)]))

    plot.setData(x=plot_dat[:,0],y=plot_dat[:,1],symbolBrush=brushes)
    pg.QtGui.QApplication.processEvents()
    #print("Max Distance = %f"%max(y))
    #pickle.dump(distance,open('distance_realtime.p','wb'))
    #count += 1
    return distance
weights_permutations = {'w%d'%i:(np.linspace(-weights_scaling_factor,weights_scaling_factor,n_possible_weight_values) if rand_init else (np.random.rand(n_possible_weight_values)-0.5)*weights_scaling_factor) for i in range(n_weights)} # (np.random.rand(100)-0.5)*0.5
bias_permutations = {'b%d'%i:(np.linspace(-bias_scaling_factor ,bias_scaling_factor,n_possible_weight_values) if rand_init else (np.random.rand(n_possible_weight_values)-0.5 )*bias_scaling_factor) for i in range(n_bias)}
#weights_permutations = {'w%d'%i:(np.linspace(-weights_scaling_factor,weights_scaling_factor,n_possible_weight_values) + (weights_scaling_factor if activation == 'relu' else 0.) if rand_init else (np.random.rand(n_possible_weight_values)-0.5 + (0.5 if activation == 'relu' else 0.))*weights_scaling_factor) for i in range(n_weights)} # (np.random.rand(100)-0.5)*0.5
#bias_permutations = {'b%d'%i:(np.linspace(-bias_scaling_factor ,bias_scaling_factor,n_possible_weight_values) + (bias_scaling_factor if activation == 'relu' else 0.) if rand_init else (np.random.rand(n_possible_weight_values)-0.5 + (0.5 if activation == 'relu' else 0.))*bias_scaling_factor) for i in range(n_bias)}

weights_permutations.update(bias_permutations)
best_params, best_score, score_results, hist, log = maximize(distance,weights_permutations,{}, population_size=population_size, generations_number=generations_number, gene_mutation_prob=gene_mutation_prob, gene_crossover_prob = gene_mutation_prob, tournament_size=tournament_size, verbose=True)
pickle.dump((best_params, best_score, score_results, hist, log),open('final_model.p','wb'))
best_weights = {best_params[weight] for weight in weights_permutations}
pickle.dump((best_weights,architecture,activation),open('weights.p','wb'), protocol=2)
## FIXME send best weights/model to weights.p when done
# FIXME do some ML on the evolution of the hyperparameters
# PCA plot of weights with showing evaluation results colored
# individual vs score, real time
pickle.dump((np.array(X),np.array(y)), open('X_y.p','wb')) # plot these later on
Esempio n. 6
0
                        alpha=1e-5,
                        hidden_layer_sizes=(2),
                        random_state=1)

from evolutionary_search import EvolutionaryAlgorithmSearchCV
cv = EvolutionaryAlgorithmSearchCV(estimator=SVC(),
                                   params=paramgrid,
                                   scoring="accuracy",
                                   cv=StratifiedKFold(n_splits=4),
                                   verbose=1,
                                   population_size=50,
                                   gene_mutation_prob=0.10,
                                   gene_crossover_prob=0.5,
                                   tournament_size=3,
                                   generations_number=5,
                                   n_jobs=1)
cv.fit(X, y)

from evolutionary_search import maximize

def func(x, y, m=1., z=False):
    return m * (np.exp(-(x**2 + y**2)) + float(z))

param_grid = {'x': [-1., 0., 1.], 'y': [-1., 0., 1.], 'z': [True, False]}
args = {'m': 1.}
best_params, best_score, score_results, hist, logbook = maximize(func, param_grid, args, verbose=False)

print(best_params)
print(best_score)
print(score_results)
#TODO: test this program with Neural Network Model
Esempio n. 7
0
    def scan(self, model, X, y, aim='worst'):
        """Scan a Keras model.

        Args:
            model (keras model): Keras model to be scanned
            X (numpy nd-array): Input Data for scanning
            y (numpy array): Input Labels for scanning
            aim (str, optional): If 'worst' is chosen, SAFE learns 
            to generate adversarial samples. If 'better' is chosen,
            SAFE learns to improve accuracy. Defaults to 'worst'.
        """

        if not isinstance(X, np.ndarray):
            X = X.values
        if not isinstance(y, np.ndarray):
            y = y.values

        [rows, self.n_features] = X.shape

        if self.n_points is not None:
            X, _, y, _ = train_test_split(X,
                                          y,
                                          train_size=self.n_points / rows,
                                          random_state=self.random_state,
                                          stratify=y)

        self.X = X
        self.y_better = np.zeros((rows, self.n_features))
        self.y_worst = np.zeros((rows, self.n_features))
        self._generate_combination()
        self.model = model
        self.aim = aim

        # TODO migrate this also
        if self.combinations_mode == 'genetic':
            param_grid = {'combination': self.combinations}
            for i, j in enumerate(tqdm(X)):
                args = {'data': j, 'label': y_train.values[i]}
                best_params, _, _, _, _ = maximize(
                    self.__combination_search_genetic,
                    param_grid,
                    args,
                    verbose=False)
                self.X_train_fs[i, :] = j
                self.y_train_fs[i, :] = best_params['combination']

        elif self.combinations_mode == 'forward_selection':
            if self.aim == 'better':
                for i, j in enumerate(tqdm(self.X)):
                    self.y_better[
                        i, :] = self.__combination_search_forward_selection_min(
                            j, y[i])
            else:
                for i, j in enumerate(tqdm(self.X)):
                    self.y_worst[
                        i, :] = self.__combination_search_forward_selection_max(
                            j, y[i])
        else:
            losses = np.zeros((rows, self.combinations.shape[0]))
            for index, p in enumerate(tqdm(self.combinations)):
                losses[:, index] = abs(
                    self.model.predict(np.multiply(p, self.X)).flatten() - y)

            self.y_worst = self.combinations[losses.argmax(axis=1)]
            self.y_better = self.combinations[losses.argmin(axis=1)]
Esempio n. 8
0
def meta_data(X, y, dataset_id, algorithms, cat_ind, preprocessors, path,
              metadata):
    ''' Runs the pipeline on dataset X, returns the score and logs the pipeline.

    Parameters:
    -----------
    X: pd.DataFrame
        Contains the dataframe of a given dataset excluding its target column.
    y: pd.Series
        Contains the series of the target of a given dataset.
    dataset_id: integer
        Contains the id of the dataset to be optimized.
    algorithms: list
        Contains the search space of predictive algorithms.
    cat_ind: list
        Contains boolean values to determine whether a column is categorical or
        not based on OpenML implementation.
    preprocessors: list
        Contains the searchspace of preprocessing algorithms.
    path: string
        Contains the path to the directory in where the files are logged.
    metadata: pd.DataFrame
        Contains the dataframe of the metadata for dataset with id X.

    Returns:
    --------
    str
        Contains confirmation that the optimization has been ran for dataset with dataset_id.
    '''
    categorical, numeric, string = category_numeric_or_string(X, cat_ind)
    X, y = impute(X, y, categorical, numeric, string, 'mean')

    if len(preprocessors) > 0:

        param_grid = {
            'p1': preprocessors,
            'p2': preprocessors,
            'p3': preprocessors,
            'alg': algorithms,
            'k': [1, 2, 5, 10, 25]
        }

        args = {
            'X': X,
            'y': y,
            'cat_ind': cat_ind,
            'dataset_id': dataset_id,
            'categorical': categorical,
            'numeric': numeric,
            'string': string,
            'path': path,
            'metadata': metadata
        }

        best_params, best_score, score_results, _, _ = maximize(
            pipeline_run,
            param_grid,
            args,
            verbose=False,
            generations_number=10,
            population_size=100,
            tournament_size=6,
            n_jobs=1,
            gene_mutation_prob=0.50,
            gene_crossover_prob=0.90)

    else:
        for algorithm in algorithms:
            for k in [1, 2, 5, 10, 25]:
                pipeline_run(X,
                             y,
                             dataset_id,
                             algorithm,
                             cat_ind,
                             k,
                             categorical,
                             numeric,
                             string,
                             path,
                             metadata,
                             p1=None,
                             p2=None,
                             p3=None)

    return "Dataset {} has finished running.".format(dataset_id)