Пример #1
0
 def __init__(self,
              hidden_nodes=None,
              activation='relu',
              algorithm='random_hill_climb',
              max_iters=100,
              bias=True,
              is_classifier=True,
              learning_rate=0.1,
              early_stopping=False,
              clip_max=1e+10,
              restarts=0,
              schedule=GeomDecay(),
              pop_size=200,
              mutation_prob=0.1,
              max_attempts=10,
              random_state=None,
              curve=False):
     super().__init__(hidden_nodes=hidden_nodes,
                      activation=activation,
                      algorithm=algorithm,
                      max_iters=max_iters,
                      bias=bias,
                      is_classifier=is_classifier,
                      learning_rate=learning_rate,
                      early_stopping=early_stopping,
                      clip_max=clip_max,
                      restarts=restarts,
                      schedule=schedule,
                      pop_size=pop_size,
                      mutation_prob=mutation_prob,
                      max_attempts=max_attempts,
                      random_state=random_state,
                      curve=curve)
Пример #2
0
    def __init__(self,
                 hidden_nodes=None,
                 activation='relu',
                 algorithm='random_hill_climb',
                 max_iters=100,
                 bias=True,
                 is_classifier=True,
                 learning_rate=0.1,
                 early_stopping=False,
                 clip_max=1e+10,
                 restarts=0,
                 schedule=GeomDecay(),
                 pop_size=200,
                 mutation_prob=0.1,
                 max_attempts=10,
                 random_state=None,
                 curve=False,
                 state_fitness_callback=None,
                 callback_user_info=None):

        super().__init__()
        if hidden_nodes is None:
            self.hidden_nodes = []
        else:
            self.hidden_nodes = hidden_nodes

        self.activation_dict = {
            'identity': identity,
            'relu': relu,
            'sigmoid': sigmoid,
            'tanh': tanh
        }
        self.activation = activation
        self.algorithm = algorithm
        self.max_iters = max_iters
        self.bias = bias
        self.is_classifier = is_classifier
        self.learning_rate = learning_rate
        self.early_stopping = early_stopping
        self.clip_max = clip_max
        self.restarts = restarts
        self.schedule = schedule
        self.pop_size = pop_size
        self.mutation_prob = mutation_prob
        self.max_attempts = max_attempts
        self.random_state = random_state
        self.curve = curve
        self.state_fitness_callback = state_fitness_callback
        self.callback_user_info = callback_user_info

        self.node_list = []
        self.fitted_weights = []
        self.loss = np.inf
        self.output_activation = None
        self.predicted_probs = []
        self.fitness_curve = []
