Do you want to develop your 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++ before. We highly recommend reading 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 depend on the number of its input nets.
This is a very simple artificial neural network in AI technolgy . Now go with this example and improve our neuron models and lets create an artificial neuron model by using arrays.
Array Based Artificial Neuron Model in C++
This is another simple neuron example with arrays. This example is also good for C applications on IoTs. If you have a constant network and you know the trained data values, this model may be easy to check activation of neurons. Arrays are easy to allocate memory and easy to use on static neural networks.
Let’s define number of neurons, if it varies in your application use int,
1 2 3 |
#define NN 3 // number of neurons |
We should have an activation function, in other term transfer function phi(), linear, sigmoid, etc.. Let’s define very simple linear transfer function.
1 2 3 4 5 6 |
float phi(float sum) { return sum ; // linear transfer function f(sum)=sum } |
First, we should define & initialize activity of neurons,
1 2 3 4 5 6 7 |
float a[NN+1]; // activaty of each neurons a[0] = 0.0; a[1] = 1.0; a[2] = 0; |
Next, we should define & initialize weights of neural links,
1 2 3 4 5 6 |
float w[NN+1][NN+1]; // weight of links between each neurons w[0][2] = 0.3; w[1][2] = 0.2; |
Finally we can calculate new activity values of output neuron by using our activation function as below,
Thus, output activation value of neuron 2 can be written as below,
1 2 3 |
a[2] = phi( a[0]*w[0][2] + a[1]*w[1][2] ); |
Here is the full example combined together,
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; } |
This is a good example and friendly with C and C++ codes, that means you can use it on IoTs with MicroC, Ardunio C, Dev C++ or you can go for GNU C/C++, Visual C++, or C++ Builder professionally.
Get started building powerful apps with C++Builder!
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition