コード例 #1
0
ファイル: RunGenericOL.py プロジェクト: viniciusmesquitac/RNA
def main():
    stream = open('configurations/runConfigurations.yml', 'r', encoding='utf-8').read()
    settings = yaml.load(stream=stream, Loader=yaml.FullLoader)
    neuron_type = settings['neuron_type']

    dataset, n_features = datasets.load_multiclass_base(settings['chosen_base'])
    n_labels = len(list(set(dataset[:, -1])))
    epochs = settings['epochs']
    learning_rate = settings['learning_rate']
    realizations = settings['realizations']
    scenarios = []

    total_time = 0
    for i in range(realizations):
        start = time.time()
        training_base, test_base = train_test_split(dataset, test_size=settings['test_size'],stratify=dataset[:, -1])
        training_base = dataset_utils.binarize_labels(training_base, neuron_type)
        test_base = dataset_utils.binarize_labels(test_base, neuron_type)

        one_layer_net = olp.GenericOLN(n_labels=n_labels, n_weights=n_features + 1, neuron_type=neuron_type)
        one_layer_net.new_training(epochs=epochs, learning_rate=learning_rate, training_base=training_base)
        hit_rate, predict = one_layer_net.hit_rate(test_base)

        end = time.time()
        total_time += (end - start)
        print('Realization {}, Hit rate:{}%.'.format(i+1, round(hit_rate, 1)))
        scenarios.append({'hit_rate': hit_rate,
                          'ol_net': deepcopy(one_layer_net),
                          'training_base': training_base,
                          'test_base': test_base})
        del one_layer_net

    print('\n--------Statistics---------')
    print('Mean execution time: {}'.format(total_time / realizations))
    best_realization = log_utils.choose_realization(scenarios, settings['criterion_choiced'])
    plt_ut.plot_epochs_error(realization=best_realization, chosen_base=settings['chosen_base'], neuron_type=neuron_type)
    plt_ut.plot_decision_region_mult(realization=best_realization, n_classes=n_labels, choosen_base=settings['chosen_base'], neuron_type=neuron_type)
コード例 #2
0
def cross_validation(splits, n_hidden_neurons, base, n_features, n_classes, chosen_base, model='MLP'):
    print('Cross Validation init:')

    stream = open('configurations/runConfigurations.yml', 'r', encoding='utf-8').read()
    settings = yaml.load(stream=stream, Loader=yaml.FullLoader)
    log_file = open('grid_search_log/{}_{}_log.txt'.format(model, chosen_base), 'w')

    folds = KFold(n_splits=splits)
    np.random.shuffle(base)
    grid_search_base = base[0:int(round((1-settings['test_size'])*base.shape[0])), :]
    n_output_neurons = 1 if n_classes == 2 else n_classes

    if n_classes > 2:
        grid_search_base = dataset_utils.binarize_labels(grid_search_base)
    accuracy = []
    for n in range(len(n_hidden_neurons)):
        realization = 0
        hit_rates = []
        print('Number of hidden neurons:{}'.format(n_hidden_neurons[n]))
        log_file.write('Number of hidden neurons:{}\n'.format(n_hidden_neurons[n]))
        for train_index, test_index in folds.split(grid_search_base):
            training_base = grid_search_base[train_index, :]
            test_base = grid_search_base[test_index, :]
            mlp = gmn.MLP(2, [n_hidden_neurons[n], n_output_neurons], n_features)
            mlp.training(training_base, epochs=settings['epochs'],initial_learning_rate=settings['initial_learning_rate'],
                         final_learning_rate=settings['final_learning_rate'])
            predicted_labels, activations = mlp.predict(test_base)
            hit = hit_rate(predicted_labels, test_base[:, n_features + 1:], activations)
            print('\tHit rate for realization {}: {}%'.format(realization + 1, hit))
            log_file.write('\tHit rate for realization {}: {}%\n'.format(realization + 1, hit))
            hit_rates.append(hit)
            realization += 1
            del mlp
        accuracy.append(np.mean(hit_rates))
        print('\tAccuracy with {} hidden neurons: {}%'.format(n_hidden_neurons[n], round(accuracy[n], 2)))
        log_file.write('\tAccuracy with {} hidden neurons: {}%\n'.format(n_hidden_neurons[n], round(accuracy[n], 2)))

    print('Cross Validation end.')
    log_file.close()
    best_realization = max(accuracy)
    return n_hidden_neurons[accuracy.index(best_realization)]
