Artificial Intelligence TechC++Language FeatureLearn C++

How To Develop A Simple Hopfield Network In C++

How To Develop A Simple Hopfield Network In C++

In the history of AI development, one of the greatest AI technologies was the pattern recognition models, especially to read texts from pixel images. One of them was the Hopfield network (or Ising model of a neural network or Ising–Lenz–Little model), which is a form of recurrent neural network, it was invented by Dr. John J. Hopfield in 1982. A Hopfield Network can be applied to pattern recognition, such as recognition from digit character images. In this post, we will develop a simple Hopfield Network example in GUI form using C++ Builder that can learn from pixelated patterns and we can recall them by testing some closest patterns.

What is Hopfield Network in AI development?

The purpose of a Hopfield Network is to store data patterns and to recall the full data pattern based on partial input. For example, a Hopfield Network can be applied to 8×8 digit character recognition from pixels. We can train some characters on this network and then we can ask a closer drawing character if it remembers trained one or not. We can use this Hopfield network to extract the characters from images and put them in a text file in ASCII form, which is called pattern recognition. The good behavior of this network is it can also remember the full form of that character while it is not given completely. This example can be used for the optical character recognition from a text and if there is deformation on a small part of a letter or on a paper, maybe be paper is dirty Hopefield network can remember this kind of problem. In new modern ML and AI applications, there are much more useful methods that are based on this Hopfield Network like Recurrent Artificial Neural Networks.

The Hopfield Network (or Ising model of a neural network or Ising–Lenz–Little model) is a form of recurrent neural network, and it was invented by Dr. John J. Hopfield in 1982. It consists of a single layer that contains one or more fully connected recurrent neurons. The Hopfield network is commonly used for auto-association and optimization tasks. Hopfield networks serve as content-addressable memory systems with binary threshold nodes. Hopfield networks also provide a primitive model for understanding how the human brain and memory can be simulated artificially.

How to Train a Hopfield Network

How we can apply this method on a digital character recognition in programming ? There is a way a Hopfield network would work on 1 and 0 pixels. We can map it out that each pixel represents one node in the Hopfield network. We can train correct form of the characters on this network to recognize each of characters. Hopfield network finds the most possible assumed character after few iterations, and eventually reproduces the pattern with the trained correct form. If we have N pixels, it means our network has NxN weights, so the problem is very computationally expensive and may be slow in some cases. In example, we can train blank form (space) and this “A” form, and so on.

All the nodes in a Hopfield network are used as both inputs and outputs, and they are fully interconnected with each other. That is, each node is an input to every other node in the network. We can think of the links from each node to itself as being a link with a weight of 0.

We can easily train Hopfield networks, we just need binary data to train this network. As we know we can have binary input vectors as well as bipolar input vectors. During the training of the Hopfield network, weights are being updated in iterations.

For example for a 3 node Hopefield network data matrix a composed with N elements and weight matrix composed with NxN elements. Here is a 3 node example to these matrixes,

How can we develop a simple Hopfield Network in C++?

Let’s create a simple Hofield Network C++ Builder example as below.

We can create a simper THopfield_Network class as in given steps below. Here is how we start to define out class,

We will add 3 public methods to this class. First, it will learn from a pattern vector by using learn_pattern() method which is defined as below,

And we will update neurons of our hopfield network by using update_neuron() method which is defined as below,

We can use this update_neuron method to test given input pattern by using a test() method which is defined as below,

As a result our simple Hopfield Network class in modern C++ will be as below,

Now we can globally define a hopfield network for given Width and Height pixels. In example if we have 6×6 pixels patterns we can define it as in below,

Now we can define a pattern vector as below,

we can train this pattern and if we have another input pattern as above, we can test it to obtain closest result (recovered_pattern) to given input as below.

Now, let’s do all this with GUIs in C++ Builder.

How can we develop a simple Hopfield Network in C++ Builder?

  1. First, create a new C++ Builder FMX application, add an Image (TImage), Memo (TMemo) and 3 Buttons (TButton) which are “Clear”, “Train” and “Test” buttons. You can add some Layouts to arrange them as given Form (TForm) design below.

2. Add our THopfield_Network class to below the line “TForm1 *Form1;”,

3. Define bmp and bmp2 bitmaps as in this Form unit’s header file as below, bmp will be used as an input pattern. bmp2 will be displayed bitmap on Image.

4. Now we can create these bitmaps when the Form is constructed as below.

5. Now we can create clear event by double clicking to Clear button, such as,

6. We should allow user to define its own pattern by clicking on the Image, so user can draw some patterns on our Image1. We can create this by double clicking OnMouseDown event as below,

7. Now, we can create training method by double clicking our Train button. In this method, we will read data from pixels of bmp bitmap then we will display this in Memo box, and then we will learn this pattern by using hopfield.learn_pattern(pattern); Here is how we do this below,

8. Finally we can create Testing event by double clicking to Test button.

How can we test a simple Hopfield Network app in C++ Builder?

Now we can run our application (F9). First, we can Clear and then we Train this blank pattern, then we can draw “A” as below and then we can Train this. You can train more different patterns.

After this step, we can ask another pattern by using Test button that looks like one of the pattern that we trained before, such as this,

As a result you will see that our Hopfield Network remembers its closest pattern and recovers it.

As you see developing AI technologies in C++ Builder is really easy and amazing to understand AI technologies that build helps to build today’s models.

Is there a full example C++ code about Hopfield Network in C++ Builder?

Here is the full example of C++ Builder FMX application, note that header is also given above,

What Is Assignment Operator Overloading C++ Builder logo

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise version.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?

C++C++17Code SnippetLearn C++

How To Convert Char Array String To Unicode String Correctly In C++?

C++C++11C++14C++17C++20Introduction to C++Learn C++Syntax

Learn Default Constructors Of Classes In Modern C++

Artificial Intelligence TechC++C++11C++14C++17Learn C++

How To Develop Special AI Activation Functions In C++?