/*! @file @id $Id$ */ // 1 2 3 4 5 6 7 8 // 45678901234567890123456789012345678901234567890123456789012345678901234567890 #include #include /** @mainpage Neural Network with Hidden Layers @section neuro-intro Overview @subsection nature Natural Neural Network From Wikipedia: «A neuron is an electrically excitable cell that processes and transmits information through electrical and chemical signals. These signals between neurons occur via synapses, specialized connections with other cells. Neurons can connect to each other to form neural networks. Neurons are the core components of the brain and spinal cord of the central nervous system, and of the ganglia of the peripheral nervous system.» The neuron connects with dendrites to the world or to the axon of other neuirons. The neurites (dendrite or axon) transport electrical stimulation to the cell, which emits the signal to the dendrites if the activation reaches a certain level. @dot digraph g { rankdir=LR; ranksep=0.8; node [shape=hexagon]; edge [arrowhead=none]; subgraph clusterInput { label="sensors"; color="white"; node [shape=point]; I0; I1; I2; I3; I4; I5; I6; I7; I8 I9; } subgraph clusterOutput { label="actors"; color="white"; node [shape=point]; O0; O1; O2; O3; O4; O5; O6; } I1 -> Cell1 [label="axon";taillabel="synapse"]; { I2; I3; I4; } -> Cell1; { I5; I6; I7; I8; } -> Cell2; { I4; I6; I9; I0; } -> Cell3; Cell1 -> Cell8 [label="axon / dendrite"]; Cell1 -> { Cell2; Cell4; Cell5; } Cell2 -> { Cell4; Cell5; Cell6; Cell8; } Cell3 -> { Cell4; Cell6; Cell7; Cell8; } { Cell4; Cell5; Cell6 } -> { Cell7; Cell8; } Cell7 -> { O0; O1; O2 }; Cell8 -> { O3; O4; O5; }; Cell8 -> O6 [label="dendrite"]; } @enddot @subsection art Artificial Neural Network A complex neural network can be imitiated as a vector @c I of @c i input values, a vector @c O of @c o output values and any number @c l of hidden layers, where each of them contains @c h neurons. A neural network with double precision is initialized as: @code NeuroNet net; @endcode @dot digraph g { rankdir=LR; ranksep=1.5; subgraph clusterInput { label="Input Layer"; I1 [label=1>]; I2 [label=2>]; Ix [label=…>]; Ii [label=i>]; } subgraph clusterHidden1 { label="First Hidden Layer"; H11 [label=11>]; H12 [label=12>]; H1x [label=1…>]; H1h [label=1h>]; } subgraph clusterHidden2 { label="Second Hidden Layer"; H21 [label=21>]; H22 [label=22>]; H2x [label=2…>]; H2h [label=2h>]; } subgraph clusterHiddenx { label="More Hidden Layers"; Hx1 [label=…1>]; Hx2 [label=…2>]; Hxx [label=……>]; Hxh [label=…h>]; } subgraph clusterHiddenl { label="Last Hidden Layer"; Hl1 [label=l1>]; Hl2 [label=l2>]; Hlx [label=l…>]; Hlh [label=lh>]; } subgraph clusterOutput { label="Output Layer"; O1 [label=1>]; O2 [label=2>]; Ox [label=…>]; Oo [label=o>]; } { I1; I2; Ix; Ii; } -> { H11; H12; H1x; H1h; } -> { H21; H22; H2x; H2h; } -> { Hx1; Hx2; Hxx; Hxh; } -> { Hl1; Hl2; Hlx; Hlh; } -> { O1; O2; Ox; Oo; } } @enddot @section neuro-forward Forward Propagation The connections between two layers can be modelled as a Matrix. Then Matrix H1 contains the weights from @c I to the first hidden layer, @c H2 from the first to the second, and so on, until @c Hl+1 contains the weights from layer @c l to the output @c O. There is also an activation function @f. For back propagation, this function needs a first derivation @c f'. To get the activation of the first hidden layer, the input vector is multiplied with the weight matrix of the first hidden layer, this results in an output vector. Then the activation function is applied to all values of the output vector:
    V1 = f(I×H1)
    
This is done for all layers, up to the output. The output vector is then calculated as:
    O = f(f(f(f(I×H1)×H2)×H)×Hl+1)
    
@code const size_type i(4); const size_type o(2); NeuroNet net; Matrix<1, i> input(1.0, 2.0, 0.0, -1.0); Matrix<1, o> output = feed(input); @endcode @section neuro-backward Back Propagation @page biblio Bibliography - Artificial Neural Networks: Matrix Form (Part 5) - Artificial Neural Networks: Mathematics of Backpropagation (Part 4) - Vorlesung Neuronale Netze - Zusammenfassung - Christoph Tornau - Neuronale Netze — Eine Einführung - Artificial Neural Network based Curve Prediction - Convolutional Neural Networks (CNNs / ConvNets) - TensorFlow Tutorials - Artificial Neural Network based Curve Prediction */ namespace math { // tangens hyperbolicus as standard activation function template TYPE tanh(const TYPE& v) { return ::tanh((long double)v); } // derivate of activation function for back propagation template TYPE tanh_diff(const TYPE& v) { TYPE ch(::cosh((long double)v)); return 1/(ch*ch); } } template , TYPE(*ACTIVATION_DIFF)(const TYPE&) = math::tanh_diff> class NeuroNet { public: NeuroNet() { } Matrix operator()(const Matrix& in) { Matrix l((in*_wi).apply(ACTIVATION)); for (int i(0); i out((l*_wo).apply(ACTIVATION)); return out; } Matrix learn(const Matrix& in, const Matrix& expect) { Matrix out((*this)(in)); Matrix diff(expect-out); return diff; } private: Matrix _wi; Matrix _wh[HIDDEN_LAYERS-1]; Matrix _wo; };