def get_accuracies(file_to_print):
    logger = logging.getLogger(CONFIG['experiment_name'])
    configure_logger_by_default(logger)
    logger.info("START " + CONFIG['experiment_name'])

    def print_info(info):
        logger.info(info)
        print(info)
        file_to_print.write(info + "\n")

    CONFIG['pso_options']['print_info'] = print_info

    train_index, test_index = next(CONFIG['cv'].split(INPUT_FEATURES,
                                                      INPUT_LABELS))
    train_features, train_labels = INPUT_FEATURES[train_index], INPUT_LABELS[
        train_index]
    test_features, test_labels = INPUT_FEATURES[test_index], INPUT_LABELS[
        test_index]
    scaler = preprocessing.StandardScaler().fit(train_features)
    train_features = scaler.transform(train_features)
    test_features = scaler.transform(test_features)

    train_features = torch.from_numpy(train_features)
    train_labels = torch.from_numpy(train_labels)
    test_features = torch.from_numpy(test_features)
    test_labels = torch.from_numpy(test_labels)

    train_accuracies = []
    test_accuracies = []
    hidden_dims = []
    for hidden_layer_percentage in CONFIG['hidden_layer_percentage']:
        hidden_dim = int(CONFIG['max_hidden_layer_size'] *
                         hidden_layer_percentage)
        model = Model(hidden_dim=hidden_dim)
        test_acc, train_acc = train_bp(model, train_features, train_labels,
                                       test_features, test_labels, CONFIG)
        train_accuracies.append(train_acc)
        test_accuracies.append(test_acc)
        hidden_dims.append(hidden_dim)
    print_info("======RESULTING ACCURACIES======")
    print_info("TRAIN: " + str(train_accuracies))
    print_info("TEST: " + str(test_accuracies))
    print_info("HIDDEN DIMS: " + str(hidden_dims))
    logger.info("END " + CONFIG['experiment_name'])
    return train_accuracies, test_accuracies, hidden_dims
Exemplo n.º 2
0
def get_accuracies(file_to_print):
    logger = logging.getLogger(CONFIG['experiment_name'])
    configure_logger_by_default(logger)
    logger.info("START " + CONFIG['experiment_name'])

    def print_info(info):
        logger.info(info)
        print(info)
        file_to_print.write(info + "\n")

    CONFIG['pso_options']['print_info'] = print_info
    full_train_index, test_index = next(CONFIG['cv'].split(
        INPUT_FEATURES, INPUT_LABELS))
    np.random.shuffle(full_train_index)
    train_accuracies = []
    test_accuracies = []
    for train_percentage in CONFIG['train_splits']:
        train_size = int(train_percentage * full_train_index.shape[0])
        train_index = full_train_index[0:train_size]
        train_features, train_labels = INPUT_FEATURES[
            train_index], INPUT_LABELS[train_index]
        test_features, test_labels = INPUT_FEATURES[test_index], INPUT_LABELS[
            test_index]
        scaler = preprocessing.StandardScaler().fit(train_features)
        train_features = scaler.transform(train_features)
        test_features = scaler.transform(test_features)

        train_features = torch.from_numpy(train_features)
        train_labels = torch.from_numpy(train_labels)
        test_features = torch.from_numpy(test_features)
        test_labels = torch.from_numpy(test_labels)

        model = Model()
        test_acc, train_acc = train_bp(model, train_features, train_labels,
                                       test_features, test_labels, CONFIG)
        train_accuracies.append(train_acc)
        test_accuracies.append(test_acc)
    print_info("======RESULTING ACCURACIES======")
    print_info("TRAIN: " + str(train_accuracies))
    print_info("TEST: " + str(test_accuracies))
    logger.info("END " + CONFIG['experiment_name'])
    return train_accuracies, test_accuracies
