Ejemplo n.º 1
0
def NAS_EA_FA(dataset: str):
    cur_time_budget = 0
    history2acc = {}
    best_valid_acc, best_test_acc, times = [0.0], [0.0], [0.0]
    x_train, y_train = [], []
    total_population = [Individual() for _ in range(POPULATION_SIZE)]

    while cur_time_budget <= MAX_TIME_BUDGET:
        population = sorted(total_population, key=lambda x: x.fitness, reverse=True)
        new_population = []
        num = 0
        for individual in population:
            valid_acc, test_acc, time = get_individual_data_from_history(individual, history2acc, dataset)
            individual.fitness = valid_acc
            if time == 0.0:
                continue

            new_population.append(individual)
            x_train.append(get_individual_sequences(individual))
            y_train.append(valid_acc)

            if valid_acc > best_valid_acc[-1]:
                best_valid_acc.append(valid_acc)
                best_test_acc.append(test_acc)
            else:
                best_valid_acc.append(best_valid_acc[-1])
                best_test_acc.append(best_test_acc[-1])
            times.append(time)
            cur_time_budget += time
            num += 1
            if cur_time_budget > MAX_TIME_BUDGET or num >= 5:
                break

        if len(new_population) != 0:
            population = new_population

        gbm = XGBRegressor(eta=0.1)
        gbm.fit(np.array(x_train), np.array(y_train))

        total_population = []
        for epoch in range(10):
            new_population = []
            for i in range(POPULATION_SIZE):
                individual = cp.deepcopy(tournament_selection(population))

                bitwise_mutation(individual)
                individual.fitness = gbm.predict(np.array([get_individual_sequences(individual)]))[0]
                new_population.append(individual)
                total_population.append(individual)
            population = new_population

    return best_valid_acc, best_test_acc, times
Ejemplo n.º 2
0
def random_search(dataset: str):
    cur_time_budget = 0
    best_valid_acc, best_test_acc, times = [0.0], [0.0], [0.0]
    while cur_time_budget <= MAX_TIME_BUDGET:
        individual = Individual()
        valid_acc, test_acc, time = get_individual_data(individual, dataset)
        if valid_acc > best_valid_acc[-1]:
            best_valid_acc.append(valid_acc)
            best_test_acc.append(test_acc)
        else:
            best_valid_acc.append(best_valid_acc[-1])
            best_test_acc.append(best_test_acc[-1])
        times.append(time)
        cur_time_budget += time

    return best_valid_acc, best_test_acc, times
def regularized_evolution_algorithm(dataset: str):
    cur_time_budget = 0
    history2acc = {}
    best_valid_acc, best_test_acc, times = [0.0], [0.0], [0.0]
    population = [Individual() for _ in range(POPULATION_SIZE)]
    for individual in population:
        valid_acc, test_acc, time = get_individual_data_from_history(
            individual, history2acc, dataset)

        individual.fitness = valid_acc
        if valid_acc > best_valid_acc[-1]:
            best_valid_acc.append(valid_acc)
            best_test_acc.append(test_acc)
        else:
            best_valid_acc.append(best_valid_acc[-1])
            best_test_acc.append(best_test_acc[-1])
        times.append(time)
        cur_time_budget += time

    while cur_time_budget <= MAX_TIME_BUDGET:
        individual = cp.deepcopy(tournament_selection(population))
        bitwise_mutation(individual)
        valid_acc, test_acc, time = get_individual_data_from_history(
            individual, history2acc, dataset)

        individual.fitness = valid_acc
        population.append(individual)
        population.pop(0)

        if time == 0.0:
            continue

        if valid_acc > best_valid_acc[-1]:
            best_valid_acc.append(valid_acc)
            best_test_acc.append(test_acc)
        else:
            best_valid_acc.append(best_valid_acc[-1])
            best_test_acc.append(best_test_acc[-1])

        times.append(time)
        cur_time_budget += time
        if cur_time_budget > MAX_TIME_BUDGET:
            break

    return best_valid_acc, best_test_acc, times
