def main(train=False):
    digit_arrays = {
        str(digit): [
            load_image(f"{BASE_PATH}{digit}_{index}.png")
            for index in range(HOW_MANY)
        ]
        for digit in range(DIGITS)
    }

    flat_arrays = {
        digit: list(map(image_to_bipolar_array, images))
        for digit, images in digit_arrays.items()
    }

    learning_rate = 0.0005
    network = Adaline(labels=list(digit_arrays.keys()),
                      learning_rate=learning_rate)

    if train:
        for label, sources in flat_arrays.items():
            for source in sources:
                network.add_sources(source, label)

        max_cycles = 200
        random_starting_weights = False
        weight_adjustment_tolerance = None
        square_error_tolerance = None

        print(f"Max cycles: {max_cycles}\n--------------------------")
        network.train(random_starting_weights=random_starting_weights,
                      max_cycles=max_cycles,
                      weight_adjustment_tolerance=weight_adjustment_tolerance,
                      square_error_tolerance=square_error_tolerance,
                      verbose=True)

        network.save_neurons('neurons.pkl')

    else:
        network.load_neurons('neurons.pkl')

    while True:
        # test_image = draw.get_character()
        test_image = load_image(input("image name:\n>>"))

        if test_image is None:
            break

        flat = image_to_bipolar_array(test_image)
        out = network.output(flat)

        for key, value in out.items():
            print(f"{key}: {value:.3f}")
Esempio n. 2
0
def main():
    sources_list = [[1, 1], [-1, 1], [1, -1], [-1, -1]]
    targets = [1, 1, 1, -1]

    square_errors = {}

    learning_rates = [1e-4, 1e-3, 1e-2, 1e-1, 2e-1, 3e-1, 0.35, 0.4]
    weight_adjustment_tolerance = None
    square_error_tolerance = None
    max_cycles = 50

    network = Adaline(activation_function=activation_function)
    for sources, target in zip(sources_list, targets):
        network.add_sources(sources, target)

    for learning_rate in learning_rates:
        network.learning_rate = learning_rate

        network.train(random_starting_weights=False,
                      max_cycles=max_cycles,
                      weight_adjustment_tolerance=weight_adjustment_tolerance,
                      square_error_tolerance=square_error_tolerance)

        print(
            f">>Learning rate: {learning_rate}\n\n"
            f"Final weights:\n"
            f"{[float(f'{weigth:.5f}') for weigth in network.neuron.weights]}\n"
            f"Final bias:\n"
            f"{network.neuron.bias:.5f}\n\n"
            f"Cycles: {network.cycles}\n"
            f"Final square error: {network.total_square_error_by_cycle[-1]:.5f}\n\n\n"
        )

        square_errors[learning_rate] = network.total_square_error_by_cycle

    curves = []
    for learning_rate, square_error in square_errors.items():
        curves.append(
            plt.plot(range(len(square_error)),
                     square_error,
                     '--',
                     linewidth=2,
                     label=str(learning_rate))[0])
    plt.ylim([-0.1, 4])
    plt.legend(handles=curves)
    plt.show()
def main():
    xs = [0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0]
    ys = [2.26, 3.8, 4.43, 5.91, 6.18, 7.26, 8.15, 9.14, 10.87, 11.58, 12.55]

    a_regression, b_regression, correlation_coefficient, _, regression_standard_error = linregress(
        xs, ys)
    regression_equation = f"y={a_regression:.5f}*x+{b_regression:.5f}"
    ys_regression = [a_regression * x + b_regression for x in xs]

    determination_coefficient = correlation_coefficient**2

    print(f"Regression equation: {regression_equation}")
    print(f"r = {correlation_coefficient:.5f}")
    print(f"r² = {determination_coefficient:.5f}")
    print(f"σ = {regression_standard_error:.5f}\n")

    learning_rate = 0.0015
    list_max_cycles = [100, 200, 500, 1000]
    random_starting_weights = False
    weight_adjustment_tolerance = None
    square_error_tolerance = None

    network = Adaline(learning_rate=learning_rate)
    for source, target in zip(xs, ys):
        network.add_sources([source], target)

    adaline_plots = []

    for max_cycles in list_max_cycles:
        print(f"Max cycles: {max_cycles}\n--------------------------")
        network.train(random_starting_weights=random_starting_weights,
                      max_cycles=max_cycles,
                      weight_adjustment_tolerance=weight_adjustment_tolerance,
                      square_error_tolerance=square_error_tolerance,
                      verbose=False)

        a_adaline, b_adaline = network.neuron.weights[0], network.neuron.bias
        adaline_equation = f"y={a_adaline:.5f}*x+{b_adaline:.5f}"
        ys_adaline = [a_adaline * x + b_adaline for x in xs]

        total_square_error = sum([(y - y_line)**2
                                  for y, y_line in zip(ys, ys_adaline)])

        adaline_standard_error = (total_square_error / len(ys))**0.5

        print(f"Adaline equation: {adaline_equation}\n")

        print(
            f"Difference for a coefficient: {abs(a_adaline - a_regression):.5f}"
        )
        print(
            f"Difference for b coefficient: {abs(b_adaline - b_regression):.5f}"
        )
        print(f"σ = {adaline_standard_error}\n-----------------------\n")

        adaline_plots.append(
            plt.plot(xs,
                     ys_adaline,
                     linestyle='--',
                     linewidth=3,
                     label=f"Cycles: {max_cycles}",
                     zorder=1)[0])

    regression_plot, = plt.plot(xs,
                                ys_regression,
                                color='blue',
                                linestyle='-',
                                linewidth=5,
                                label=f"Regression: {regression_equation}",
                                zorder=0)

    scatter_plot = plt.scatter(xs,
                               ys,
                               color='black',
                               marker='x',
                               s=80,
                               label='Source points',
                               zorder=2)
    plt.legend(handles=[scatter_plot, *adaline_plots, regression_plot])

    plt.show()