コード例 #3
0
def main(chosen_base):
    stream = open('configurations/runConfigurations.yml',
                  'r',
                  encoding='utf-8').read()
    settings = yaml.load(stream=stream, Loader=yaml.FullLoader)

    dataset, n_features = datasets.load_multiclass_base(chosen_base)
    n_labels = len(list(set(dataset[:, -1])))
    n_output_neurons = 1 if n_labels == 2 else n_labels

    epochs = settings['epochs']
    initial_learning_rate = settings['initial_learning_rate']
    final_learning_rate = settings['final_learning_rate']
    realizations = settings['realizations']

    total_execution_init = time.time()
    n_neurons = model_utils.cross_validation(5, [5, 7, 9, 11, 13],
                                             deepcopy(dataset), n_features,
                                             n_labels, chosen_base)
    execution_log = open(
        'execution_logs/MLP_{}_{}_neurons.txt'.format(chosen_base, n_neurons),
        'w')
    scenarios = []
    hit_rates = []
    execution_log.write('Base escolhida: {}\n'.format(chosen_base))
    print('Número de neurônios escohidos: {}.'.format(n_neurons))
    for i in range(realizations):
        training_base, test_base = train_test_split(
            dataset, test_size=settings['test_size'], stratify=dataset[:, -1])
        if n_labels > 2:
            training_base = dataset_utils.binarize_labels(training_base)
            test_base = dataset_utils.binarize_labels(test_base)
        start = time.time()
        mlp_test = MLP(n_layers=2,
                       n_neurons=[n_neurons, n_output_neurons],
                       n_features=n_features)
        mlp_test.training(training_base,
                          epochs,
                          initial_learning_rate=initial_learning_rate,
                          final_learning_rate=final_learning_rate)
        end = time.time()
        total_time = (end - start)
        predicted_labels, activations = mlp_test.predict(test_base)
        hit_rate = model_utils.hit_rate(predicted_labels,
                                        test_base[:, n_features + 1:],
                                        activations)
        scenarios.append({
            'hit_rate': hit_rate,
            'ol_net': deepcopy(mlp_test),
            'training_base': training_base,
            'test_base': test_base,
            'predict': predicted_labels
        })
        hit_rates.append(hit_rate)
        print('Realização: {} | Tempo de treinamento:{} | Taxa de acerto: {}%'.
              format(i + 1, round(total_time, 2), hit_rate))
        print(
            '----------------------------------------------------------------------'
        )
        execution_log.write(
            'Realização: {} | Tempo de treinamento:{} | Taxa de acerto: {}%\n'.
            format(i + 1, round(total_time, 2), hit_rate))
        execution_log.write(
            '----------------------------------------------------------------------\n'
        )

    execution_log.write('Acurácia: {}%\nDesvio Padrão:{}\n'.format(
        round(float(np.mean(hit_rates)), 2), round(float(np.std(hit_rates)),
                                                   2)))
    best_realization = log_utils.choose_realization(
        scenarios, settings['criterion_choiced'])
    total_execution_final = time.time()
    plot_utils.plot_conf_matrix(
        predict=best_realization['predict'],
        desired_label=best_realization['test_base'][:, n_features + 1:],
        chosen_base=chosen_base,
        n_labels=n_labels,
        model='MLP')

    print('Tempo de execução total:{} segundos'.format(
        round(total_execution_final - total_execution_init)))
    execution_log.write('Tempo de execução total:{} segundos'.format(
        round(total_execution_final - total_execution_init)))
    execution_log.close()