neuralpp/doc/html/examples_2networkForSumsAndSubtractions_8cpp-example.html

67 lines
4.3 KiB
HTML
Raw Permalink Normal View History

2009-09-04 11:27:14 +02:00
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Neural++: examples/networkForSumsAndSubtractions.cpp</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
<link href="tabs.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.5.6 -->
<div class="navigation" id="top">
<div class="tabs">
<ul>
<li><a href="index.html"><span>Main&nbsp;Page</span></a></li>
<li><a href="namespaces.html"><span>Namespaces</span></a></li>
<li><a href="annotated.html"><span>Classes</span></a></li>
<li><a href="files.html"><span>Files</span></a></li>
<li><a href="examples.html"><span>Examples</span></a></li>
</ul>
</div>
</div>
<div class="contents">
<h1>examples/networkForSumsAndSubtractions.cpp</h1>This program creates a neural network from scratch. Its purpose is to get two numbers and learn to compute their sum and difference (so the network provides two output values). The training set is auto-generated to an XML string, and then the network is trained.<p>
<div class="fragment"><pre class="fragment">
<span class="preprocessor">#include &lt;iostream&gt;</span>
<span class="preprocessor">#include &lt;<a class="code" href="neural_09_09_8hpp.html">neural++.hpp</a>&gt;</span>
<span class="keyword">using namespace </span>std;
<span class="keyword">using namespace </span>neuralpp;
<span class="keywordtype">int</span> main() {
<a name="_a0"></a><a class="code" href="classneuralpp_1_1NeuralNet.html" title="Main project&amp;#39;s class.">NeuralNet</a> net(2, 2, 2, 0.002, 1000);
<span class="keywordtype">string</span> xml;
<span class="keywordtype">double</span> tmp;
<span class="keywordtype">int</span> <span class="keywordtype">id</span> = 0;
<span class="comment">// XML initialization. Then, I say XML that 3+2=5, 3-2=1; 4+2=6, 4-2=2; 6+3=9, 6-3=3</span>
<span class="comment">// Strings' format is "input1,input2,...,inputn;output1,output2,...,outputm</span>
NeuralNet::initXML(xml);
xml += NeuralNet::XMLFromSet(<span class="keywordtype">id</span>, <span class="stringliteral">"3,2;5,1"</span>);
xml += NeuralNet::XMLFromSet(<span class="keywordtype">id</span>, <span class="stringliteral">"4,2;6,2"</span>);
xml += NeuralNet::XMLFromSet(<span class="keywordtype">id</span>, <span class="stringliteral">"6,3;9,3"</span>);
NeuralNet::closeXML(xml);
cout &lt;&lt; xml &lt;&lt; endl;
net.<a name="a1"></a><a class="code" href="classneuralpp_1_1NeuralNet.html#1c9e17437d41a7048611e21a3cc1c7dd" title="Train a network using a training set loaded from an XML file.">train</a>(xml, NeuralNet::str);
vector&lt;double&gt; v;
cout &lt;&lt; <span class="stringliteral">"Network status: trained\n\n"</span>;
cout &lt;&lt; <span class="stringliteral">"First number: "</span>;
cin &gt;&gt; tmp;
v.push_back(tmp);
cout &lt;&lt; <span class="stringliteral">"Second number: "</span>;
cin &gt;&gt; tmp;
v.push_back(tmp);
net.<a name="a2"></a><a class="code" href="classneuralpp_1_1NeuralNet.html#405b32d2928344314ecf0469070b0f17" title="It sets the input for the network.">setInput</a>(v);
net.<a name="a3"></a><a class="code" href="classneuralpp_1_1NeuralNet.html#c129c180647362da963758bfd1ba6890" title="It propagates values through the network.">propagate</a>();
cout &lt;&lt; <span class="stringliteral">"Output: "</span> &lt;&lt; net.<a name="a4"></a><a class="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>()[0] &lt;&lt; <span class="stringliteral">"; "</span> &lt;&lt; net.<a class="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>()[1] &lt;&lt; endl;
<span class="keywordflow">return</span> 0;
}
</pre></div> </div>
<hr size="1"><address style="text-align: right;"><small>Generated on Fri Sep 4 11:25:49 2009 for Neural++ by&nbsp;
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.6 </small></address>
</body>
</html>