Where can I find examples of programs using neural networks in C ++?
Closed due to the fact that it is necessary to reformulate the question so that it was possible to give an objectively correct answer by the participants of the Kromster , Vladimir Martyanov , aleksandr barakin , Kirill Stoianov , αλΡΟΞΏΞ»Ο Ο 8 Oct '16 at 12:02 .
The question gives rise to endless debates and discussions based not on knowledge, but on opinions. To get an answer, rephrase your question so that it can be given an unambiguously correct answer, or delete the question altogether. If the question can be reformulated according to the rules set out in the certificate , edit it .
- And I would also be interested. Find - lay out. - 0xdb
1 answer
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 the numbers in the pictures - a classic task in most articles of the runet :) However, the structure of the neurons tried to make the structure itself independent of the task, and in my opinion it turned out to be quite revealing. I hope comments in the comments will be enough. Classes represent parts of an artificial neuron, and are called respectively
Header file:
class Neuron { public: /*ΠΠΊΡΠΎΠ½ - Π²ΡΡ
ΠΎΠ΄Π½Π°Ρ ΡΠ°ΡΡΡ Π½Π΅ΠΉΡΠΎΠ½Π°, ΠΊΠΎΡΠΎΡΠ°Ρ ΠΏΠ΅ΡΠ΅Π΄Π°Π΅Ρ ΡΠΎΡΡΠΎΡΠ½ΠΈΠ΅ Π½Π΅ΠΉΡΠΎΠ½Π° Π΄Π°Π»ΡΡΠ΅, ΠΎΠ±ΡΠ°Π±ΠΎΡΠ°Π½Π½ΠΎΠ΅ ΡΡΠ½ΠΊΡΠΈΠ΅ΠΉ Π°ΠΊΡΠΈΠ²Π°ΡΠΈΠΈ*/ class Akson{ public: float getSignal();//Π€ΡΠ½ΠΊΡΠΈΡ Π°ΠΊΡΠΈΠ²Π°ΡΠΈΠΈ Neuron *ownerNeuron; }; /*ΠΠ΅Π½Π΄ΡΠΈΡ - ΠΎΠ΄Π½Π° ΠΈΠ· Π²Ρ
ΠΎΠ΄Π½ΡΡ
Π»ΠΈΠ½ΠΈΠΉ Π½Π΅ΠΉΡΠΎΠ½Π°. Π‘ΠΎΠ΄Π΅ΡΠΆΠΈΡ Π²Π΅ΡΠΎΠ²ΠΎΠΉ ΠΊΠΎΡΡΡΠΈΡΠΈΠ΅Π½Ρ. Π Π΄Π°Π½Π½ΠΎΠΉ Π°ΡΡ
ΠΈΡΠ΅ΠΊΡΡΡΠ΅ ΠΊΠ°ΠΆΠ΄ΡΠΉ Π΄Π΅Π½Π΄ΡΠΈΡ ΡΠΎΠ΄Π΅ΡΠΆΠΈΡ ΡΠΊΠ°Π·Π°ΡΠ΅Π»Ρ Π½Π° ΠΏΡΠ΅Π΄ΡΠ΅ΡΡΠ²ΡΡΡΠΈΠΉ Π΅ΠΌΡ Π°ΠΊΡΠΎΠ½, Ρ ΠΊΠΎΡΠΎΡΠΎΠ³ΠΎ ΠΎΠ½ ΠΈ ΠΏΠΎΠ»ΡΡΠ°Π΅Ρ ΡΠΈΠ³Π½Π°Π».*/ class Dendrit{ public: float weight = 0.1; Akson *inputAkson; }; Neuron(); /*ΠΠ΅ΠΊΡΠΎΡ, ΡΠΎΠ΄Π΅ΡΠΆΠ°ΡΠΈΠΉ Π²Ρ
ΠΎΠ΄Π½ΡΠ΅ Π΄Π΅Π½Π΄ΡΠΈΡΡ, Ρ.ΠΊ. ΠΈΡ
ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ ΡΠ°Π·Π½ΠΎΠ΅ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ*/ std::vector<Dendrit> dendrits; Akson akson; /*Π‘ΡΠΌΠΌΠΈΡΡΡΡΠ°Ρ ΡΡΠ½ΠΊΡΠΈΡ, ΠΊΠΎΡΠΎΡΠ°Ρ ΡΡΠΌΠΌΠΈΡΡΠ΅Ρ(Π ΡΠ°ΠΌΠΎΠΌ ΠΏΡΠΎΡΡΠΎΠΌ ΡΠ»ΡΡΠ°Π΅) Π²Ρ
ΠΎΠ΄Π½ΡΠ΅ ΡΠΈΠ³Π½Π°Π»Ρ*/ virtual float kernelFunction(); /*Π‘ΠΎΡΡΠΎΡΠ½ΠΈΠ΅ Π½Π΅ΠΉΡΠΎΠ½Π°*/ float value; }; The implementation file itself implements the functions of the neuron and axon. The function of the neuron is declared virtual, so that it can be redefined for each layer of neurons.
Here is the implementation code:
#include "neuron.h" #include <cmath> /*Π ΠΊΠ°ΡΠ΅ΡΡΠ²Π΅ ΡΡΠ½ΠΊΡΠΈΠΈ Π°ΠΊΡΠΈΠ²Π°ΡΠΈΠΈ ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΠ΅ΡΡΡ Ρ.Π½. ΡΠΈΠ³ΠΌΠ°-ΡΡΠ½ΠΊΡΠΈΡ, ΠΊΠΎΡΠΎΡΠ°Ρ ΠΎΠ±ΡΠ°Π±Π°ΡΡΠ²Π°Π΅Ρ ΡΠΈΠ³Π½Π°Π», ΠΏΠΎΠ»ΡΡΠ΅Π½Π½ΡΠΉ ΡΡΠΌΠΌΠΈΡΡΡΡΠ΅ΠΉ ΡΡΠ½ΠΊΡΠΈΠ΅ΠΉ Π½Π΅ΠΉΡΠΎΠ½Π°*/ float Neuron::Akson::getSignal() { return (1 / (1 + exp(-(ownerNeuron->kernelFunction())))) ; } /*ΠΠΎΠ½ΡΡΡΡΠΊΡΠΎΡ ΡΡΡΠ°Π½Π°Π²Π»ΠΈΠ²Π°Π΅Ρ Π΄Π»Ρ Π² ΠΊΠ°ΡΠ΅ΡΡΠ²Π΅ Π½Π΅ΠΉΡΠΎΠ½Π°-Π²Π»Π°Π΄Π΅Π»ΡΡΠ° Π°ΠΊΡΠΎΠ½Π° ΡΠ²ΠΎΠΉ ΠΎΠ±ΡΠ΅ΠΊΡ*/ Neuron::Neuron() { akson.ownerNeuron = this; } /*ΠΠ΅ΠΉΡΠΎΠ½ Π·Π°ΠΏΡΠ°ΡΠΈΠ²Π°Π΅Ρ ΡΠΈΠ³Π½Π°Π» Ρ ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ ΡΠ²ΠΎΠ΅Π³ΠΎ Π΄Π΅Π½Π΄ΡΠΈΡΠ°, ΡΠ΅ΠΌ ΡΠ°ΠΌΡΠΌ ΠΎΠ±ΡΠ°ΡΠ°ΡΡΡ ΠΊ Π°ΠΊΡΠΎΠ½Π°ΠΌ Π½Π΅ΠΉΡΠΎΠ½ΠΎΠ² ΠΏΡΠ΅Π΄ΡΠ΄ΡΡΠ΅Π³ΠΎ ΡΠ»ΠΎΡ, ΠΊΠΎΡΠΎΡΡΠ΅ Π² ΡΠ²ΠΎΡ ΠΎΡΠ΅ΡΠ΅Π΄Ρ ΠΎΠ±ΡΠ°ΡΠ°ΡΡΡΡ ΠΊ ΡΠ²ΠΎΠΈΠΌ Π½Π΅ΠΉΡΠΎΠ½Π°ΠΌ ΠΈ.Ρ.Π΄, ΡΠΎ Π΅ΡΡΡ ΡΠ±ΠΎΡ ΡΠΈΠ³Π½Π°Π»ΠΎΠ² ΠΈΠ΄Π΅Ρ ΡΠ΅ΠΏΠΎΡΠΊΠΎΠΉ ΠΎΡ ΡΠ°ΠΌΠΎΠ³ΠΎ ΠΏΠ΅ΡΠ²ΠΎΠ³ΠΎ ΡΠ»ΠΎΡ*/ float Neuron::kernelFunction() { float result(0.); for(Dendrit d : dendrits){ result += d.inputAkson->getSignal() * d.weight; } return result; } Next comes the definition of the class of the neural network itself, consisting, in fact, of neurons:
class KohonenNet { public: /*Π‘ΠΎΠ·Π΄Π°Π½ΠΈΠ΅ ΠΏΠΎΠ΄Π²ΠΈΠ΄Π° Π½Π΅ΠΉΡΠΎΠ½Π°: Π²Ρ
ΠΎΠ΄Π½ΠΎΠΉ Π½Π΅ΠΉΡΠΎΠ½, ΠΊΠΎΡΠΎΡΡΠΉ ΡΠΎΠ»ΡΠΊΠΎ ΠΏΠ΅ΡΠ΅Π΄Π°Π΅Ρ Π²Ρ
ΠΎΠ΄Π½ΠΎΠΉ ΡΠΈΠ³Π½Π°Π» Π΄Π°Π»ΡΡΠ΅*/ class InputNeuron : public Neuron{ public: float kernelFunction(){ return value; } }; KohonenNet(); vector<Neuron*> inNeurons; vector<Neuron> outNeurons; int handle(QImage); private: /*Π€ΡΠ½ΠΊΡΠΈΡ ΠΎΠ±ΡΡΠ΅Π½ΠΈΡ Π½Π΅ΠΉΡΠΎΠ½Π°*/ void study(Neuron &n, QImage pic); /*ΠΠ°ΡΡΠΈΠ½ΠΊΠΈ Ρ ΡΠΈΡΡΠ°ΠΌΠΈ Π΄Π»Ρ ΠΎΠ±ΡΡΠ΅Π½ΠΈΡ*/ 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 do not give it.
P.S. I hope my work will help someone understand :)