<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="l00060"></a>00060 <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="l00061"></a>00061
<aname="l00067"></a>00067 <spanclass="keywordtype">void</span><aclass="code"href="classneuralpp_1_1NeuralNet.html#62695a82dfb1df758a44150921aec8e0"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="l00074"></a>00074 <spanclass="keywordtype">double</span><aclass="code"href="classneuralpp_1_1NeuralNet.html#0616c51404efaca2714e37dd7478997e"title="Get the error made on the expected result as |v-v&#39;|/v.">error</a>(<spanclass="keywordtype">double</span><aclass="code"href="classneuralpp_1_1NeuralNet.html#261f5f68fcc5be54250cfa03945266dd">ex</a>) <spanclass="keyword">const</span>;
<aname="l00075"></a>00075
<aname="l00080"></a>00080 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="l00083"></a><aclass="code"href="classneuralpp_1_1NeuralNet.html#e2b4e8405f9d25edab395d61502bdba9">00083</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="l00084"></a><aclass="code"href="classneuralpp_1_1NeuralNet.html#bbdaa1b6c0a1a95d2b18cd25fda2a266">00084</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="l00085"></a><aclass="code"href="classneuralpp_1_1NeuralNet.html#fa9b2dbcbb39d0fc70f790ac24069a74">00085</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="l00086"></a>00086
<aname="l00090"></a><aclass="code"href="classneuralpp_1_1NeuralNet.html#94c36c94060e785ea67a0014c4182f8f6d06b4fe9414a158c97aee1a3679a904">00090</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="l00091"></a>00091
<aname="l00095"></a><aclass="code"href="classneuralpp_1_1NeuralNet.html#92b145f2f6f00bf1ba645ce2235882c2">00095</a><aclass="code"href="classneuralpp_1_1NeuralNet.html#92b145f2f6f00bf1ba645ce2235882c2"title="Empty constructor for the class - it just makes nothing.">NeuralNet</a>() {}
<aname="l00096"></a>00096
<aname="l00107"></a>00107 <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, <spanclass="keywordtype">int</span> e);
<aname="l00108"></a>00108
<aname="l00114"></a>00114 <aclass="code"href="classneuralpp_1_1NeuralNet.html#92b145f2f6f00bf1ba645ce2235882c2"title="Empty constructor for the class - it just makes nothing.">NeuralNet</a> (<spanclass="keyword">const</span><spanclass="keywordtype">string</span><aclass="code"href="classneuralpp_1_1NeuralNet.html#94c36c94060e785ea67a0014c4182f8f5ec2727c0756ddb097b53efe49b81afb">file</a>) <spanclass="keywordflow">throw</span>(NetworkFileNotFoundException);
<aname="l00115"></a>00115
<aname="l00116"></a>00116
<aname="l00128"></a>00128 <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,
<aname="l00136"></a>00136 <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="l00137"></a>00137
<aname="l00142"></a>00142 vector<double><aclass="code"href="classneuralpp_1_1NeuralNet.html#a6b8bf3800b43b58843c65fc431207ae"title="It gets the output of the network in case the output layer contains more neurons...">getOutputs</a>();
<aname="l00143"></a>00143
<aname="l00149"></a>00149 <spanclass="keywordtype">double</span><aclass="code"href="classneuralpp_1_1NeuralNet.html#562dfe9fb8d73bf25a23ce608451d3aa"title="It gets the value expected.">expected</a>() <spanclass="keyword">const</span>;
<aname="l00150"></a>00150
<aname="l00155"></a>00155 <spanclass="keywordtype">void</span><aclass="code"href="classneuralpp_1_1NeuralNet.html#b6475762b7e9eab086befdc511f7c236"title="It sets the value you expect from your network.">setExpected</a>(<spanclass="keywordtype">double</span><aclass="code"href="classneuralpp_1_1NeuralNet.html#261f5f68fcc5be54250cfa03945266dd">ex</a>);
<aname="l00156"></a>00156
<aname="l00162"></a>00162 <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="l00163"></a>00163
<aname="l00168"></a>00168 <spanclass="keywordtype">void</span><aclass="code"href="classneuralpp_1_1NeuralNet.html#c129c180647362da963758bfd1ba6890"title="It propagates values through the network.">propagate</a>();
<aname="l00169"></a>00169
<aname="l00174"></a>00174 <spanclass="keywordtype">void</span><aclass="code"href="classneuralpp_1_1NeuralNet.html#0de170e8ab561ad63d0739b4c4b74f68"title="It sets the input for the network.">setInput</a> (vector<double>& v);
<aname="l00175"></a>00175
<aname="l00180"></a>00180 <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="l00181"></a>00181
<aname="l00188"></a>00188 <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>(NetworkFileWriteException);
<aname="l00189"></a>00189
<aname="l00197"></a>00197 <spanclass="keywordtype">void</span><aclass="code"href="classneuralpp_1_1NeuralNet.html#ead4bdef0602a5cadbe3beb685e01f5f"title="Train a network using a training set loaded from an XML file.">train</a> (<spanclass="keywordtype">string</span> 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>(InvalidXMLException);
<aname="l00198"></a>00198
<aname="l00203"></a>00203 <spanclass="keyword">static</span><spanclass="keywordtype">void</span><aclass="code"href="classneuralpp_1_1NeuralNet.html#45c7645d4affe65752d37cd230afba24"title="Initialize the training XML for the neural network.">initXML</a> (<spanclass="keywordtype">string</span>& xml);
<aname="l00204"></a>00204
<aname="l00211"></a>00211 <spanclass="keyword">static</span> vector<double><aclass="code"href="classneuralpp_1_1NeuralNet.html#e07af23ceb8666518da0c035bf1e0376"title="Splits a string into a vector of doubles, given a delimitator.">split</a> (<spanclass="keywordtype">char</span> delim, <spanclass="keywordtype">string</span><aclass="code"href="classneuralpp_1_1NeuralNet.html#94c36c94060e785ea67a0014c4182f8f6d06b4fe9414a158c97aee1a3679a904">str</a>);
<aname="l00225"></a>00225 <spanclass="keyword">static</span><spanclass="keywordtype">string</span><aclass="code"href="classneuralpp_1_1NeuralNet.html#4be31ecb0b543a192997bd83c6995ccb"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>, <spanclass="keywordtype">string</span><spanclass="keyword">set</span>);
<aname="l00226"></a>00226
<aname="l00231"></a>00231 <spanclass="keyword">static</span><spanclass="keywordtype">void</span><aclass="code"href="classneuralpp_1_1NeuralNet.html#28b9966c5f197b8e86d57dd104aa32a6"title="Closes an open XML document generated by &quot;initXML&quot; and &quot;XMLFromSet&quot;...">closeXML</a>(<spanclass="keywordtype">string</span>& xml);
<aname="l00232"></a>00232 };
<aname="l00233"></a>00233
<aname="l00239"></a><aclass="code"href="classneuralpp_1_1Synapsis.html">00239</a><spanclass="keyword">class </span><aclass="code"href="classneuralpp_1_1Synapsis.html"title="Class for managing synapsis.">Synapsis</a> {
<aname="l00244"></a><aclass="code"href="classneuralpp_1_1Synapsis.html#83d41c158054b08bd05051736e89a0ad">00244</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="l00245"></a><aclass="code"href="classneuralpp_1_1Synapsis.html#fb219e05038fa0da20db1082ab0500be">00245</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="l00257"></a>00257 <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="l00258"></a>00258
<aname="l00265"></a>00265 <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="l00266"></a>00266
<aname="l00274"></a>00274 <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="l00280"></a>00280 <aclass="code"href="classneuralpp_1_1Neuron.html"title="Class for managing neurons.">Neuron</a>* <aclass="code"href="classneuralpp_1_1Synapsis.html#5ba8a93a5741f4855390eb8a46e99435">getIn</a>();
<aname="l00281"></a>00281
<aname="l00285"></a>00285 <aclass="code"href="classneuralpp_1_1Neuron.html"title="Class for managing neurons.">Neuron</a>* <aclass="code"href="classneuralpp_1_1Synapsis.html#61c9a04e03291a01f44520cef143cbdd">getOut</a>();
<aname="l00286"></a>00286
<aname="l00291"></a>00291 <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>(InvalidSynapticalWeightException);
<aname="l00298"></a>00298 <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>(InvalidSynapticalWeightException);
<aname="l00299"></a>00299
<aname="l00304"></a>00304 <spanclass="keywordtype">double</span><aclass="code"href="classneuralpp_1_1Synapsis.html#aa79c16ec6b59949e5d2f75a3f10d530"title="Return the weight of the synapsis.">getWeight</a>();
<aname="l00305"></a>00305
<aname="l00310"></a>00310 <spanclass="keywordtype">double</span><aclass="code"href="classneuralpp_1_1Synapsis.html#18f15b920609be8b818d43a0227aada5"title="Return the delta of the synapsis.">getDelta</a>();
<aname="l00311"></a>00311
<aname="l00316"></a>00316 <spanclass="keywordtype">double</span><aclass="code"href="classneuralpp_1_1Synapsis.html#2fe3e9ec97542f1476d8b9306aa09756"title="Get the delta of the synapsis at the previous iteration.">getPrevDelta</a>();
<aname="l00317"></a>00317
<aname="l00328"></a>00328 <spanclass="keywordtype">double</span><aclass="code"href="classneuralpp_1_1Synapsis.html#ecdb17182de791f7fdd417232e184350"title="Get the inertial momentum of a synapsis.">momentum</a> (<spanclass="keywordtype">int</span> N, <spanclass="keywordtype">int</span> x);
<aname="l00329"></a>00329 };
<aname="l00330"></a>00330
<aname="l00336"></a><aclass="code"href="classneuralpp_1_1Neuron.html">00336</a><spanclass="keyword">class </span><aclass="code"href="classneuralpp_1_1Neuron.html"title="Class for managing neurons.">Neuron</a> {
<aname="l00366"></a>00366 <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="l00367"></a>00367
<aname="l00373"></a>00373 <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="l00374"></a>00374
<aname="l00379"></a>00379 <spanclass="keywordtype">void</span><aclass="code"href="classneuralpp_1_1Neuron.html#583ada6e1dd3f2e113415b4d89196e62"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="l00385"></a>00385 <spanclass="keywordtype">void</span><aclass="code"href="classneuralpp_1_1Neuron.html#bca65db84f56f9d40694bfbcd25812cb"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="l00386"></a>00386
<aname="l00391"></a>00391 <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="l00392"></a>00392
<aname="l00397"></a>00397 <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="l00398"></a>00398
<aname="l00403"></a>00403 <spanclass="keywordtype">double</span><aclass="code"href="classneuralpp_1_1Neuron.html#55993867179f0ac7d1e0e2c460ceb611"title="Get the activation value of the neuron.">getActv</a>();
<aname="l00404"></a>00404
<aname="l00409"></a>00409 <spanclass="keywordtype">double</span><aclass="code"href="classneuralpp_1_1Neuron.html#57c022f82213f662e2a263fc134a3fc9"title="Get the propagation value of the neuron.">getProp</a>();
<aname="l00410"></a>00410
<aname="l00414"></a>00414 <spanclass="keywordtype">double</span><aclass="code"href="classneuralpp_1_1Neuron.html#8b0ca61cd0e047c8691ab39aae56dbda"title="It propagates its activation value to the connected neurons.">propagate</a>();
<aname="l00415"></a>00415
<aname="l00420"></a>00420 <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="l00421"></a>00421
<aname="l00426"></a>00426 <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="l00427"></a>00427
<aname="l00431"></a>00431 <spanclass="keywordtype">void</span><aclass="code"href="classneuralpp_1_1Neuron.html#2e2ccb69277fc3d992a3a3f2360ed154"title="Remove input and output synapsis from a neuron.">synClear</a>();
<aname="l00432"></a>00432 };
<aname="l00433"></a>00433
<aname="l00439"></a><aclass="code"href="classneuralpp_1_1Layer.html">00439</a><spanclass="keyword">class </span><aclass="code"href="classneuralpp_1_1Layer.html"title="Class for managing layers of neurons.">Layer</a> {
<aname="l00472"></a>00472 <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="l00473"></a>00473
<aname="l00478"></a>00478 <spanclass="keywordtype">void</span><aclass="code"href="classneuralpp_1_1Layer.html#dbad95f635343a2998794113d7762cf7"title="It sets a vector of propagation values to all its neurons.">setProp</a> (vector<double>& v);
<aname="l00479"></a>00479
<aname="l00484"></a>00484 <spanclass="keywordtype">void</span><aclass="code"href="classneuralpp_1_1Layer.html#98f79281b680b1d18fd91fa5794c0899"title="It sets a vector of activation values to all its neurons.">setActv</a> (vector<double>& v);
<aname="l00485"></a>00485
<aname="l00489"></a>00489 <spanclass="keywordtype">void</span><aclass="code"href="classneuralpp_1_1Layer.html#fcfd306039dbaf91c9e2dcc8fc1f1ce1"title="It propagates its activation values to the output layers.">propagate</a>();