neurodynex3.hopfield_network package

Submodules

neurodynex3.hopfield_network.demo module

neurodynex3.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 neurodynex3.hopfield_network.network.HopfieldNetwork(nr_neurons)[source]

Bases: object

Implements a Hopfield network.

nrOfNeurons

Number of neurons

Type

int

weights

nrOfNeurons x nrOfNeurons matrix of weights

Type

numpy.ndarray

state

current network state. matrix of shape (nrOfNeurons, nrOfNeurons)

Type

numpy.ndarray

iterate()[source]

Executes one timestep of the dynamics

reset_weights()[source]

Resets the weights to random values

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.

set_state_from_pattern(pattern)[source]

Sets the neuron states to the pattern pixel. The pattern is flattened.

Parameters

pattern – pattern

store_patterns(pattern_list)[source]

Learns the patterns by setting the network weights. The patterns themselves are not stored, only the weights are updated! self connections are set to 0.

Parameters

pattern_list – a nonempty list of patterns.

neurodynex3.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 neurodynex3.hopfield_network.pattern_tools.PatternFactory(pattern_length, pattern_width=None)[source]

Bases: object

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_all_off()[source]
Returns

2d pattern, all pixels off

create_all_on()[source]
Returns

2d pattern, all pixels on

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

reshape_patterns(pattern_list)[source]

reshapes all patterns in pattern_list to have shape = (self.pattern_length, self.pattern_width)

Parameters
  • self

  • pattern_list

Returns:

neurodynex3.hopfield_network.pattern_tools.compute_overlap(pattern1, pattern2)[source]

compute overlap

Parameters
  • pattern1

  • pattern2

Returns: Overlap between pattern1 and pattern2

neurodynex3.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

neurodynex3.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]

neurodynex3.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

neurodynex3.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:

neurodynex3.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.

neurodynex3.hopfield_network.pattern_tools.load_alphabet()[source]

Load alphabet dict from the file data/alphabet.pickle.gz, which is included in the neurodynex3 release.

Returns

Dictionary of 10x10 patterns

Return type

dict

Raises

ImportError – Raised if neurodynex can not be imported. Please install neurodynex.

neurodynex3.hopfield_network.pattern_tools.reshape_patterns(pattern_list, shape)[source]

reshapes each pattern in pattern_list to the given shape

Parameters
  • pattern_list

  • shape

Returns:

neurodynex3.hopfield_network.plot_tools module

Module contents