In this post, you’ll get answers to these questions:
- How can I use Vectors in an ANN model?
- Can I use vectors in a Simple Artificial Neuron example?
- Do you have an Artificial Intelligence example in Modern C++?
- How to apply vectors to an ANN model?
- Can we use vectors as arrays in our AI applications?
- What are the benefits of using Modern C++ features like Vectors and Classes in AI applications?
- What are Vectors in AI? How can I create a 2D vector and resize each member?
By learning how to make a vector-based Simple Artificial Neuron Model, it will help you to easily build C++ applications with the use of a C++ IDE.
Table of Contents
What is a Simple ANN Model?
We have presented this Simple AI neuron before. 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 technology . Now go with this example and improve our neuron models and lets create an artificial neuron model by using arrays.
What are Vectors in C++?
Vectors are the same as dynamic arrays and they have the ability to resize themselves automatically when a member of a vector is inserted or deleted. Dynamic arrays of vectors are handled automatically by the container. Members of vector datatypes are placed in contiguous memory storage, thus it can be accessed and traversed using iterators.
Vectors are dynamic arrays that mean elements of a vector are stored contiguously and when you insert their size changes automatically. Inserting data to vectors may take a time than other static arrays because data is inserted to the end and because of the need of extending the vector array. As in dynamic array implementations, vectors have low memory usage because of being saleable and they have a good locality of reference and data cache utilization.
We can randomly access an element of a vector, members can be referenced by indices in the same manner as elements of arrays.
Vectors allow random access; that is, an element of a vector may be referenced in the same manner as elements of arrays (by array indices). Linked lists and sets, on the other hand, do not support random access or pointer arithmetic.
The vector data structure allocates the necessary memory needed for specific data storage This is useful for storing data in lists whose length may not be known before setting up the list but where removal (other than, perhaps, at the end) is rare. Erasing elements from a vector or even clearing the vector entirely does not necessarily free any of the memory associated with that element.
Simply a vector can be defined in this syntax,
1 2 3 |
std::vector<object type> variable_name; |
Now let’s use vectors on our simple ANN model,
How to define a vector Based Artificial Neuron Model in C++?
This is another simple neuron example with vectors. 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 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 8 9 |
std::vector<float> a; // activity of each neurons in vector form //let's define activity of two input neurons (a0, a1) and one output neuron (a2) a.resize(3); a[0] = 0.0; a[1] = 1.0; a[2] = 0; |
Note that we can create 2D vectors in this format,
1 2 3 |
std::vector< std::vector<object type> > variable_name; |
Now we can define our 2D weight vector and we can initialize weights of neural links as below
1 2 3 4 5 6 7 8 9 10 |
std::vector< std::vector<float> > w; // 2D vector definition for the weight of links between each neurons //let's define weights of signals comes from two input neurons to output neuron (0 to 2 and 1 to 2) w.resize(2); w[0].resize(3); // we can resize each neuron connections w[1].resize(3); 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] ); |
Note that here each a and w are vectors and we can easily reach their values as same as arrays.
Is there a full example of using C++ vectors to create an artificial neuron model?
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 40 |
#include <iostream> #include <vector> #define NN 3 // number of neurons //------------------------------------------------------------------------------ // let's define a activation function (or transfer function, threshold) for the output neuron float phi(float sum) { return sum ; // linear transfer function f(sum)=sum, you can use your own activation function here } //------------------------------------------------------------------------------ int main() { std::vector<float> a; // activity of each neurons in vector form std::vector< std::vector<float> > w; // 2D vector definition for the weight of links between each neurons //let's define activity of two input neurons (a0, a1) and one output neuron (a2) a.resize(3); 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.resize(2); w[0].resize(3); // we can resize each neuron connections w[1].resize(3); 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] ); std::cout << "Output Neuron Activation: " << a[2] << std::endl; getchar(); return 0; } |
This is a very simple yet useful example of a vector-based ANN model in modern C++. You can use it on any C++ Compilers Dev C++ or you can go for GNU C/C++, Visual C++, or C++ Builder professionally.
In this example weights are based on neuron index numbers. That means you should resize net links connected to number of neurons while neuron has only 1-2 connections. Benefits of this example gives you flexibility to use all neurons with the more memory usage. In the large networks with millions of neurons, to reduce memory, links can be indexed and neuron number can be included. I am sure you can enhance these models with loops on the higher number of neurons.
In LearnCPlusPlus.org we explained other Simple AN models like Simple, Class Based, Struct Based and Array Based Neuron Models. In Modern C++ AI applications, try to use the Classes and the Vectors because of benefits of their methods and iterative methods, safe memory management ability and their flexibility on the usage.
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 versions of C++ Builder and there is a trial version you can download from here.