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;
1 2 3 4 |
struct struct_name { // List data members here } dataobject_names; |
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,
1 2 3 4 5 6 7 |
struct st_neuron { float a; // activity of each neurons float w[NN+1]; // weight of links between each neurons }; |
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,
1 2 3 |
struct st_neuron n[NN+1]; |
We can define activity of two input neurons (a0, a1) and one output neuron (a2) as below,
1 2 3 4 5 |
n[0].a = 0.0; n[1].a = 1.0; n[2].a = 0; |
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,
1 2 3 4 |
n[0].w[2] = 0.3; n[1].w[2] = 0.2; |
Now we need a activation function, let’s define a simple activation function phi() (transfer function or threshold) for the output neuron as below,
1 2 3 4 5 6 |
float phi(float sum) { return sum ; // linear transfer function f(sum)=sum } |
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,
1 2 3 |
n[2].a = phi(n[0].a*n[0].w[2] + n[1].a*n[1].w[2]); |
Let’s combine them in a full C++ example as in below;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
#include <stdio.h> #define NN 3 // number of neurons struct st_neuron { float a; // activaty of each neurons float w[NN+1]; // weight of links between each neurons float phi; // threshold value of each neurons for the activity output }; struct st_neuron n[NN+1]; void main() { //let's define activity of two input neurons (a0, a1) and one output neuron (a2) n[0].a = 0.0; n[1].a = 1.0; n[2].a = 0; //let's define weights of signals comes from two input neurons to output neuron (0 to 2 and 1 to 2) n[0].w[2] = 0.3; n[1].w[2] = 0.2; // Let's fire our artificial neuron activity, output will be n[2].a = phi(n[0].a*n[0].w[2] + n[1].a*n[1].w[2]); printf("%10.6f\n", n[2].a); getchar(); return 0; } |
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.