HopfieldNetwork<T>
Models & Types · AiDotNet.NeuralNetworks
Represents a Hopfield Network, a recurrent neural network designed for pattern storage and retrieval.
Inheritance: NeuralNetworkBase<T> → DeclaredModelLayoutBase<T> → VectorModelLayoutBase<T> → HopfieldNetwork<T>
For Beginners
A Hopfield Network is like a special memory system that can store and recall patterns.
Think of it like a photo album with a magical property:
- You can store a collection of complete photos in the album
- Later, if you show the album a damaged or partial photo, it can recall the complete original version
For example:
- You might store clear images of the digits 0-9
- If you later show the network a smudged or partially erased “7”, it can recall the clean version
Hopfield networks work differently from most neural networks:
- They don’t have separate input and output layers
- All neurons are connected to each other (but not to themselves)
- They use a special learning rule based on correlations between pattern elements
These networks are useful for tasks like:
- Image reconstruction
- Pattern recognition
- Noise filtering
- Solving certain optimization problems
How It Works
A Hopfield Network is a type of recurrent artificial neural network that serves as a content-addressable memory system. It can store patterns and retrieve them based on partial or noisy inputs. The network consists of a single layer of fully connected neurons, with each neuron connected to all others except itself. Hopfield networks are particularly useful for pattern recognition, image restoration, and optimization problems.
Example
var pattern = Tensor<float>.CreateRandom(new[] { 1, 100 });
var trainX = Tensor<float>.CreateRandom(4, 8);
var trainY = Tensor<float>.CreateRandom(4, 2);
var result = new AiModelBuilder<float, Tensor<float>, Tensor<float>>()
.ConfigureModel(new HopfieldNetwork<float>())
.Build(trainX, trainY);
var recalled = result.Predict(pattern);
Constructors
| Constructor | Summary |
|---|---|
HopfieldNetwork() |
Initializes a new instance of the HopfieldNetwork class with the specified architecture and size. |
HopfieldNetwork(NeuralNetworkArchitecture<T> architecture, int size, ILossFunction<T> lossFunction, HopfieldNetworkOptions options) |
Properties
| Property | Summary |
|---|---|
bool SupportsTraining { get; } |
Converts an input tensor to a vector, performs pattern recall, and converts back to a tensor. |
Methods
| Method | Summary |
|---|---|
Dictionary<string, Tensor<T>> GetNamedLayerActivations(Tensor<T> input) |
Returns the weight matrix state as a named activation (Hopfield has no layers). |
ModelMetadata<T> GetModelMetadata() |
Gets metadata about the Hopfield network model. |
T CalculateEnergy(Vector<T> state) |
Serializes Hopfield network-specific data to a binary writer. |
Vector<T> Recall(Vector<T> input, int maxIterations) |
Performs pattern recall to retrieve a complete pattern from a partial or noisy input. |
int GetNetworkCapacity() |
Gets the maximum number of patterns that can be reliably stored in the network. |
void Train(List<Vector<T>> patterns) |
Trains the Hopfield network on a set of patterns. |