Пример #3
0
def simulated_annealing(problem,
                        schedule=GeomDecay(),
                        max_attempts=10,
                        max_iters=np.inf,
                        init_state=None,
                        curve=False,
                        fevals=False,
                        random_state=None,
                        state_fitness_callback=None,
                        callback_user_info=None):
    """Use simulated annealing to find the optimum for a given
    optimization problem.
    Parameters
    ----------
    problem: optimization object
        Object containing fitness function optimization problem to be solved.
        For example, :code:`DiscreteOpt()`, :code:`ContinuousOpt()` or
        :code:`TSPOpt()`.
    schedule: schedule object, default: :code:`mlrose_hiive.GeomDecay()`
        Schedule used to determine the value of the temperature parameter.
    max_attempts: int, default: 10
        Maximum number of attempts to find a better neighbor at each step.
    max_iters: int, default: np.inf
        Maximum number of iterations of the algorithm.
    init_state: array, default: None
        1-D Numpy array containing starting state for algorithm.
        If :code:`None`, then a random state is used.
    curve: bool, default: False
        Boolean to keep fitness values for a curve.
        If :code:`False`, then no curve is stored.
        If :code:`True`, then a history of fitness values is provided as a
        third return value.
    fevals: bool, default: False
        Boolean to track the number of fitness function evaluations.
        If :code:`False`, then nothing additional is returned.
        If :code:`True`, then a history of function evaluations per iteration
        is provided as a fourth return value.
    random_state: int, default: None
        If random_state is a positive integer, random_state is the seed used
        by np.random.seed(); otherwise, the random seed is not set.
    state_fitness_callback: function taking five parameters, default: None
        If specified, this callback will be invoked once per iteration.
        Parameters are (iteration, max attempts reached?, current best state, current best fit, user callback data).
        Return true to continue iterating, or false to stop.
    callback_user_info: any, default: None
        User data passed as last parameter of callback.
    Returns
    -------
    best_state: array
        Numpy array containing state that optimizes the fitness function.
    best_fitness: float
        Value of fitness function at best state.
    fitness_curve: array
        Numpy array containing the fitness at every iteration.
        Only returned if input argument :code:`curve` is :code:`True`.
    References
    ----------
    Russell, S. and P. Norvig (2010). *Artificial Intelligence: A Modern
    Approach*, 3rd edition. Prentice Hall, New Jersey, USA.
    """
    if (not isinstance(max_attempts, int) and not max_attempts.is_integer()) \
            or (max_attempts < 0):
        raise Exception("""max_attempts must be a positive integer.""")

    if (not isinstance(max_iters, int) and max_iters != np.inf
            and not max_iters.is_integer()) or (max_iters < 0):
        raise Exception("""max_iters must be a positive integer.""")

    if init_state is not None and len(init_state) != problem.get_length():
        raise Exception("""init_state must have same length as problem.""")

    # Set random seed
    if isinstance(random_state, int) and random_state > 0:
        np.random.seed(random_state)

    # Initialize problem, time and attempts counter
    if init_state is None:
        problem.reset()
    else:
        problem.set_state(init_state)

    if state_fitness_callback is not None:
        # initial call with base data
        state_fitness_callback(iteration=0,
                               state=problem.get_state(),
                               fitness=problem.get_adjusted_fitness(),
                               user_data=callback_user_info)

    fitness_curve = []

    attempts = 0
    iters = 0
    continue_iterating = True

    while (attempts < max_attempts) and (iters < max_iters):
        temp = schedule.evaluate(iters)
        iters += 1
        problem.current_iteration += 1

        if temp == 0:
            break

        else:
            # Find random neighbor and evaluate fitness
            next_state = problem.random_neighbor()
            next_fitness = problem.eval_fitness(next_state)

            # Calculate delta E and change prob
            current_fitness = problem.get_fitness()
            delta_e = next_fitness - current_fitness
            prob = np.exp(delta_e / temp)
            # print(f'{iters} : {current_fitness}')
            # If best neighbor is an improvement or random value is less
            # than prob, move to that state and reset attempts counter
            if (delta_e > 0) or (np.random.uniform() < prob):
                problem.set_state(next_state)
                attempts = 0
            else:
                attempts += 1

        if curve:
            fitness_curve.append(problem.get_adjusted_fitness())

        # invoke callback
        if state_fitness_callback is not None:
            max_attempts_reached = (attempts == max_attempts) or (
                iters == max_iters) or problem.can_stop()
            continue_iterating = state_fitness_callback(
                iteration=iters,
                attempt=attempts + 1,
                done=max_attempts_reached,
                state=problem.get_state(),
                fitness=problem.get_adjusted_fitness(),
                curve=np.asarray(fitness_curve) if curve else None,
                user_data=callback_user_info)

        # break out if requested
        if not continue_iterating:
            break

    best_fitness = problem.get_maximize() * problem.get_fitness()
    best_state = problem.get_state()

    if fevals:
        return best_state, best_fitness, np.asarray(
            fitness_curve) if curve else None, problem.fevals
    else:
        return best_state, best_fitness, np.asarray(
            fitness_curve) if curve else None