def random_search(dataset: str):
    cur_time_budget = 0
    best_valid_acc, best_test_acc, times = [0.0], [0.0], [0.0]
    history_hash = set()
    while cur_time_budget <= MAX_TIME_BUDGET:
        individual = Individual()
        individual_hash = get_individual_hash(individual)
        if individual_hash in history_hash:
            continue
        else:
            history_hash.add(individual_hash)
        valid_acc, test_acc, time = get_individual_data(individual, dataset)
        if valid_acc > best_valid_acc[-1]:
            best_valid_acc.append(valid_acc)
            best_test_acc.append(test_acc)
        else:
            best_valid_acc.append(best_valid_acc[-1])
            best_test_acc.append(best_test_acc[-1])
        times.append(time)
        cur_time_budget += time

    return best_valid_acc, best_test_acc, times
Ejemplo n.º 5
0
def sample_models(n=100):
    models = [Individual() for _ in range(n)]

    return models
Ejemplo n.º 6
0
def NAS_EA_FA_V2(dataset: str):
    cur_time_budget = 0
    history2acc = {}
    best_valid_acc, best_test_acc, times = [0.0], [0.0], [0.0]
    x_train, y_train = [], []
    total_population = [Individual() for _ in range(POPULATION_SIZE)]

    while cur_time_budget <= MAX_TIME_BUDGET:
        population = sorted(total_population, key=lambda x: x.fitness, reverse=True)
        new_population = []
        num = start_index = 0
        for index in range(len(population)):
            individual = population[index]
            valid_acc, test_acc, time = get_individual_data_from_history(individual, history2acc, dataset)
            individual.fitness = valid_acc
            if time == 0.0:
                continue

            new_population.append(individual)
            # x_train.append(get_individual_sequences(individual))
            temp_sequences = get_all_isomorphic_sequences(individual)
            x_train.extend(temp_sequences)
            y_train.extend([valid_acc for _ in range(len(temp_sequences))])

            if valid_acc > best_valid_acc[-1]:
                best_valid_acc.append(valid_acc)
                best_test_acc.append(test_acc)
            else:
                best_valid_acc.append(best_valid_acc[-1])
                best_test_acc.append(best_test_acc[-1])
            times.append(time)
            cur_time_budget += time
            num += 1
            if cur_time_budget > MAX_TIME_BUDGET or num >= 3:
                start_index = index
                break

        # diversity individual
        max_dis_list = [get_min_distance(x_train, get_individual_sequences(indiv)) for indiv in
                        population[start_index + 1:]]
        while num < 5 and cur_time_budget <= MAX_TIME_BUDGET:
            max_dis, temp_index = 0, 0
            for index in range(len(max_dis_list)):
                if max_dis_list[index] > max_dis:
                    max_dis = max_dis_list[index]
                    temp_index = index

            if max_dis == 0:
                break

            individual = population[temp_index + start_index + 1]
            max_dis_list[temp_index] = 0
            valid_acc, test_acc, time = get_individual_data_from_history(individual, history2acc, dataset)
            individual.fitness = valid_acc
            if time == 0.0:
                continue

            new_population.append(individual)
            temp_sequences = get_all_isomorphic_sequences(individual)
            x_train.extend(temp_sequences)
            y_train.extend([valid_acc for _ in range(len(temp_sequences))])

            if valid_acc > best_valid_acc[-1]:
                best_valid_acc.append(valid_acc)
                best_test_acc.append(test_acc)
            else:
                best_valid_acc.append(best_valid_acc[-1])
                best_test_acc.append(best_test_acc[-1])
            times.append(time)
            cur_time_budget += time
            num += 1

        if len(new_population) != 0:
            population = new_population

        gbm = XGBRegressor(eta=0.1)
        gbm.fit(np.array(x_train), np.array(y_train))

        total_population = []
        for epoch in range(10):
            new_population = []
            for i in range(POPULATION_SIZE):
                individual = cp.deepcopy(tournament_selection(population))

                bitwise_mutation(individual)
                individual.fitness = gbm.predict(np.array([get_individual_sequences(individual)]))[0]
                new_population.append(individual)
                total_population.append(individual)
            population = new_population

    return best_valid_acc, best_test_acc, times