neuralpp/examples/learnAdd.cpp

75 lines
2.7 KiB
C++
Raw Normal View History

2009-02-18 00:19:29 +01:00
/**
* 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.
2009-02-18 00:19:29 +01:00
*
* by BlackLight, 2009
*/
#include <iostream>
2009-08-16 18:02:24 +02:00
#include <fstream>
#include <ctime>
#include <neural++.hpp>
2009-08-16 11:09:42 +02:00
using namespace std;
2009-02-18 00:19:29 +01:00
using namespace neuralpp;
int main() {
2009-08-16 18:02:24 +02:00
int id = 0;
string xml;
time_t t1, t2;
// Create the neural network. The network is going to have
// => 2 neurons for the input layer
// => 2 neurons for the hidden layer
// => 1 neuron for the output layer
2009-09-02 21:16:47 +02:00
// => 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)
// => 1000 learning steps (i.e. the network will be ready after 1000 training steps to adjust the synaptical weights
// => 0.1 as neural threshold (the threshold above which a neuron activates)
2009-09-02 21:16:47 +02:00
NeuralNet net(2, 2, 1, 0.002, 2000);
// Initialize a training XML as a string in 'xml'
2009-08-16 18:02:24 +02:00
NeuralNet::initXML(xml);
// Build some training sets for the XML. The format is:
// "input1,input2,...,inputn;output1,output2,...,outputn
// The 'id' variable is passed as reference, starting from 0,
// and it's used to enumerate the sets in the XML file.
2009-08-16 18:02:24 +02:00
xml += NeuralNet::XMLFromSet(id, "2,3;5");
xml += NeuralNet::XMLFromSet(id, "3,2;5");
xml += NeuralNet::XMLFromSet(id, "6,2;8");
xml += NeuralNet::XMLFromSet(id, "2,2;4");
xml += NeuralNet::XMLFromSet(id, "1,2;3");
xml += NeuralNet::XMLFromSet(id, "-1,-2;-3");
xml += NeuralNet::XMLFromSet(id, "8,9;17");
xml += NeuralNet::XMLFromSet(id, "10,10;20");
2009-09-02 21:16:47 +02:00
xml += NeuralNet::XMLFromSet(id, "4,1;5");
xml += NeuralNet::XMLFromSet(id, "2,6;8");
xml += NeuralNet::XMLFromSet(id, "2,7;9");
xml += NeuralNet::XMLFromSet(id, "8,9;17");
2009-09-04 11:27:14 +02:00
xml += NeuralNet::XMLFromSet(id, "4,7;11");
xml += NeuralNet::XMLFromSet(id, "5,2;7");
2009-08-16 18:02:24 +02:00
NeuralNet::closeXML(xml);
// Save the XML string just created to a file
2009-08-16 18:02:24 +02:00
ofstream out("adder.xml");
out << xml;
out.close();
cout << "Training file adder.xml has been written\n";
2009-02-18 00:19:29 +01:00
// Start the training from the XML file
2009-08-16 18:02:24 +02:00
t1 = time(NULL);
2009-08-15 02:59:09 +02:00
cout << "Training in progress - This may take a while...\n";
net.train("adder.xml", NeuralNet::file);
2009-08-16 18:02:24 +02:00
t2 = time(NULL);
2009-02-18 00:19:29 +01:00
// Save the trained network to a binary file, that can be reloaded from any
// application that is going to use that network
2009-09-02 21:16:47 +02:00
net.save("network.xml");
2009-08-16 18:02:24 +02:00
cout << "Network trained in " << (t2-t1) << " seconds. You can use adder.net file now to load this network\n";
2009-02-18 00:19:29 +01:00
return 0;
}