mirror of
https://github.com/BlackLight/neuralpp.git
synced 2024-12-28 04:05:12 +01:00
91 lines
6.6 KiB
HTML
91 lines
6.6 KiB
HTML
<!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/learnAdd.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 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/learnAdd.cpp</h1>Show how to train a network that performs sums between two real numbers. The training XML is built from scratch, then saved to a file, then the network is initialized using that XML file, trained, and the resulting trained network is saved to adder.net. Then, you should take a look at doAdd.cpp to see how to use that file to use the network.<p>
|
|
<div class="fragment"><pre class="fragment">
|
|
<span class="preprocessor">#include <iostream></span>
|
|
<span class="preprocessor">#include <fstream></span>
|
|
<span class="preprocessor">#include <ctime></span>
|
|
<span class="preprocessor">#include <<a class="code" href="neural_09_09_8hpp.html">neural++.hpp</a>></span>
|
|
|
|
<span class="keyword">using namespace </span>std;
|
|
<span class="keyword">using namespace </span>neuralpp;
|
|
|
|
<span class="keywordtype">int</span> main() {
|
|
<span class="keywordtype">int</span> <span class="keywordtype">id</span> = 0;
|
|
<span class="keywordtype">string</span> xml;
|
|
time_t t1, t2;
|
|
|
|
<span class="comment">// Create the neural network. The network is going to have</span>
|
|
<span class="comment">// => 2 neurons for the input layer</span>
|
|
<span class="comment">// => 2 neurons for the hidden layer</span>
|
|
<span class="comment">// => 1 neuron for the output layer</span>
|
|
<span class="comment">// => a learning rate == 0.002 (just get it doing some tests until satisfied, but remember to keep its value quite low and ~ 0 to keep the network stable)</span>
|
|
<span class="comment">// => 1000 learning steps (i.e. the network will be ready after 1000 training steps to adjust the synaptical weights</span>
|
|
<span class="comment">// => 0.1 as neural threshold (the threshold above which a neuron activates)</span>
|
|
<a name="_a0"></a><a class="code" href="classneuralpp_1_1NeuralNet.html" title="Main project&#39;s class.">NeuralNet</a> net(2, 2, 1, 0.002, 2000);
|
|
|
|
<span class="comment">// Initialize a training XML as a string in 'xml'</span>
|
|
NeuralNet::initXML(xml);
|
|
|
|
<span class="comment">// Build some training sets for the XML. The format is:</span>
|
|
<span class="comment">// "input1,input2,...,inputn;output1,output2,...,outputn</span>
|
|
<span class="comment">// The 'id' variable is passed as reference, starting from 0,</span>
|
|
<span class="comment">// and it's used to enumerate the sets in the XML file.</span>
|
|
xml += NeuralNet::XMLFromSet(<span class="keywordtype">id</span>, <span class="stringliteral">"2,3;5"</span>);
|
|
xml += NeuralNet::XMLFromSet(<span class="keywordtype">id</span>, <span class="stringliteral">"3,2;5"</span>);
|
|
xml += NeuralNet::XMLFromSet(<span class="keywordtype">id</span>, <span class="stringliteral">"6,2;8"</span>);
|
|
xml += NeuralNet::XMLFromSet(<span class="keywordtype">id</span>, <span class="stringliteral">"2,2;4"</span>);
|
|
xml += NeuralNet::XMLFromSet(<span class="keywordtype">id</span>, <span class="stringliteral">"1,2;3"</span>);
|
|
xml += NeuralNet::XMLFromSet(<span class="keywordtype">id</span>, <span class="stringliteral">"-1,-2;-3"</span>);
|
|
xml += NeuralNet::XMLFromSet(<span class="keywordtype">id</span>, <span class="stringliteral">"8,9;17"</span>);
|
|
xml += NeuralNet::XMLFromSet(<span class="keywordtype">id</span>, <span class="stringliteral">"10,10;20"</span>);
|
|
xml += NeuralNet::XMLFromSet(<span class="keywordtype">id</span>, <span class="stringliteral">"4,1;5"</span>);
|
|
xml += NeuralNet::XMLFromSet(<span class="keywordtype">id</span>, <span class="stringliteral">"2,6;8"</span>);
|
|
xml += NeuralNet::XMLFromSet(<span class="keywordtype">id</span>, <span class="stringliteral">"2,7;9"</span>);
|
|
xml += NeuralNet::XMLFromSet(<span class="keywordtype">id</span>, <span class="stringliteral">"8,9;17"</span>);
|
|
xml += NeuralNet::XMLFromSet(<span class="keywordtype">id</span>, <span class="stringliteral">"4,7;11"</span>);
|
|
xml += NeuralNet::XMLFromSet(<span class="keywordtype">id</span>, <span class="stringliteral">"5,2;7"</span>);
|
|
NeuralNet::closeXML(xml);
|
|
|
|
<span class="comment">// Save the XML string just created to a file</span>
|
|
ofstream out(<span class="stringliteral">"adder.xml"</span>);
|
|
out << xml;
|
|
out.close();
|
|
cout << <span class="stringliteral">"Training file adder.xml has been written\n"</span>;
|
|
|
|
<span class="comment">// Start the training from the XML file</span>
|
|
t1 = time(NULL);
|
|
cout << <span class="stringliteral">"Training in progress - This may take a while...\n"</span>;
|
|
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>(<span class="stringliteral">"adder.xml"</span>, NeuralNet::file);
|
|
t2 = time(NULL);
|
|
|
|
<span class="comment">// Save the trained network to a binary file, that can be reloaded from any</span>
|
|
<span class="comment">// application that is going to use that network</span>
|
|
net.<a name="a2"></a><a class="code" href="classneuralpp_1_1NeuralNet.html#fdf94c276720c25e565cac834fe8a407" title="Save a trained neural network to a binary file.">save</a>(<span class="stringliteral">"network.xml"</span>);
|
|
cout << <span class="stringliteral">"Network trained in "</span> << (t2-t1) << <span class="stringliteral">" seconds. You can use adder.net file now to load this network\n"</span>;
|
|
<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
|
|
<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>
|