Artificial Intelligence TechC++C++11C++14C++17Learn C++

How To Make A Vector-Based Simple Artificial Neuron Model

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.

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,

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,

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

First, we should define & initialize activity of neurons,

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

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

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,

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:

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.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Learn C++

What Is The Priority Queue (std::priority_queue) In Modern C++?

C++C++11C++14C++17C++20

What Is The Stack (std::stack) In Modern C++?

C++C++11C++14C++17C++20Learn C++

What Is The Queue (std::queue) In Modern C++?

C++C++11C++14C++17Learn C++SyntaxTemplates

What Are The Logical Operation Metafunctions In Modern C++?