Artificial Intelligence TechC++Learn C++

AI Techs :: Structure Based Simple Artificial Neuron Model in C++

Do you want to develop your of artificial intelligence application from the scratch ? Want to learn how you can develop a simple artificial neuron model in C++? In this post we will explain with a very simple artificial neuron example. We have released A Simple Artificial Neuron Model in C++ and Very Simple Artificial Neural Network (ANN) Example in C++ and we also released Array Based Simple Artificial Neuron Model in C++ before. We highly recommend reading Introduction to AI Technologies and them if you are new to AI technology.

A Simple ANN Model

We have presented this Simple AI neuron before, let’s remember. A Minimum Artificial Neuron has an activation value (a), an activation function ( phi() ) and weighted (w) input net links. So it has one activation value, one activation function and one or more weights depends on number of it’s input nets.

This is a very simple artificial neural network in AI technology . Now let’s go with this example and improve our neuron models and lets create an artificial neuron model by using arrays.

Structure Based Artificial Neuron Model in C++

This is another simple neuron example with structures. In C & C++ programming; a Data Structure (struct) is a data block in memory and group of data elements grouped together with a name, and these data elements are variables (integers, floats, strings …) called as members, all can be defined to a new variable under a single name. They can have different types and different lengths. Data structures can be declared by using struct statement, using the following syntax;

Structs are primarily used in C programming language, you can use and store your data in C++ too. Mostly in C++ Classes are used to store data and function instead of structures. The main difference between a struct and a class is classes can obtain both data and functions while structs has data only, functions can be used externally. One of the benefit of structs are they have fixed size.

A Minimum Artificial Neuron has an activation value (a), an activation function ( phi() ) and weighted (w) input net links. So we can use structs to define a simple neuron model in C++ as below,

This is a good example if all neurons are linked each other, or there are few number of neurons. This example is also very good to calculate in IoT machines which codes can be compiled in C only, Here NN shows number of neurons and number of weights are depends on number of net links, the right way here could be using a pointer for the array, we will use this simple model here. Now we can create our n[] artificial neuron structure for the NN number neurons as here,

We can define activity of two input neurons (a0, a1) and one output neuron (a2) as below,

Note that n[2] is our output neuron here and we will calculate it’s new activation value while it’s given 0 here.

In our example above, two input neurons are connected to output neuron. Thus we have 2 links that means we have weights. We can define weights of signals comes from two input neurons to output neuron (0 to 2 and 1 to 2) as in here,

Now we need a activation function, let’s define a simple activation function phi() (transfer function or threshold) for the output neuron as below,

We can fire our artificial neuron by the activation function to calculate it’s activity . Let’s fire n[2] calculate activation of output neuron as below,

Let’s combine them in a full C++ example as in below;

Last Notes,

This is a very simple structure based artificial neuron example to give you idea how you can develop your own structure based neuron model in C++. If you have a lot of neurons and each neuron has links, links and weights should be stored outside of this structure. You can use pointers to point net link arrays for each neuron structure too. This example very good to understand a simple Artificial Neuron in C++ and it is easy to use on IoTs with C or C++. It can be recommended for the neural networks which has few neurons.

Get started building powerful apps with C++Builder!


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.

About author

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
C++C++17Code SnippetComponentsIteratorsLearn C++

What Are The Boost C++ Libraries?

C++C++17Introduction to C++Learn C++Syntax

Learn Bits and How To Use std::bitset In C++

C++C++17IteratorsLearn C++

How To Use Insert In std::map In Modern C++

C++C++17Introduction to C++IteratorsLearn C++Syntax

What Are The Differences Between Vector And Array In C++?

Worth reading...
AI Techs :: Structure Based Simple Artificial Neuron Model in C++