What is a simple artificial neuron in C++? How can we code a simple AI neuron in C++? Should we use arrays, classes, or structs? In previous AI Tech posts, we answered all these questions. In this post, we will list all the simple AI models in C++. There can be many models depending on your research. We have tried to keep these posts simple to understand the different ways of AI programming in C++ with the use of C++ IDE.
Table of Contents
What is an Artificial Neural Network or ANN?
An Artificial Neural Network, also called ANN is a part of Artificial Intelligence that models artificial neurons connected in artificial layers. C++ allows us to create our own AI neuron models. 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 its input nets.
We prepared a very simple Artificial Neural Network example with an Activation Function.
By the given example above, Here we go,
Is there a simple Artificial Neuron Model example in C++?
In the introduction post of the AI Tech series, we gave a very Simple Artificial Neural Network Example with a Simple Artificial Neuron in C++ as 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 |
#include <iostream> // let's define a transfer function (or threshold) for the output neuron float phi(float sum) { return sum ; // linear transfer function f(sum)=sum } int main() { //let's define activity of two input neurons (a0, a1) and one output neuron (a2) float a0 = 0.0, a1 = 1.0, a2 = 0; //let's define weights of signals comes from two input neurons to output neuron (0 to 2 and 1 to 2) float w02 = 0.3, w12 = 0.2; // Let's fire our artificial neuron to obtain activity by the transfer function, output will be std::cout << "Firing Output Neuon ...\n" ; a2 = phi(a0*w02 + a1*w12); std::cout << "Output Neuron Activation Value:" << a2 << '\n'; getchar(); return 0; } |
Now, let’s do this same example with different neuron models in C++;
Is there an array-based Artificial Neuron Model example in C++?
Yes. Here is another simple neuron example with arrays. This example is also good for C applications on IoT devices. If you have a constant network and you know the trained data values, this model may be easy to check activation of neurons. Arrays allow you to use for
loops.
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 38 39 |
#include <stdio.h> #define NN 3 // number of neurons // let's define a transfer function (or threshold) for the output neuron float phi(float sum) { return sum ; // linear transfer function f(sum)=sum } int main() { float a[NN+1]; // activaty of each neurons float w[NN+1][NN+1]; // weight of links between each neurons //let's define activity of two input neurons (a0, a1) and one output neuron (a2) a[0] = 0.0; a[1] = 1.0; a[2] = 0; //let's define weights of signals comes from two input neurons to output neuron (0 to 2 and 1 to 2) w[0][2] = 0.3; w[1][2] = 0.2; // Let's fire our artificial neuron activity, output will be a[2] = phi( a[0]*w[0][2] + a[1]*w[1][2] ); printf("%10.6f\n", a[2]); getchar(); return 0; } |
Is there a structure-based Artificial Neuron Model example for the C language?
This example is perfect for calculating high numbers of neurons on machines which code can be compiled only in C:
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 38 39 40 41 42 43 |
#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]; // let's define a transfer function (or threshold) for the output neuron float phi(float sum) { return sum ; // linear transfer function f(sum)=sum } 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; } |
We can also add a ‘fire’ function to fire each neurons something like this;
1 2 3 4 5 6 7 8 9 10 11 12 13 |
float fire(int i) { float sum = 0; for ( int j=0; j<NN; j++ ) { if( n[i].w[j]>=0 ) sum += n[i].a*n[i].w[j]; } return phi(sum); } |
Is there a Class-Based Artificial Neuron Model Example in C++?
Classes are the best to use in C++; they are safe and much more functional. If you are new to Classes please read our previous posts about Classes & Objects.
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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
#include <stdio.h> #define NN 2 // number of neurons class Tneuron // neuron class { public: float a; // activity of each neurons float w[NN+1]; // weight of links between each neurons Tneuron() { a=0; for(int i=0; i<=NN; i++) w[i]=-1; // if weight is negative there is no link } // let's define an activation function (or threshold) for the output neuron float activation_function(float sum) { return sum ; // linear transfer function f(sum)=sum } }; Tneuron ne[NN+1]; // neuron objects void fire(int nn) { float sum = 0; for ( int j=0; j<=NN; j++ ) { if( ne[j].w[nn]>=0 ) sum += ne[j].a*ne[j].w[nn]; } ne[nn].a = ne[nn].activation_function(sum); } int main() { //let's define activity of two input neurons (a0, a1) and one output neuron (a2) ne[0].a = 0.0; ne[1].a = 1.0; ne[2].a = 0; //let's define weights of signals comes from two input neurons to output neuron (0 to 2 and 1 to 2) ne[0].w[2] = 0.3; ne[1].w[2] = 0.2; // Let's fire our artificial neuron activity, output will be fire(2); printf("%10.6f\n", ne[2].a); getchar(); return 0; } |
If you are developing an AI algorithm for the C based devices, IoTs, and so on, then simple model, array-based models – but mostly struct-based AI models can be useful. These can also be useful with C++ based devices. If you are developing modern applications on Windows, macOS, iOS, Android or Linux then Struct- and Class-based models can be used. Class-based models can be also used on some IoTs which has C++ supports.
These example models above are very simple artificial neuron models. In C++ we’re lucky in that we can create our own models by using C++’s strong Classes, Objects and Structure features.