From adfa58800fdb848c495ea2f23898c9c36aa3785a Mon Sep 17 00:00:00 2001 From: blacklight Date: Sun, 16 Aug 2009 16:02:21 +0200 Subject: [PATCH] It finally supports multiple outputs for the network --- examples/adder.xml | 25 ++++++++++++++----------- examples/adderFromScratch.cpp | 15 ++++++--------- examples/doAdd.cpp | 2 +- examples/learnAdd.cpp | 2 +- include/neural++.hpp | 12 ++++++------ src/neuralnet.cpp | 5 ++--- src/synapsis.cpp | 12 ++++++------ 7 files changed, 36 insertions(+), 37 deletions(-) diff --git a/examples/adder.xml b/examples/adder.xml index 32f31c4..cca0c96 100644 --- a/examples/adder.xml +++ b/examples/adder.xml @@ -10,22 +10,25 @@ --> - - 2 + 3 - 5 + 2 + 5 + 1 - - 3 - 4 - 7 + + 5 + 3 + 8 + 2 - - 10 - 10 - 20 + + 6 + 3 + 9 + 3 diff --git a/examples/adderFromScratch.cpp b/examples/adderFromScratch.cpp index 9be0ef5..76eeb75 100644 --- a/examples/adderFromScratch.cpp +++ b/examples/adderFromScratch.cpp @@ -12,17 +12,18 @@ using namespace std; using namespace neuralpp; int main() { - NeuralNet net(3, 3, 1, 0.005, 1000); + NeuralNet net(2, 2, 2, 0.005, 1000); string xml; double tmp; // XML initialization. Then, I say XML that 2+3=5, 3+3=6, 5+4=9 // Strings' format is "input1,input2,...,inputn;output1,output2,...,outputm NeuralNet::initXML(xml); - xml += NeuralNet::XMLFromSet(0, "2,3,4;9"); - xml += NeuralNet::XMLFromSet(1, "3,3,1;7"); - xml += NeuralNet::XMLFromSet(2, "5,4,2;11"); + xml += NeuralNet::XMLFromSet(0, "3,2;5,1"); + xml += NeuralNet::XMLFromSet(1, "4,2;6,2"); + xml += NeuralNet::XMLFromSet(2, "6,3;9,3"); NeuralNet::closeXML(xml); + cout << xml << endl; net.train(xml, NeuralNet::str); vector v; @@ -36,13 +37,9 @@ int main() { cin >> tmp; v.push_back(tmp); - cout << "Third number to add: "; - cin >> tmp; - v.push_back(tmp); - net.setInput(v); net.propagate(); - cout << "Output: " << net.getOutput() << endl; + cout << "Output: " << net.getOutputs()[0] << "; " << net.getOutputs()[1] << endl; return 0; } diff --git a/examples/doAdd.cpp b/examples/doAdd.cpp index f94bdf2..de2c53c 100644 --- a/examples/doAdd.cpp +++ b/examples/doAdd.cpp @@ -38,7 +38,7 @@ int main() { net.setInput(v); net.propagate(); - cout << "Neural net output: " << net.getOutput() << endl; + cout << "Neural net output: " << net.getOutputs()[0] << "; " << net.getOutputs()[1] << endl; return 0; } diff --git a/examples/learnAdd.cpp b/examples/learnAdd.cpp index 1583dc5..e3e0dd1 100644 --- a/examples/learnAdd.cpp +++ b/examples/learnAdd.cpp @@ -13,7 +13,7 @@ using namespace std; using namespace neuralpp; int main() { - NeuralNet net(2, 2, 1, 0.005, 1000); + NeuralNet net(2, 2, 2, 0.005, 1000); cout << "Training in progress - This may take a while...\n"; net.train("adder.xml", NeuralNet::file); diff --git a/include/neural++.hpp b/include/neural++.hpp index dee0c3b..38d4a8d 100644 --- a/include/neural++.hpp +++ b/include/neural++.hpp @@ -289,12 +289,12 @@ namespace neuralpp { /** * @return Reference to input neuron of the synapsis */ - Neuron* getIn(); + Neuron* getIn() const; /** * @return Reference to output neuron of the synapsis */ - Neuron* getOut(); + Neuron* getOut() const; /** * @brief Set the weight of the synapsis @@ -313,19 +313,19 @@ namespace neuralpp { * @brief Return the weight of the synapsis * @return Weight of the synapsis */ - double getWeight(); + double getWeight() const; /** * @brief Return the delta of the synapsis * @return Delta of the synapsis */ - double getDelta(); + double getDelta() const; /** * @brief Get the delta of the synapsis at the previous iteration * @return The previous delta */ - double getPrevDelta(); + double getPrevDelta() const; /** * @brief Get the inertial momentum of a synapsis. This value is inversely proportional @@ -337,7 +337,7 @@ namespace neuralpp { * @param x The number of iterations already taken * @return The inertial momentum of the synapsis */ - double momentum (int N, int x); + double momentum (int N, int x) const; }; /** diff --git a/src/neuralnet.cpp b/src/neuralnet.cpp index e7eefc5..96c40b9 100644 --- a/src/neuralnet.cpp +++ b/src/neuralnet.cpp @@ -11,7 +11,6 @@ * this program. If not, see . * **************************************************************************************************/ -#include #include #include @@ -87,7 +86,7 @@ namespace neuralpp { output->propagate(); } - void NeuralNet::setInput(vector v) { + void NeuralNet::setInput(vector v) { input->setInput(v); } @@ -473,8 +472,8 @@ namespace neuralpp { xml.OutOfElem(); setInput(input); - propagate(); setExpected(output); + propagate(); update(); } } diff --git a/src/synapsis.cpp b/src/synapsis.cpp index 77b843e..f309b5a 100644 --- a/src/synapsis.cpp +++ b/src/synapsis.cpp @@ -47,23 +47,23 @@ namespace neuralpp { actv_f = a; } - Neuron *Synapsis::getIn() { + Neuron *Synapsis::getIn() const { return in; } - Neuron *Synapsis::getOut() { + Neuron *Synapsis::getOut() const { return out; } - double Synapsis::getWeight() { + double Synapsis::getWeight() const { return weight; } - double Synapsis::getDelta() { + double Synapsis::getDelta() const { return delta; } - double Synapsis::getPrevDelta() { + double Synapsis::getPrevDelta() const { return prev_delta; } @@ -82,7 +82,7 @@ namespace neuralpp { delta = d; } - double Synapsis::momentum(int N, int x) { + double Synapsis::momentum(int N, int x) const { return (BETA0 * N) / (20 * x + N); } }