Exemplo n.º 3
0
def fit_model(file_to_print):
    logger = logging.getLogger('one_split_fit')
    configure_logger_by_default(logger)
    logger.info("START fit_model")

    def print_info(info):
        logger.info(info)
        print(info)
        file_to_print.write(info + "\n")

    CONFIG['pso_options']['print_info'] = print_info
    train_index, test_index = next(CONFIG['cv'].split(INPUT_FEATURES,
                                                      INPUT_LABELS))
    train_features, train_labels = INPUT_FEATURES[train_index], INPUT_LABELS[
        train_index]
    test_features, test_labels = INPUT_FEATURES[test_index], INPUT_LABELS[
        test_index]
    scaler = preprocessing.StandardScaler().fit(train_features)
    train_features = scaler.transform(train_features)
    test_features = scaler.transform(test_features)

    train_features = torch.from_numpy(train_features)
    train_labels = torch.from_numpy(train_labels)
    test_features = torch.from_numpy(test_features)
    test_labels = torch.from_numpy(test_labels)

    model = Model()
    for trainer_type in CONFIG['trainers_to_use']:
        if trainer_type == 'bp':
            test_acc, train_acc, test_loss, train_loss = \
                train_bp(model, train_features, train_labels, test_features, test_labels, CONFIG)
            print_info("Best Test Accuracy: " + str(test_acc) +
                       ", Train Accuracy: " + str(train_acc) +
                       ", Test Loss: " + str(test_loss) + ", Train Loss: " +
                       str(train_loss))
        elif trainer_type == 'pso':
            train_pso(model, train_features, train_labels, test_features,
                      test_labels, CONFIG)
    logger.info("END fit_model")
Exemplo n.º 4
0
def run_cross_validation_psobp(file_to_print) -> torch.Tensor:
    logger = logging.getLogger('10_fold_cv')
    configure_logger_by_default(logger)
    logger.info("START run_cross_validation")

    def print_info(info):
        logger.info(info)
        print(info)
        file_to_print.write(info + "\n")

    accuracies = torch.zeros((CONFIG['n_splits'], CONFIG['n_repeats']))
    fold_number = -1
    repeat_number = -1
    CONFIG['pso_options']['print_info'] = print_info

    for train_index, test_index in CONFIG['cv'].split(INPUT_FEATURES,
                                                      INPUT_LABELS):
        fold_number += 1
        if fold_number % CONFIG['n_splits'] == 0:
            fold_number = 0
            repeat_number += 1

        def print_info(info):
            logger.info(info)
            print(info)
            file_to_print.write(info + "\n")

        print_info("New " + str(fold_number) + " fold. repeat = " +
                   str(repeat_number))
        train_features, train_labels = INPUT_FEATURES[
            train_index], INPUT_LABELS[train_index]
        test_features, test_labels = INPUT_FEATURES[test_index], INPUT_LABELS[
            test_index]

        scaler = preprocessing.StandardScaler().fit(train_features)
        train_features = scaler.transform(train_features)
        test_features = scaler.transform(test_features)

        train_features = torch.from_numpy(train_features)
        train_labels = torch.from_numpy(train_labels)
        test_features = torch.from_numpy(test_features)
        test_labels = torch.from_numpy(test_labels)

        model = Model().to(CONFIG['device'])

        best_accuracy_bp, best_accuracy_pso = None, None
        for trainer_type in CONFIG['trainers_to_use']:
            if trainer_type == 'bp':
                best_accuracy_bp, _ = train_bp(model, train_features,
                                               train_labels, test_features,
                                               test_labels, CONFIG)
            elif trainer_type == 'pso':
                best_accuracy_pso = train_pso(model, train_features,
                                              train_labels, test_features,
                                              test_labels, CONFIG)
        print_info('Best accuracy pso: ' + str(best_accuracy_pso) + ', bp: ' +
                   str(best_accuracy_bp))
        best_accuracy = max(best_accuracy_bp, best_accuracy_pso)
        accuracies[fold_number][repeat_number] = best_accuracy
        print_info('Best final accuracy: ' + str(best_accuracy))
        print_info('END OF EVALUATION OF ' + str(fold_number) +
                   ' FOLD, REPEAT ' + str(repeat_number))
        print_info(
            '========================================================================='
        )
    logger.info("END run_cross_validation")
    return accuracies