neurodynex.hopfield_network package¶
Submodules¶
neurodynex.hopfield_network.demo module¶
-
neurodynex.hopfield_network.demo.
run_hf_demo
(pattern_size=4, nr_random_patterns=3, reference_pattern=0, initially_flipped_pixels=3, nr_iterations=6, random_seed=None)[source]¶ Simple demo.
Parameters: - pattern_size –
- nr_random_patterns –
- reference_pattern –
- initially_flipped_pixels –
- nr_iterations –
- random_seed –
Returns:
neurodynex.hopfield_network.network module¶
This file implements a Hopfield network. It provides functions to set and retrieve the network state, store patterns.
- Relevant book chapters:
-
class
neurodynex.hopfield_network.network.
HopfieldNetwork
(nr_neurons)[source]¶ Implements a Hopfield network.
-
nrOfNeurons
¶ int – Number of neurons
-
weights
¶ numpy.ndarray – nrOfNeurons x nrOfNeurons matrix of weights
-
state
¶ numpy.ndarray – current network state. matrix of shape (nrOfNeurons, nrOfNeurons)
-
run
(nr_steps=5)[source]¶ Runs the dynamics.
Parameters: nr_steps (float, optional) – Timesteps to simulate
-
run_with_monitoring
(nr_steps=5)[source]¶ Iterates at most nr_steps steps. records the network state after every iteration
Parameters: nr_steps – Returns: a list of 2d network states
-
set_dynamics_sign_async
()[source]¶ Sets the update dynamics to the g(h) = sign(h) functions. Neurons are updated asynchronously: In random order, all neurons are updated sequentially
-
set_dynamics_sign_sync
()[source]¶ sets the update dynamics to the synchronous, deterministic g(h) = sign(h) function
-
set_dynamics_to_user_function
(update_function)[source]¶ Sets the network dynamics to the given update function
Parameters: update_function – upd(state_t0, weights) -> state_t1. Any function mapping a state s0 to the next state s1 using a function of s0 and weights.
-
neurodynex.hopfield_network.pattern_tools module¶
Functions to create 2D patterns. Note, in the hopfield model, we define patterns as vectors. To make the exercise more visual, we use 2D patterns (N by N ndarrays).
-
class
neurodynex.hopfield_network.pattern_tools.
PatternFactory
(pattern_length, pattern_width=None)[source]¶ Creates square patterns of size pattern_length x pattern_width If pattern length is omitted, square patterns are produced
-
create_L_pattern
(l_width=1)[source]¶ creates a pattern with column 0 (left) and row n (bottom) set to +1. Increase l_width to set more columns and rows (default is 1)
Parameters: l_width (int) – nr of rows and columns to set Returns: an L shaped pattern.
-
create_checkerboard
()[source]¶ creates a checkerboard pattern of size (pattern_length x pattern_width) :returns: checkerboard pattern
-
create_random_pattern
(on_probability=0.5)[source]¶ Creates a pattern_length by pattern_width 2D random pattern :param on_probability:
Returns: a new random pattern
-
create_random_pattern_list
(nr_patterns, on_probability=0.5)[source]¶ Creates a list of nr_patterns random patterns :param nr_patterns: length of the new list :param on_probability:
Returns: a list of new random patterns of size (pattern_length x pattern_width)
-
create_row_patterns
(nr_patterns=None)[source]¶ creates a list of n patterns, the i-th pattern in the list has all states of the i-th row set to active. This is convenient to create a list of orthogonal patterns which are easy to visually identify
Parameters: nr_patterns – Returns: list of orthogonal patterns
-
-
neurodynex.hopfield_network.pattern_tools.
compute_overlap
(pattern1, pattern2)[source]¶ compute overlap
Parameters: - pattern1 –
- pattern2 –
Returns: Overlap between pattern1 and pattern2
-
neurodynex.hopfield_network.pattern_tools.
compute_overlap_list
(reference_pattern, pattern_list)[source]¶ Computes the overlap between the reference_pattern and each pattern in pattern_list
Parameters: - reference_pattern –
- pattern_list – list of patterns
Returns: A list of the same length as pattern_list
-
neurodynex.hopfield_network.pattern_tools.
compute_overlap_matrix
(pattern_list)[source]¶ For each pattern, it computes the overlap to all other patterns.
Parameters: pattern_list – Returns: the matrix m(i,k) = overlap(pattern_list[i], pattern_list[k]
-
neurodynex.hopfield_network.pattern_tools.
flip_n
(template, nr_of_flips)[source]¶ makes a copy of the template pattern and flips exactly n randomly selected states. :param template: :param nr_of_flips:
Returns: a new pattern
-
neurodynex.hopfield_network.pattern_tools.
get_noisy_copy
(template, noise_level)[source]¶ Creates a copy of the template pattern and reassigns N pixels. N is determined by the noise_level Note: reassigning a random value is not the same as flipping the state. This function reassigns a random value.
Parameters: - template –
- noise_level – a value in [0,1]. for 0, this returns a copy of the template.
- 1, a random pattern of the same size as template is returned. (for) –
Returns:
-
neurodynex.hopfield_network.pattern_tools.
get_pattern_diff
(pattern1, pattern2, diff_code=0)[source]¶ Creates a new pattern of same size as the two patterns. the diff pattern has the values pattern1 = pattern2 where the two patterns have the same value. Locations that differ between the two patterns are set to diff_code (default = 0)
Parameters: - pattern1 –
- pattern2 –
- diff_code – the values of the new pattern, at locations that differ between
- two patterns are set to diff_code. (the) –
Returns: the diff pattern.
-
neurodynex.hopfield_network.pattern_tools.
load_alphabet
()[source]¶ Load alphabet dict from the file
data/alphabet.pickle.gz
, which is included in the neurodynex release.Returns: Dictionary of 10x10 patterns Return type: dict Raises: ImportError
– Raised ifneurodynex
can not be imported. Please install neurodynex.
neurodynex.hopfield_network.plot_tools module¶
Helper tools to visualize patterns and network state
-
neurodynex.hopfield_network.plot_tools.
plot_nework_weights
(hopfield_network, color_map='jet')[source]¶ Visualizes the network’s weight matrix
Parameters: - hopfield_network –
- color_map –
-
neurodynex.hopfield_network.plot_tools.
plot_overlap_matrix
(overlap_matrix, color_map='bwr')[source]¶ Visualizes the pattern overlap
Parameters: - overlap_matrix –
- color_map –
-
neurodynex.hopfield_network.plot_tools.
plot_pattern
(pattern, reference=None, color_map='brg', diff_code=0)[source]¶ - Plots the pattern. If a (optional) reference pattern is provided, the pattern is plotted
- with differences highlighted
Parameters: - pattern (numpy.ndarray) – N by N pattern to plot
- reference (numpy.ndarray) – optional. If set, differences between pattern and reference are highlighted
-
neurodynex.hopfield_network.plot_tools.
plot_pattern_list
(pattern_list, color_map='brg')[source]¶ Plots the list of patterns
Parameters: - pattern_list –
- color_map –
Returns:
-
neurodynex.hopfield_network.plot_tools.
plot_state_sequence_and_overlap
(state_sequence, pattern_list, reference_idx, color_map='brg', suptitle=None)[source]¶ For each time point t ( = index of state_sequence), plots the sequence of states and the overlap (barplot) between state(t) and each pattern.
Parameters: - state_sequence – (list(numpy.ndarray))
- pattern_list – (list(numpy.ndarray))
- reference_idx – (int) identifies the pattern in pattern_list for which wrong pixels are colored.