Exemple #1
0
def train_model(network: Jordan, dataset: BaseDataset,
                n_epochs: int) -> List[float]:
    """
    Performs model training

    :param network: network to train
    :param dataset: dataset for training
    :param n_epochs: number of  epochs we want to train
    :return: list of average epoch errors
    """

    tqdm_epochs = tqdm(range(n_epochs), postfix=f'Epochs...')

    total_error_list = list()

    for _ in tqdm_epochs:
        errors_epoch_list = list()

        for input_values, true_prediction in dataset:
            result = network.propagate_forward(x=input_values)

            error = network.propagate_backward(target=true_prediction)

            errors_epoch_list.append(error)

        average_error = sum(errors_epoch_list) / len(errors_epoch_list)

        tqdm_epochs.set_postfix(
            text=
            f'Epochs... Average error: {sum(errors_epoch_list) / len(errors_epoch_list):.2f}'
        )

        total_error_list.append(average_error)

    return total_error_list
Exemple #2
0
def train_eval_factorial(verbose: bool = False) -> Tuple[float, List[float]]:
    """
    Trains jordan model on factorial sequence and evaluates it.
    Sequence example: [1, 1, 2, 6, 24, 120, 720, ...]

    :param verbose: if verbose draws plots
    :return: accuracy og the model
    """

    dataset = FactorialDataset(
        number_of_precalculated_values=Config.num_of_precalculated_values,
        number_of_input_elements=Config.num_of_input_elements)

    in_features = dataset.number_of_input_elements
    out_features = 1

    network = Jordan(
        lr=Config.learning_rate,
        momentum=Config.momentum,
        shape=[in_features, Config.num_of_hidden_neurons, out_features])

    errors_list = train_model(network=network,
                              dataset=dataset,
                              n_epochs=Config.num_epochs)
    accuracy = eval_model(network=network, dataset=dataset)

    if verbose:
        draw_error_plot(errors_list=errors_list,
                        title='Jordan model on factorial sequence errors')

    return accuracy, errors_list
Exemple #3
0
def train_model_min_error(network: Jordan,
                          dataset: BaseDataset,
                          n_epochs: int,
                          min_error: float,
                          verbose: bool = True) -> List[float]:
    """
    Performs model training

    :param network: network to train
    :param dataset: dataset for training
    :param n_epochs: number of  epochs we want to train
    :param min_error: error we want to reach
    :param verbose: if True shows progress bar
    :return: list of average epoch errors
    """

    tqdm_epochs = tqdm(range(n_epochs),
                       postfix=f'Epochs...',
                       disable=not verbose)

    total_error_list = list()

    for _ in tqdm_epochs:
        errors_epoch_list = list()

        for input_values, true_prediction in dataset:
            result = network.propagate_forward(x=input_values)

            error = network.propagate_backward(target=true_prediction)

            errors_epoch_list.append(error)

        average_error = sum(errors_epoch_list) / len(errors_epoch_list)

        if average_error <= min_error:
            break

        tqdm_epochs.set_postfix(
            text=
            f'Epochs... Average error: {sum(errors_epoch_list) / len(errors_epoch_list):.2f}'
        )

        total_error_list.append(average_error)

    return total_error_list
Exemple #4
0
def perform_pipeline_for_plots():
    config = Config()

    data_mapping: Dict = {
        'factorial':
        generate_factorial_values(
            number_of_precalculated_values=config.num_of_precalculated_values),
        'fibonacci':
        generate_fibonacci_values(
            number_of_precalculated_values=config.num_of_precalculated_values),
        'period':
        generate_period_values(
            number_of_precalculated_values=config.num_of_precalculated_values),
        'exponent':
        generate_exp_values(
            number_of_precalculated_values=config.num_of_precalculated_values),
        'custom':
        config.data
    }

    dataset = CustomDataset(
        data=data_mapping[config.data_type],
        number_of_input_elements=config.num_of_input_elements,
        number_of_output_elements=config.num_of_output_elements)

    in_features = dataset.number_of_input_elements
    out_features = dataset.number_of_output_elements

    network = Jordan(
        lr=config.learning_rate,
        momentum=config.momentum,
        make_zero_context=config.make_zero_context,
        shape=[in_features, config.num_of_hidden_neurons, out_features])

    errors_list = train_model_min_error(network=network,
                                        dataset=dataset,
                                        n_epochs=Config.num_epochs,
                                        min_error=Config.min_error,
                                        verbose=True)

    return errors_list
Exemple #5
0
def eval_model(network: Jordan, dataset: BaseDataset):
    results_array = list()

    for input_values, true_prediction in dataset:
        result = network.propagate_forward(x=input_values)
        result = result.tolist()

        result = [int(round(curr_result)) for curr_result in result]

        try:
            print(f'Input: {input_values}, Pred num: {result}. True: {true_prediction}')

            if true_prediction == result:
                results_array.append(1)
            else:
                results_array.append(0)
        except ValueError as err:
            results_array.append(0)

    resulting_accuracy = sum(results_array) / len(results_array)

    return resulting_accuracy