Site icon Learn C++

How To Make A Vector-Based Simple Artificial Neuron Model

In this post, you’ll get answers to these questions:

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.

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,

[crayon-673f9b4bd2c52804897628/]

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,

[crayon-673f9b4bd2c5a292744857/]

We should have an activation function, in other term transfer function phi(), linear, sigmoid, etc.. Let’s define very simple linear transfer function.

[crayon-673f9b4bd2c5b993777235/]

First, we should define & initialize activity of neurons,

[crayon-673f9b4bd2c5c792364400/]

Note that we can create 2D vectors in this format,

[crayon-673f9b4bd2c5d627066997/]

Now we can define our 2D weight vector and we can initialize weights of neural links as below

[crayon-673f9b4bd2c5e358233763/]

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,

[crayon-673f9b4bd2c5f763765469/]

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:

[crayon-673f9b4bd2c62393444278/]

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.

Exit mobile version