def run_hf_demo(pattern_size=4, nr_random_patterns=3, reference_pattern=0,
                initially_flipped_pixels=3, nr_iterations=6, random_seed=None):
    """
    Simple demo.

    Args:
        pattern_size:
        nr_random_patterns:
        reference_pattern:
        initially_flipped_pixels:
        nr_iterations:
        random_seed:

    Returns:

    """
    # instantiate a hofpfield network
    hopfield_net = network.HopfieldNetwork(pattern_size**2)

    # for the demo, use a seed to get a reproducible pattern
    np.random.seed(random_seed)

    # instantiate a pattern factory
    factory = pattern_tools.PatternFactory(pattern_size, pattern_size)
    # create a checkerboard pattern and add it to the pattern list
    checkerboard = factory.create_checkerboard()
    pattern_list = [checkerboard]
    # add random patterns to the list
    pattern_list.extend(factory.create_random_pattern_list(nr_random_patterns, on_probability=0.5))
    hfplot.plot_pattern_list(pattern_list)
    # let the hopfield network "learn" the patterns. Note: they are not stored
    # explicitly but only network weights are updated !
    hopfield_net.store_patterns(pattern_list)

    # how similar are the random patterns? Check the overlaps
    overlap_matrix = pattern_tools.compute_overlap_matrix(pattern_list)
    hfplot.plot_overlap_matrix(overlap_matrix)
    # create a noisy version of a pattern and use that to initialize the network
    noisy_init_state = pattern_tools.flip_n(pattern_list[reference_pattern], initially_flipped_pixels)
    hopfield_net.set_state_from_pattern(noisy_init_state)

    # uncomment the following line to enable a PROBABILISTIC network dynamic
    # hopfield_net.set_dynamics_probabilistic_sync(2.5)
    # uncomment the following line to enable an ASYNCHRONOUS network dynamic
    # hopfield_net.set_dynamics_sign_async()

    # run the network dynamics and record the network state at every time step
    states = hopfield_net.run_with_monitoring(nr_iterations)
    # each network state is a vector. reshape it to the same shape used to create the patterns.
    states_as_patterns = factory.reshape_patterns(states)
    # plot the states of the network
    hfplot.plot_state_sequence_and_overlap(states_as_patterns, pattern_list, reference_pattern)
    plt.show()
def run_hf_demo(pattern_size=4, nr_random_patterns=3, reference_pattern=0,
                initially_flipped_pixels=3, nr_iterations=6, random_seed=None):
    """
    Simple demo.

    Args:
        pattern_size:
        nr_random_patterns:
        reference_pattern:
        initially_flipped_pixels:
        nr_iterations:
        random_seed:

    Returns:

    """
    # instantiate a hofpfield network
    hopfield_net = network.HopfieldNetwork(pattern_size**2)

    # for the demo, use a seed to get a reproducible pattern
    np.random.seed(random_seed)

    # instantiate a pattern factory
    factory = pattern_tools.PatternFactory(pattern_size, pattern_size)
    # create a checkerboard pattern and add it to the pattern list
    checkerboard = factory.create_checkerboard()
    pattern_list = [checkerboard]
    # add random patterns to the list
    pattern_list.extend(factory.create_random_pattern_list(nr_random_patterns, on_probability=0.5))
    hfplot.plot_pattern_list(pattern_list)
    # let the hopfield network "learn" the patterns. Note: they are not stored
    # explicitly but only network weights are updated !
    hopfield_net.store_patterns(pattern_list)

    # how similar are the random patterns? Check the overlaps
    overlap_matrix = pattern_tools.compute_overlap_matrix(pattern_list)
    hfplot.plot_overlap_matrix(overlap_matrix)
    # create a noisy version of a pattern and use that to initialize the network
    noisy_init_state = pattern_tools.flip_n(pattern_list[reference_pattern], initially_flipped_pixels)
    hopfield_net.set_state_from_pattern(noisy_init_state)

    # uncomment the following line to enable a PROBABILISTIC network dynamic
    # hopfield_net.set_dynamics_probabilistic_sync(2.5)
    # uncomment the following line to enable an ASYNCHRONOUS network dynamic
    # hopfield_net.set_dynamics_sign_async()

    # run the network dynamics and record the network state at every time step
    states = hopfield_net.run_with_monitoring(nr_iterations)
    # each network state is a vector. reshape it to the same shape used to create the patterns.
    states_as_patterns = factory.reshape_patterns(states)
    # plot the states of the network
    hfplot.plot_state_sequence_and_overlap(states_as_patterns, pattern_list, reference_pattern)
    plt.show()
Beispiel #3
0
# create a checkerboard pattern and add it to the pattern list
checkerboard = factory.create_checkerboard()
pattern_list = [checkerboard]

# add random patterns to the list
pattern_list.extend(
    factory.create_random_pattern_list(nr_patterns=3, on_probability=0.5))
plot_tools.plot_pattern_list(pattern_list)
# how similar are the random patterns and the checkerboard? Check the overlaps
overlap_matrix = pattern_tools.compute_overlap_matrix(pattern_list)
plot_tools.plot_overlap_matrix(overlap_matrix)

# let the hopfield network "learn" the patterns. Note: they are not stored
# explicitly but only network weights are updated !
hopfield_net.store_patterns(pattern_list)

# create a noisy version of a pattern and use that to initialize the network
noisy_init_state = pattern_tools.flip_n(checkerboard, nr_of_flips=4)
hopfield_net.set_state_from_pattern(noisy_init_state)

# from this initial state, let the network dynamics evolve.
states = hopfield_net.run_with_monitoring(nr_steps=4)

# each network state is a vector. reshape it to the same shape used to create the patterns.
states_as_patterns = factory.reshape_patterns(states)
# plot the states of the network
plot_tools.plot_state_sequence_and_overlap(states_as_patterns,
                                           pattern_list,
                                           reference_idx=0,
                                           suptitle="Network dynamics")