<h1>neural++.hpp</h1><ahref="neural_09_09_8hpp.html">Go to the documentation of this file.</a><divclass="fragment"><preclass="fragment"><aname="l00001"></a>00001 <spanclass="comment">/**************************************************************************************************</span>
<aname="l00005"></a>00005 <spanclass="comment"> * This program is free software: you can redistribute it and/or modify it under the terms of the *</span>
<aname="l00006"></a>00006 <spanclass="comment"> * GNU General Public License as published by the Free Software Foundation, either version 3 of *</span>
<aname="l00007"></a>00007 <spanclass="comment"> * the License, or (at your option) any later version. This program is distributed in the hope *</span>
<aname="l00008"></a>00008 <spanclass="comment"> * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of *</span>
<aname="l00009"></a>00009 <spanclass="comment"> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for *</span>
<aname="l00010"></a>00010 <spanclass="comment"> * more details. You should have received a copy of the GNU General Public License along with *</span>
<aname="l00011"></a>00011 <spanclass="comment"> * this program. If not, see <http://www.gnu.org/licenses/>. *</span>
<aname="l00075"></a>00075 <spanclass="keywordtype">void</span><aclass="code"href="classneuralpp_1_1NeuralNet.html#94169c89a7cd47122ab5dbf1d5c5e108"title="It updates the weights of the net&#39;s synapsis through back-propagation.">updateWeights</a>();
<aname="l00076"></a>00076
<aname="l00082"></a>00082 <spanclass="keywordtype">void</span><aclass="code"href="classneuralpp_1_1NeuralNet.html#f697a8d9967ad8f03e5a16a42cd110c5"title="It commits the changes made by updateWeights() to the layer l.">commitChanges</a> (<aclass="code"href="classneuralpp_1_1Layer.html"title="Class for managing layers of neurons.">Layer</a>& l);
<aname="l00083"></a>00083
<aname="l00089"></a>00089 <spanclass="keywordtype">double</span><aclass="code"href="classneuralpp_1_1NeuralNet.html#8a140d28e6dd4097470c7c138801ad01"title="Get the error made on the expected result as squared deviance.">error</a> (<spanclass="keywordtype">double</span> ex);
<aname="l00090"></a>00090
<aname="l00095"></a>00095 double (*<aclass="code"href="classneuralpp_1_1NeuralNet.html#c1469e6afd87d85b82f14bc246f82457"title="Private pointer to function, containing the function to be used as activation function...">actv_f</a>)(double);
<aname="l00098"></a><aclass="code"href="classneuralpp_1_1NeuralNet.html#e2b4e8405f9d25edab395d61502bdba9">00098</a><aclass="code"href="classneuralpp_1_1Layer.html"title="Class for managing layers of neurons.">Layer</a>* <aclass="code"href="classneuralpp_1_1NeuralNet.html#e2b4e8405f9d25edab395d61502bdba9">input</a>;
<aname="l00099"></a><aclass="code"href="classneuralpp_1_1NeuralNet.html#bbdaa1b6c0a1a95d2b18cd25fda2a266">00099</a><aclass="code"href="classneuralpp_1_1Layer.html"title="Class for managing layers of neurons.">Layer</a>* <aclass="code"href="classneuralpp_1_1NeuralNet.html#bbdaa1b6c0a1a95d2b18cd25fda2a266">hidden</a>;
<aname="l00100"></a><aclass="code"href="classneuralpp_1_1NeuralNet.html#fa9b2dbcbb39d0fc70f790ac24069a74">00100</a><aclass="code"href="classneuralpp_1_1Layer.html"title="Class for managing layers of neurons.">Layer</a>* <aclass="code"href="classneuralpp_1_1NeuralNet.html#fa9b2dbcbb39d0fc70f790ac24069a74">output</a>;
<aname="l00101"></a>00101
<aname="l00105"></a><aclass="code"href="classneuralpp_1_1NeuralNet.html#94c36c94060e785ea67a0014c4182f8f6d06b4fe9414a158c97aee1a3679a904">00105</a><spanclass="keyword">typedef</span><spanclass="keyword">enum</span> { <aclass="code"href="classneuralpp_1_1NeuralNet.html#94c36c94060e785ea67a0014c4182f8f5ec2727c0756ddb097b53efe49b81afb">file</a>, <aclass="code"href="classneuralpp_1_1NeuralNet.html#94c36c94060e785ea67a0014c4182f8f6d06b4fe9414a158c97aee1a3679a904">str</a> } <aclass="code"href="classneuralpp_1_1NeuralNet.html#94c36c94060e785ea67a0014c4182f8f"title="Enum to choose the eventual training source for our network (XML from a file or from...">source</a>;
<aname="l00106"></a>00106
<aname="l00110"></a><aclass="code"href="classneuralpp_1_1NeuralNet.html#92b145f2f6f00bf1ba645ce2235882c2">00110</a><aclass="code"href="classneuralpp_1_1NeuralNet.html#92b145f2f6f00bf1ba645ce2235882c2"title="Empty constructor for the class - it just makes nothing.">NeuralNet</a>() {}
<aname="l00111"></a>00111
<aname="l00125"></a>00125 <aclass="code"href="classneuralpp_1_1NeuralNet.html#92b145f2f6f00bf1ba645ce2235882c2"title="Empty constructor for the class - it just makes nothing.">NeuralNet</a> (<spanclass="keywordtype">size_t</span> in_size, <spanclass="keywordtype">size_t</span> hidden_size, <spanclass="keywordtype">size_t</span> out_size, <spanclass="keywordtype">double</span> l,
<aname="l00133"></a>00133 <aclass="code"href="classneuralpp_1_1NeuralNet.html#92b145f2f6f00bf1ba645ce2235882c2"title="Empty constructor for the class - it just makes nothing.">NeuralNet</a> (<spanclass="keyword">const</span> std::string <aclass="code"href="classneuralpp_1_1NeuralNet.html#94c36c94060e785ea67a0014c4182f8f5ec2727c0756ddb097b53efe49b81afb">file</a>) <spanclass="keywordflow">throw</span>(<aclass="code"href="classneuralpp_1_1NetworkFileNotFoundException.html"title="Exception thrown when doing an attempt to load a network from an invalid file.">NetworkFileNotFoundException</a>);
<aname="l00134"></a>00134
<aname="l00140"></a>00140 <spanclass="keywordtype">double</span><aclass="code"href="classneuralpp_1_1NeuralNet.html#961dce8913264bf64c899dce4e25f810"title="It gets the output of the network (note: the layer output should contain an only...">getOutput</a>() <spanclass="keyword">const</span>;
<aname="l00141"></a>00141
<aname="l00146"></a>00146 <spanclass="keywordtype">double</span><aclass="code"href="classneuralpp_1_1NeuralNet.html#e08cdcf4b70f987700e553d9914f6179"title="Get the threshold of the neurons in the network.">getThreshold</a>() <spanclass="keyword">const</span>;
<aname="l00147"></a>00147
<aname="l00152"></a>00152 std::vector<double><aclass="code"href="classneuralpp_1_1NeuralNet.html#e6d2215ecc8b560db2f6797db642191c"title="It gets the output of the network in case the output layer contains more neurons...">getOutputs</a>();
<aname="l00153"></a>00153
<aname="l00159"></a>00159 <spanclass="keywordtype">double</span><aclass="code"href="classneuralpp_1_1NeuralNet.html#562dfe9fb8d73bf25a23ce608451d3aa"title="Get the expected value (in case you have an only neuron in output layer).">expected</a>() <spanclass="keyword">const</span>;
<aname="l00160"></a>00160
<aname="l00166"></a>00166 std::vector<double><aclass="code"href="classneuralpp_1_1NeuralNet.html#51a1851ed07b85bec091c9053ae99cf7"title="Get the expected value (in case you have an only neuron in output layer).">getExpected</a>() <spanclass="keyword">const</span>;
<aname="l00167"></a>00167
<aname="l00172"></a>00172 <spanclass="keywordtype">void</span><aclass="code"href="classneuralpp_1_1NeuralNet.html#b6475762b7e9eab086befdc511f7c236"title="It sets the value you expect from your network (in case the network has an only neuron...">setExpected</a>(<spanclass="keywordtype">double</span> ex);
<aname="l00173"></a>00173
<aname="l00178"></a>00178 <spanclass="keywordtype">void</span><aclass="code"href="classneuralpp_1_1NeuralNet.html#b6475762b7e9eab086befdc511f7c236"title="It sets the value you expect from your network (in case the network has an only neuron...">setExpected</a>(std::vector<double> ex);
<aname="l00179"></a>00179
<aname="l00185"></a>00185 <spanclass="keywordtype">void</span><aclass="code"href="classneuralpp_1_1NeuralNet.html#b0bd1daadb06980dff1f50d33a7c098e"title="It updates through back-propagation the weights of the synapsis and computes again...">update</a>();
<aname="l00186"></a>00186
<aname="l00191"></a>00191 <spanclass="keywordtype">void</span><aclass="code"href="classneuralpp_1_1NeuralNet.html#c129c180647362da963758bfd1ba6890"title="It propagates values through the network.">propagate</a>();
<aname="l00192"></a>00192
<aname="l00197"></a>00197 <spanclass="keywordtype">void</span><aclass="code"href="classneuralpp_1_1NeuralNet.html#405b32d2928344314ecf0469070b0f17"title="It sets the input for the network.">setInput</a> (std::vector<double> v);
<aname="l00203"></a>00203 <spanclass="keywordtype">void</span><aclass="code"href="classneuralpp_1_1NeuralNet.html#46f23f462318a4ffc037a4e806364c3f"title="It links the layers of the network (input, hidden, output).">link</a>();
<aname="l00211"></a>00211 <spanclass="keywordtype">void</span><aclass="code"href="classneuralpp_1_1NeuralNet.html#fdf94c276720c25e565cac834fe8a407"title="Save a trained neural network to a binary file.">save</a> (<spanclass="keyword">const</span><spanclass="keywordtype">char</span>* fname) <spanclass="keywordflow">throw</span>(<aclass="code"href="classneuralpp_1_1NetworkFileWriteException.html"title="Exception thrown when trying to write the network&#39;s information to a file that...">NetworkFileWriteException</a>);
<aname="l00220"></a>00220 <spanclass="keywordtype">void</span><aclass="code"href="classneuralpp_1_1NeuralNet.html#1c9e17437d41a7048611e21a3cc1c7dd"title="Train a network using a training set loaded from an XML file.">train</a> (std::string xml, <aclass="code"href="classneuralpp_1_1NeuralNet.html#94c36c94060e785ea67a0014c4182f8f"title="Enum to choose the eventual training source for our network (XML from a file or from...">source</a> src) <spanclass="keywordflow">throw</span>(<aclass="code"href="classneuralpp_1_1InvalidXMLException.html"title="Exception thrown when trying parsing an invalid XML.">InvalidXMLException</a>);
<aname="l00221"></a>00221
<aname="l00226"></a>00226 <spanclass="keyword">static</span><spanclass="keywordtype">void</span><aclass="code"href="classneuralpp_1_1NeuralNet.html#96da6712a72051cf34ad961761ef6e08"title="Initialize the training XML for the neural network.">initXML</a> (std::string& xml);
<aname="l00227"></a>00227
<aname="l00234"></a>00234 <spanclass="keyword">static</span> std::vector<double><aclass="code"href="classneuralpp_1_1NeuralNet.html#83c6555520856d5867752904349ab6ca"title="Splits a string into a vector of doubles, given a delimitator.">split</a> (<spanclass="keywordtype">char</span> delim, std::string <aclass="code"href="classneuralpp_1_1NeuralNet.html#94c36c94060e785ea67a0014c4182f8f6d06b4fe9414a158c97aee1a3679a904">str</a>);
<aname="l00235"></a>00235
<aname="l00248"></a>00248 <spanclass="keyword">static</span> std::string <aclass="code"href="classneuralpp_1_1NeuralNet.html#0a2733037af912b3e6a10146e7b7172f"title="Get a training set from a string and copies it to an XML For example, these strings...">XMLFromSet</a> (<spanclass="keywordtype">int</span>&<spanclass="keywordtype">id</span>, std::string <spanclass="keyword">set</span>);
<aname="l00249"></a>00249
<aname="l00254"></a>00254 <spanclass="keyword">static</span><spanclass="keywordtype">void</span><aclass="code"href="classneuralpp_1_1NeuralNet.html#e17732ed578bc4bd6032bfae58a5cf51"title="Closes an open XML document generated by &quot;initXML&quot; and &quot;XMLFromSet&quot;...">closeXML</a>(std::string& xml);
<aname="l00255"></a>00255 };
<aname="l00256"></a>00256
<aname="l00262"></a><aclass="code"href="classneuralpp_1_1Synapsis.html">00262</a><spanclass="keyword">class </span><aclass="code"href="classneuralpp_1_1Synapsis.html"title="Class for managing synapsis.">Synapsis</a> {
<aname="l00267"></a><aclass="code"href="classneuralpp_1_1Synapsis.html#83d41c158054b08bd05051736e89a0ad">00267</a><aclass="code"href="classneuralpp_1_1Neuron.html"title="Class for managing neurons.">Neuron</a> *<aclass="code"href="classneuralpp_1_1Synapsis.html#83d41c158054b08bd05051736e89a0ad">in</a>;
<aname="l00268"></a><aclass="code"href="classneuralpp_1_1Synapsis.html#fb219e05038fa0da20db1082ab0500be">00268</a><aclass="code"href="classneuralpp_1_1Neuron.html"title="Class for managing neurons.">Neuron</a> *<aclass="code"href="classneuralpp_1_1Synapsis.html#fb219e05038fa0da20db1082ab0500be">out</a>;
<aname="l00280"></a>00280 <aclass="code"href="classneuralpp_1_1Synapsis.html#0729de9e737b9967421edcfc4b410bd8"title="Constructor.">Synapsis</a>(<aclass="code"href="classneuralpp_1_1Neuron.html"title="Class for managing neurons.">Neuron</a>* i, <aclass="code"href="classneuralpp_1_1Neuron.html"title="Class for managing neurons.">Neuron</a>* o, <spanclass="keywordtype">double</span> w, <spanclass="keywordtype">double</span> d);
<aname="l00281"></a>00281
<aname="l00288"></a>00288 <aclass="code"href="classneuralpp_1_1Synapsis.html#0729de9e737b9967421edcfc4b410bd8"title="Constructor.">Synapsis</a> (<aclass="code"href="classneuralpp_1_1Neuron.html"title="Class for managing neurons.">Neuron</a>* i, <aclass="code"href="classneuralpp_1_1Neuron.html"title="Class for managing neurons.">Neuron</a>* o, <spanclass="keywordtype">double</span>(*a)(<spanclass="keywordtype">double</span>));
<aname="l00289"></a>00289
<aname="l00297"></a>00297 <aclass="code"href="classneuralpp_1_1Synapsis.html#0729de9e737b9967421edcfc4b410bd8"title="Constructor.">Synapsis</a> (<aclass="code"href="classneuralpp_1_1Neuron.html"title="Class for managing neurons.">Neuron</a>* i, <aclass="code"href="classneuralpp_1_1Neuron.html"title="Class for managing neurons.">Neuron</a>* o,
<aname="l00303"></a>00303 <aclass="code"href="classneuralpp_1_1Neuron.html"title="Class for managing neurons.">Neuron</a>* <aclass="code"href="classneuralpp_1_1Synapsis.html#298fd3c7483ad572899fecec01ac8fdf">getIn</a>() <spanclass="keyword">const</span>;
<aname="l00304"></a>00304
<aname="l00308"></a>00308 <aclass="code"href="classneuralpp_1_1Neuron.html"title="Class for managing neurons.">Neuron</a>* <aclass="code"href="classneuralpp_1_1Synapsis.html#b46d876761a73a24db87f5a144a0e899">getOut</a>() <spanclass="keyword">const</span>;
<aname="l00309"></a>00309
<aname="l00314"></a>00314 <spanclass="keywordtype">void</span><aclass="code"href="classneuralpp_1_1Synapsis.html#acee77d0fdf9889464ab5ed27beae0ff"title="Set the weight of the synapsis.">setWeight</a>(<spanclass="keywordtype">double</span> w) <spanclass="keywordflow">throw</span>(<aclass="code"href="classneuralpp_1_1InvalidSynapticalWeightException.html"title="Exception raised when, while trying the network or directly, the weight of a synapsis...">InvalidSynapticalWeightException</a>);
<aname="l00315"></a>00315
<aname="l00321"></a>00321 <spanclass="keywordtype">void</span><aclass="code"href="classneuralpp_1_1Synapsis.html#429ad5b25930faf436a9d725582802e1"title="It sets the delta (how much to change the weight after an update) of the synapsis...">setDelta</a>(<spanclass="keywordtype">double</span> d) <spanclass="keywordflow">throw</span>(<aclass="code"href="classneuralpp_1_1InvalidSynapticalWeightException.html"title="Exception raised when, while trying the network or directly, the weight of a synapsis...">InvalidSynapticalWeightException</a>);
<aname="l00322"></a>00322
<aname="l00327"></a>00327 <spanclass="keywordtype">double</span><aclass="code"href="classneuralpp_1_1Synapsis.html#bcbf7228632ff4d6bbb67703323d2db0"title="Return the weight of the synapsis.">getWeight</a>() <spanclass="keyword">const</span>;
<aname="l00328"></a>00328
<aname="l00333"></a>00333 <spanclass="keywordtype">double</span><aclass="code"href="classneuralpp_1_1Synapsis.html#00c8e9c0804662f2b3247d6dddb4ca6c"title="Return the delta of the synapsis.">getDelta</a>() <spanclass="keyword">const</span>;
<aname="l00334"></a>00334
<aname="l00339"></a>00339 <spanclass="keywordtype">double</span><aclass="code"href="classneuralpp_1_1Synapsis.html#0148b9c8db870c928711168702ae51c5"title="Get the delta of the synapsis at the previous iteration.">getPrevDelta</a>() <spanclass="keyword">const</span>;
<aname="l00340"></a>00340
<aname="l00351"></a>00351 <spanclass="keywordtype">double</span><aclass="code"href="classneuralpp_1_1Synapsis.html#cff10a022d4c021688e4df944c05d8bd"title="Get the inertial momentum of a synapsis.">momentum</a> (<spanclass="keywordtype">int</span> N, <spanclass="keywordtype">int</span> x) <spanclass="keyword">const</span>;
<aname="l00352"></a>00352 };
<aname="l00353"></a>00353
<aname="l00359"></a><aclass="code"href="classneuralpp_1_1Neuron.html">00359</a><spanclass="keyword">class </span><aclass="code"href="classneuralpp_1_1Neuron.html"title="Class for managing neurons.">Neuron</a> {
<aname="l00394"></a>00394 <aclass="code"href="classneuralpp_1_1Synapsis.html"title="Class for managing synapsis.">Synapsis</a>&<aclass="code"href="classneuralpp_1_1Neuron.html#29f2d9dcc4ca34f224d4dc39bb2f180a"title="Get the i-th synapsis connected on the input of the neuron.">synIn</a> (<spanclass="keywordtype">size_t</span> i);
<aname="l00395"></a>00395
<aname="l00401"></a>00401 <aclass="code"href="classneuralpp_1_1Synapsis.html"title="Class for managing synapsis.">Synapsis</a>&<aclass="code"href="classneuralpp_1_1Neuron.html#655f1637e1b754461413ac7fc2ffeebe"title="Get the i-th synapsis connected on the output of the neuron.">synOut</a> (<spanclass="keywordtype">size_t</span> i);
<aname="l00402"></a>00402
<aname="l00407"></a>00407 <spanclass="keywordtype">void</span><aclass="code"href="classneuralpp_1_1Neuron.html#4d252151c35839975838539d846d70be"title="It pushes a new input synapsis.">push_in</a> (<aclass="code"href="classneuralpp_1_1Synapsis.html"title="Class for managing synapsis.">Synapsis</a> s);
<aname="l00408"></a>00408
<aname="l00413"></a>00413 <spanclass="keywordtype">void</span><aclass="code"href="classneuralpp_1_1Neuron.html#2c0acb0e6d413c4e0fc9e7939da1a684"title="It pushes a new output synapsis.">push_out</a> (<aclass="code"href="classneuralpp_1_1Synapsis.html"title="Class for managing synapsis.">Synapsis</a> s);
<aname="l00414"></a>00414
<aname="l00419"></a>00419 <spanclass="keywordtype">void</span><aclass="code"href="classneuralpp_1_1Neuron.html#ddf00ffef030b27ed11901aad08822bd"title="Change the activation value of the neuron.">setActv</a> (<spanclass="keywordtype">double</span> a);
<aname="l00420"></a>00420
<aname="l00425"></a>00425 <spanclass="keywordtype">void</span><aclass="code"href="classneuralpp_1_1Neuron.html#aa6e58f073a76b3481fea9115a4e6ea6"title="Change the propagation value of the neuron.">setProp</a> (<spanclass="keywordtype">double</span> p);
<aname="l00426"></a>00426
<aname="l00431"></a>00431 <spanclass="keywordtype">double</span><aclass="code"href="classneuralpp_1_1Neuron.html#55993867179f0ac7d1e0e2c460ceb611"title="Get the activation value of the neuron.">getActv</a>();
<aname="l00432"></a>00432
<aname="l00437"></a>00437 <spanclass="keywordtype">double</span><aclass="code"href="classneuralpp_1_1Neuron.html#57c022f82213f662e2a263fc134a3fc9"title="Get the propagation value of the neuron.">getProp</a>();
<aname="l00438"></a>00438
<aname="l00442"></a>00442 <spanclass="keywordtype">void</span><aclass="code"href="classneuralpp_1_1Neuron.html#928d9bf5aed600119c640779e4034f30"title="Compute the propagation value of the neuron and set it.">propagate</a>();
<aname="l00443"></a>00443
<aname="l00448"></a>00448 <spanclass="keywordtype">size_t</span><aclass="code"href="classneuralpp_1_1Neuron.html#ad97f1a082d5f969eb4c69ab454ecfbb"title="Get the number of input synapsis for the neuron.">nIn</a>();
<aname="l00449"></a>00449
<aname="l00454"></a>00454 <spanclass="keywordtype">size_t</span><aclass="code"href="classneuralpp_1_1Neuron.html#fe458021e3b20d58dc608fb94ae2135b"title="Get the number of output synapsis for the neuron.">nOut</a>();
<aname="l00455"></a>00455
<aname="l00459"></a>00459 <spanclass="keywordtype">void</span><aclass="code"href="classneuralpp_1_1Neuron.html#2e2ccb69277fc3d992a3a3f2360ed154"title="Remove input and output synapsis from a neuron.">synClear</a>();
<aname="l00460"></a>00460 };
<aname="l00461"></a>00461
<aname="l00467"></a><aclass="code"href="classneuralpp_1_1Layer.html">00467</a><spanclass="keyword">class </span><aclass="code"href="classneuralpp_1_1Layer.html"title="Class for managing layers of neurons.">Layer</a> {
<aname="l00499"></a>00499 <aclass="code"href="classneuralpp_1_1Neuron.html"title="Class for managing neurons.">Neuron</a>&<aclass="code"href="classneuralpp_1_1Layer.html#45ff7554830558155c6fbce3b6827122"title="Redefinition for operator [].">operator[] </a>(<spanclass="keywordtype">size_t</span> i) <spanclass="keywordflow">throw</span>(<aclass="code"href="classneuralpp_1_1NetworkIndexOutOfBoundsException.html"title="Exception raised when trying to access a neuron whose index is larger than the number...">NetworkIndexOutOfBoundsException</a>);
<aname="l00500"></a>00500
<aname="l00505"></a>00505 <spanclass="keywordtype">void</span><aclass="code"href="classneuralpp_1_1Layer.html#ac33444fde14633fa1ad4acb024ad150"title="It links a layer to another.">link</a> (<aclass="code"href="classneuralpp_1_1Layer.html"title="Class for managing layers of neurons.">Layer</a>& l);
<aname="l00511"></a>00511 <spanclass="keywordtype">void</span><aclass="code"href="classneuralpp_1_1Layer.html#88ceffc23f02a9dc24d4355767b7cca7"title="Set the input values for the neurons of the layer (just use it for the input layer)...">setInput</a> (std::vector<double> v);
<aname="l00512"></a>00512
<aname="l00516"></a>00516 <spanclass="keywordtype">void</span><aclass="code"href="classneuralpp_1_1Layer.html#fcfd306039dbaf91c9e2dcc8fc1f1ce1"title="It propagates its activation values to the output layers.">propagate</a>();