Home c++ Sample C++ Neural Networks

Sample C++ Neural Networks [Locked]

Author

Date

Category

Where can I find sample programs using neural networks in C++?


Answer 1, authority 100%

Just the other day I was reading articles about the simplest neural networks, and along the way I sketched my simplest network. This network was designed to recognize numbers in pictures – a classic task in most articles of the Russian Internet 🙂 However, I tried to make the structure of neurons independent of the task, and in my opinion it turned out quite indicative. I hope there will be enough explanations in the comments. Classes represent parts of an artificial neuron, and are named accordingly

Header file:

class Neuron
{
public:
/ * Axon - the output part of the neuron that transmits the state
 neuron further processed by the activation function * /
  class Akson {
  public:
    float getSignal (); // Activation function
    Neuron * ownerNeuron;
  };
/ * Dendrite is one of the input lines of the neuron. Contains a weighting factor.
In this architecture, each dendrite contains a pointer to the one that precedes it.
the axon from which it receives the signal. * /
  class Dendrit {
  public:
    float weight = 0.1;
    Akson * inputAkson;
  };
  Neuron ();
/ * The vector containing the input dendrites, since there may be a different number of them * /
  std :: vector & lt; Dendrit & gt; dendrits;
  Akson akson;
/ * Summing function that sums (In the simplest case)
input signals * /
  virtual float kernelFunction ();
/ * Neuron state * /
  float value;
};

The implementation file actually implements the functions of the neuron and axon. The neuron function is declared virtual so that it can be redefined for each layer of neurons.

Here is the implementation code:

# include "neuron.h"
#include & lt; cmath & gt;
/ * As the activation function is used the so-called. sigma function,
which processes the signal received by the summing function of the neuron * /
float Neuron :: Akson :: getSignal ()
{
  return (1 / (1 + exp (- (ownerNeuron- & gt; kernelFunction ()))));
}
/ * Constructor sets for as owner neuron
axon own object * /
Neuron :: Neuron ()
{
  akson.ownerNeuron = this;
}
/ * The neuron requests a signal from each of its dendrites,
thereby referring to the axons of the neurons of the previous layer,
which, in turn, refer to their neurons, etc.,
that is, the collection of signals goes in a chain from the very first layer * /
float Neuron :: kernelFunction ()
{
  float result (0.);
  for (Dendrit d: dendrits) {
    result + = d.inputAkson- & gt; getSignal () * d.weight;
  }
  return result;
}

Next is the definition of the class of the neural network itself, consisting, in fact, of neurons:

class KohonenNet
{
public:
/ * Create a subtype of neuron: an input neuron that only
passes the input signal on * /
  class InputNeuron: public Neuron {
  public:
    float kernelFunction () {
      return value;
    }
  };
  KohonenNet ();
  vector & lt; Neuron * & gt; inNeurons;
  vector & lt; Neuron & gt; outNeurons;
  int handle (QImage);
private:
/ * Neuron learning function * /
  void study (Neuron & amp; n, QImage pic);
/ * Pictures with numbers for training * /
  QString names [9] = {"0.bmp", "1.bmp", "2.bmp", "3.bmp", "4.bmp", "5.bmp", "6.bmp", " 7.bmp "," 8.bmp "};
  const uint numNumbers = 9;
};

The implementation of network functions already solves a specific problem, takes many lines of code and carries little information about the device of the network itself, so I am not giving it.

P.S. I hope my work will help someone figure it out 🙂

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions