![]() |
VOOZH | about |
A Hopfield Network is a type of recurrent neural network designed to store and recall patterns by mimicking associative memory in the human brain. It works by repeatedly updating interconnected neurons until the system settles into a stable state that represents a stored pattern. This makes it especially useful for recovering complete information from partial or noisy inputs.
Hopfield Networks are classified based on how neuron outputs are represented and how the network dynamics evolve over time.
A Discrete Hopfield Network is a fully interconnected recurrent neural network where each neuron is connected to every other neuron (except itself). The network updates neuron states in discrete steps and produces finite, distinct outputs.
Discrete networks operate in two possible representations:
The weight matrix in a discrete Hopfield network follows two important rules:
(Symmetric weights)
(No self-connections)
These properties ensure the existence of a well-defined energy function, which guarantees convergence to a stable state.
A Continuous Hopfield Network differs from the discrete version in that neuron outputs are not limited to fixed values. Instead outputs vary continuously between 0 and 1.
The Hopfield Network is a single-layer fully connected recurrent network where neurons update through feedback until a stable state is reached. Its symmetric structure enables associative memory and energy-based convergence. The core structural features of a Hopfield Network are:
For a network with n neurons:
Training a Hopfield Network means storing patterns in the weight matrix using the Hebbian learning rule. After training, the network can recall stored patterns from partial or noisy inputs through an iterative testing process.
The Hopfield Network stores patterns using the Hebbian learning rule, which strengthens connections between neurons that activate together.
For storing a set of P input patterns the weight matrix is computed using the Hebbian outer-product rule.
Here is the state of the i-th neuron in that p-th pattern
Case 1: Binary Patterns (0/1)
For binary patterns, values are first converted into bipolar form before applying the Hebbian rule.
Here
Case 2: Bipolar Patterns (-1/+1)
For bipolar patterns, weights are directly computed using the outer-product learning rule.
Here and the weights are symmetric()
After training, the Hopfield Network recalls stored patterns by iteratively updating neuron states until convergence to a stable state (energy minimum). Asynchronous updating is typically used to guarantee convergence.
Step 1: Initialize the Network
Set the initial state equal to the input vector
Step 2: Compute Net Input
For the selected neuron i, compute the net input:
Step 3: Apply Activation Function
For binary representation (0/1):
For bipolar representation (-1/+1):
Step 4: Update and Feedback
In asynchronous updating, one neuron is updated at a time by replacing its old state with the new value and the updated output is then fed back to all other neurons in the network.
Step 5: Check for Convergence
The network is said to have converged when no neuron changes its state during a complete update cycle, indicating that a stable state (local energy minimum) has been reached.
Energy function (Lyapunov function) assigns an energy value to every possible state of the network. During the updating process, the energy either decreases or remains constant, ensuring that the network eventually converges to a stable state (local minimum).
For a network with symmetric weights and no self-connections, the energy function is defined as:
where
In asynchronous updating, the energy never increases and the network converges to a stable memory state at a local minimum.
In the continuous network neuron outputs vary smoothly and the energy function is defined as:
where represent continuous output of neuron i.
For continuous dynamics, convergence is guaranteed if:
This means the energy decreases over time. The neuron dynamics are governed by:
where
Here we implement Hopfield Neural Network
Here we import all the necessary libraries for building and running the Hopfield Network
Here we define the Hopfield Network class and implement the weight training method.
Here we implements pattern recall:
Convert grayscale images into bipolar format −1,+1 using a threshold.
Visual comparison between stored memory, input and network output.
Calculate the number of differing bits between predicted patterns and stored memories.
Pick one image per class for training and one for testing. Preprocess them.
Train the weight matrix using the flattened training images.
Run the network on test images with asynchronous updates and record energy convergence.
Compare training images, inputs and outputs.
Output:
Show how energy decreases during iterative updates.
Output:
These plots how the energy of each test image changes over iterations in the Hopfield Network.
You can download full code from here