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_alphabet(letters, initialization_noise_level=0.2, random_seed=None): """ Simple demo Args: letters: initialization_noise_level: random_seed: Returns: """ # fixed size 10 for the alphabet. pattern_size = 10 # pick some letters we want to store in the network if letters is None: letters = ['A', 'B', 'C', 'R', 'S', 'X', 'Y', 'Z'] reference_pattern = 0 # 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) # load the dictionary abc_dict = pattern_tools.load_alphabet() # for each key in letters, append the pattern to the list pattern_list = [abc_dict[key] for key in letters] hfplot.plot_pattern_list(pattern_list) hopfield_net.store_patterns(pattern_list) hopfield_net.set_state_from_pattern( pattern_tools.get_noisy_copy(abc_dict[letters[reference_pattern]], initialization_noise_level)) states = hopfield_net.run_with_monitoring(6) state_patterns = pattern_tools.reshape_patterns(states, pattern_list[0].shape) hfplot.plot_state_sequence_and_overlap(state_patterns, pattern_list, reference_pattern) plt.show()
def run_user_function_demo(): def upd_random(state_s0, weights): nr_neurons = len(state_s0) random_neuron_idx_list = np.random.permutation(int(len(state_s0) / 2)) state_s1 = state_s0.copy() for i in range(len(random_neuron_idx_list)): state_s1[i] = -1 if (np.random.rand() < .5) else +1 return state_s1 hopfield_net = network.HopfieldNetwork(6**2) hopfield_net.set_dynamics_to_user_function(upd_random) # for the demo, use a seed to get a reproducible pattern # instantiate a pattern factory factory = pattern_tools.PatternFactory(6, 6) # 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(4, 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) hopfield_net.set_state_from_pattern(pattern_list[0]) # 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(5) # 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, 0) plt.show()
def run_user_function_demo(): def upd_random(state_s0, weights): nr_neurons = len(state_s0) random_neuron_idx_list = np.random.permutation(int(len(state_s0)/2)) state_s1 = state_s0.copy() for i in range(len(random_neuron_idx_list)): state_s1[i] = -1 if (np.random.rand() < .5) else +1 return state_s1 hopfield_net = network.HopfieldNetwork(6**2) hopfield_net.set_dynamics_to_user_function(upd_random) # for the demo, use a seed to get a reproducible pattern # instantiate a pattern factory factory = pattern_tools.PatternFactory(6, 6) # 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(4, 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) hopfield_net.set_state_from_pattern(pattern_list[0]) # 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(5) # 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, 0) plt.show()
# 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")