Exemple #1
0
def show_one(hopfield_net, rows_no, cols_no, patterns, args):
    # Choose a pattern
    original_pattern = choice(patterns)
    hopfield_net.reset(original_pattern)
    hopfield_net.display_as_matrix(rows_no, cols_no, msg='Original pattern')
    # hopfield_net.display_as_image(rows_no, cols_no)

    # Apply noise to it
    noisy_pattern = apply_noise(original_pattern, args.noise)  # apply noise
    hopfield_net.reset(noisy_pattern)  # The net starts from the noisy pattern
    hopfield_net.display_as_matrix(rows_no, cols_no, msg='Noisy pattern')
    # hopfield_net.display_as_image(rows_no, cols_no)

    # Let the network reach an energetic mimimum
    hopfield_net.convergence_test = False
    while not hopfield_net.is_energy_minimal():
        hopfield_net.single_update()
        hopfield_net.display_as_matrix(rows_no, cols_no, msg='After update')
        print("Energy: %f" % hopfield_net.energy())

    # hopfield_net.display_as_image(rows_no, cols_no)

    if original_pattern == HopfieldNetwork.state_to_string(hopfield_net.state):
        print("Recovered OK")
    else:
        print("NOT RECOVERED OK")

    print("Done!")
Exemple #2
0
def test_hopfield(hopfield_net, patterns, args):
    ########################## TASK 2: Testarea retelei #######################
    # args.test_no - numarul de sabloane ce vor fi testate
    # intoarce acuratetea
    count_correct = 0

    for original_pattern in patterns:
        hopfield_net.reset(original_pattern)
        hopfield_net.display_as_matrix(rows_no,
                                       cols_no,
                                       msg='Original pattern')
        # hopfield_net.display_as_image(rows_no, cols_no)

        # Apply noise to it
        noisy_pattern = apply_noise(original_pattern,
                                    args.noise)  # apply noise
        hopfield_net.reset(
            noisy_pattern)  # The net starts from the noisy pattern
        hopfield_net.display_as_matrix(rows_no, cols_no, msg='Noisy pattern')
        # hopfield_net.display_as_image(rows_no, cols_no)

        # Let the network reach an energetic mimimum
        hopfield_net.convergence_test = False
        while not hopfield_net.is_energy_minimal():
            hopfield_net.single_update()
            hopfield_net.display_as_matrix(rows_no,
                                           cols_no,
                                           msg='After update')
            print("Energy: %f" % hopfield_net.energy())

        # hopfield_net.display_as_image(rows_no, cols_no)
        if original_pattern == HopfieldNetwork.state_to_string(
                hopfield_net.state):
            count_correct += 1
            print("Recovered OK")

    return count_correct / len(patterns)