Пример #4
0
def main():
    seed = 903387974
    if verbose:
        logging.basicConfig(level=logging.DEBUG)
    else:
        logging.basicConfig(level=logging.INFO)

    if seed is None:
        seed = np.random.randint(0, (2**31) - 1)
        logger.info("Using seed {}".format(seed))
    df_credit = pd.read_csv(data_directory / credit_card_file)

    # code copied from https://www.kaggle.com/kernels/scriptcontent/2094910/download
    df_credit = df_credit.rename(columns={
        'default.payment.next.month': 'def_pay',
        'PAY_0': 'PAY_1'
    })

    features_credit = [
        'LIMIT_BAL', 'SEX', 'EDUCATION', 'MARRIAGE', 'AGE', 'PAY_1', 'PAY_2',
        'PAY_3', 'PAY_4', 'PAY_5', 'PAY_6', 'BILL_AMT1', 'BILL_AMT2',
        'BILL_AMT3', 'BILL_AMT4', 'BILL_AMT5', 'BILL_AMT6', 'PAY_AMT1',
        'PAY_AMT2', 'PAY_AMT3', 'PAY_AMT4', 'PAY_AMT5', 'PAY_AMT6'
    ]
    x_scaler = StandardScaler()
    df_credit[features_credit] = x_scaler.fit_transform(
        df_credit[features_credit])

    x_scaler = StandardScaler()
    df_credit[features_credit] = x_scaler.fit_transform(
        df_credit[features_credit])

    # model = MLPClassifier(hidden_layer_sizes=(10, 10, 10), max_iter=1000, random_state=seed)
    x_train, x_test, y_train, y_test = train_test_split(
        df_credit[features_credit],
        df_credit['def_pay'],
        test_size=0.2,
        random_state=seed)

    # One hot encode target values
    one_hot = OneHotEncoder()

    y_train_hot = one_hot.fit_transform(y_train.values.reshape(-1,
                                                               1)).todense()
    y_test_hot = one_hot.transform(y_test.values.reshape(-1, 1)).todense()

    grid_search_parameters = ({
        'hidden_layer_sizes': [(10, 10, 10)],
        'activation': ['tanh']
    })
    curves = []

    algorithms = [('random_hill_climb', 20000), ('simulated_annealing', 20000),
                  ('gradient_descent', 50), ('genetic_alg', 50)]
    restarts = 0,
    schedule = GeomDecay(init_temp=10),
    pop_size = 200,
    mutation_prob = 0.1,
    max_attempts = 10,
    for algo, iterations in algorithms:
        model = NeuralNetwork(hidden_nodes=[10, 10, 10],
                              activation='tanh',
                              algorithm=algo,
                              restarts=100,
                              pop_size=250,
                              mutation_prob=0.03,
                              schedule=GeomDecay(init_temp=10),
                              max_iters=iterations,
                              bias=True,
                              is_classifier=True,
                              learning_rate=0.001,
                              early_stopping=False,
                              clip_max=5,
                              max_attempts=100,
                              random_state=seed,
                              curve=True)
        start_time = time.time()
        model.fit(x_train, y_train_hot)
        logger.info(
            f"fit time {algo} {iterations} {str(round(time.time() - start_time, 2))}"
        )
        start_time = time.time()
        # Predict labels for train set and assess accuracy
        y_train_pred = model.predict(x_train)
        logger.info(
            f"Train predict time {algo} {iterations} {str(round(time.time() - start_time, 2))}"
        )
        y_train_accuracy = accuracy_score(y_train_hot, y_train_pred)
        logger.info(f"Train Accuracy {algo} {iterations} {y_train_accuracy}")
        # Predict labels for test set and assess accuracy
        start_time = time.time()
        y_test_pred = model.predict(x_test)
        logger.info(
            f"Test predict time {algo} {iterations} {str(round(time.time() - start_time, 2))}"
        )
        y_test_accuracy = accuracy_score(y_test_hot, y_test_pred)

        logger.info(f"Test Accuracy {algo} {iterations} {y_test_accuracy}")
        curves.append(model.fitness_curve)

    algo_list = [algorithm[0] for algorithm in algorithms]
    df_curves = pd.DataFrame({
        algo_list[0]: pd.Series(curves[0]),
        algo_list[1]: pd.Series(curves[1]),
        algo_list[2]: pd.Series(curves[2]),
        algo_list[3]: pd.Series(curves[3])
    })
    df_curves.fillna(method='ffill', inplace=True)
    timestr = time.strftime("%Y%m%d-%H%M%S")
    df_curves.to_csv(f'./output/df_curves_{timestr}.csv', index=False)
    ax = df_curves.plot()
    ax.set_xlabel("Iteration")
    ax.set_ylabel("Best Fitness")
    timestr = time.strftime("%Y%m%d-%H%M%S")
    plt.savefig('./graphs/' + 'Credit_All_Algorithms' + '_' + str(timestr) +
                '.png')