mirror of
https://github.com/BlackLight/neuralpp.git
synced 2024-11-16 06:47:16 +01:00
Fixing more and more
This commit is contained in:
parent
25996a5e70
commit
73f13abb56
2 changed files with 57 additions and 53 deletions
|
@ -50,8 +50,7 @@ namespace neuralpp {
|
||||||
int epochs;
|
int epochs;
|
||||||
int ref_epochs;
|
int ref_epochs;
|
||||||
double l_rate;
|
double l_rate;
|
||||||
//double ex;
|
std::vector<double> expect;
|
||||||
std::vector<double> ex;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief It updates the weights of the net's synapsis through back-propagation.
|
* @brief It updates the weights of the net's synapsis through back-propagation.
|
||||||
|
@ -71,7 +70,7 @@ namespace neuralpp {
|
||||||
* @param ex Expected value
|
* @param ex Expected value
|
||||||
* @return Mean error
|
* @return Mean error
|
||||||
*/
|
*/
|
||||||
double error(double ex) const;
|
double error (double ex);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Private pointer to function, containing the function to
|
* @brief Private pointer to function, containing the function to
|
||||||
|
|
|
@ -11,8 +11,10 @@
|
||||||
* this program. If not, see <http://www.gnu.org/licenses/>. *
|
* this program. If not, see <http://www.gnu.org/licenses/>. *
|
||||||
**************************************************************************************************/
|
**************************************************************************************************/
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
#include "neural++.hpp"
|
#include "neural++.hpp"
|
||||||
|
@ -70,8 +72,14 @@ namespace neuralpp {
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
double NeuralNet::error(double expected) const {
|
double NeuralNet::error(double expected) {
|
||||||
return 0.5*(getOutput()-expected)*(getOutput()-expected);
|
double err = 0.0;
|
||||||
|
vector<double> out = getOutputs();
|
||||||
|
|
||||||
|
for (size_t i=0; i < output->size(); i++)
|
||||||
|
err += 0.5*(out[i] - expect[i]) * (out[i] - expect[i]);
|
||||||
|
|
||||||
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
void NeuralNet::propagate() {
|
void NeuralNet::propagate() {
|
||||||
|
@ -89,58 +97,71 @@ namespace neuralpp {
|
||||||
}
|
}
|
||||||
|
|
||||||
void NeuralNet::setExpected(double e) {
|
void NeuralNet::setExpected(double e) {
|
||||||
ex.clear();
|
expect.clear();
|
||||||
ex.push_back(e);
|
expect.push_back(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
void NeuralNet::setExpected(vector<double> e) {
|
||||||
|
expect.clear();
|
||||||
|
expect.assign(e.begin(), e.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
double NeuralNet::expected() const {
|
double NeuralNet::expected() const {
|
||||||
return ex[0];
|
return expect[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
vector<double> NeuralNet::getExpected() const {
|
||||||
|
return expect;
|
||||||
}
|
}
|
||||||
|
|
||||||
void NeuralNet::updateWeights() {
|
void NeuralNet::updateWeights() {
|
||||||
double out_delta;
|
double Dk = 0.0;
|
||||||
|
size_t k = output->size();
|
||||||
|
|
||||||
for (size_t i = 0; i < output->size(); i++) {
|
for (size_t i = 0; i < k; i++) {
|
||||||
Neuron *n = &(*output)[i];
|
Neuron *n = &(*output)[i];
|
||||||
|
double out_delta = 0.0,
|
||||||
|
z = n->getActv(),
|
||||||
|
d = expect[i],
|
||||||
|
f = df(actv_f, n->getProp());
|
||||||
|
|
||||||
for (size_t j = 0; j < n->nIn(); j++) {
|
for (size_t j = 0; j < n->nIn(); j++) {
|
||||||
Synapsis *s = &(n->synIn(j));
|
Synapsis *s = &(n->synIn(j));
|
||||||
|
double y = s->getIn()->getActv(),
|
||||||
|
beta = s->momentum(ref_epochs, ref_epochs - epochs);
|
||||||
|
|
||||||
if (ref_epochs - epochs > 0)
|
if (ref_epochs - epochs > 0)
|
||||||
out_delta =
|
out_delta =
|
||||||
(-l_rate) * (getOutput() - expected()) *
|
(-l_rate) * (z-d) * f * y +
|
||||||
df(actv_f, n->getProp()) * s->getIn()->getActv() +
|
beta * s->getPrevDelta();
|
||||||
s->momentum(ref_epochs, ref_epochs - epochs) *
|
|
||||||
s->getPrevDelta();
|
|
||||||
else
|
else
|
||||||
out_delta =
|
out_delta =
|
||||||
(-l_rate) * (getOutput() - expected()) *
|
(-l_rate) * (z-d) * f * y;
|
||||||
df(actv_f, n->getProp()) * s->getIn()->getActv();
|
|
||||||
|
|
||||||
|
Dk += ( (z-d) * f * s->getWeight() );
|
||||||
s->setDelta(out_delta);
|
s->setDelta(out_delta);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (size_t i = 0; i < hidden->size(); i++) {
|
for (size_t i = 0; i < hidden->size(); i++) {
|
||||||
Neuron *n = &(*hidden)[i];
|
Neuron *n = &(*hidden)[i];
|
||||||
double d =
|
double hidden_delta = 0.0,
|
||||||
df(actv_f, n->getProp()) *
|
d = df(actv_f, n->getProp()) * Dk;
|
||||||
n->synOut(0).getWeight() * out_delta;
|
|
||||||
|
|
||||||
for (size_t j = 0; j < n->nIn(); j++) {
|
for (size_t j = 0; j < n->nIn(); j++) {
|
||||||
Synapsis *s = &(n->synIn(j));
|
Synapsis *s = &(n->synIn(j));
|
||||||
|
double x = s->getIn()->getActv(),
|
||||||
|
beta = s->momentum(ref_epochs, ref_epochs - epochs);
|
||||||
|
|
||||||
if (ref_epochs - epochs > 0)
|
if (ref_epochs - epochs > 0)
|
||||||
s->setDelta((-l_rate) * d *
|
hidden_delta =
|
||||||
s->getIn()->getActv() +
|
(-l_rate) * d * x +
|
||||||
s->momentum(ref_epochs,
|
beta * s->getPrevDelta();
|
||||||
ref_epochs
|
|
||||||
-
|
|
||||||
epochs) *
|
|
||||||
s->getPrevDelta());
|
|
||||||
else
|
else
|
||||||
s->setDelta((-l_rate) * d *
|
hidden_delta =
|
||||||
s->getIn()->getActv());
|
(-l_rate) * d * x;
|
||||||
|
|
||||||
|
s->setDelta(hidden_delta);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -153,7 +174,7 @@ namespace neuralpp {
|
||||||
Synapsis *s = &(n->synIn(j));
|
Synapsis *s = &(n->synIn(j));
|
||||||
s->setWeight(s->getWeight() +
|
s->setWeight(s->getWeight() +
|
||||||
s->getDelta());
|
s->getDelta());
|
||||||
s->setDelta(0);
|
s->setDelta(0.0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -180,7 +201,7 @@ namespace neuralpp {
|
||||||
|
|
||||||
record.epochs = ref_epochs;
|
record.epochs = ref_epochs;
|
||||||
record.l_rate = l_rate;
|
record.l_rate = l_rate;
|
||||||
record.ex = ex[0];
|
record.ex = expect[0];
|
||||||
|
|
||||||
if (out.write((char*) &record, sizeof(struct netrecord)) <= 0)
|
if (out.write((char*) &record, sizeof(struct netrecord)) <= 0)
|
||||||
throw NetworkFileWriteException();
|
throw NetworkFileWriteException();
|
||||||
|
@ -416,7 +437,6 @@ namespace neuralpp {
|
||||||
|
|
||||||
void NeuralNet::train(string xmlsrc, NeuralNet::source src =
|
void NeuralNet::train(string xmlsrc, NeuralNet::source src =
|
||||||
file) throw(InvalidXMLException) {
|
file) throw(InvalidXMLException) {
|
||||||
double out;
|
|
||||||
CMarkup xml;
|
CMarkup xml;
|
||||||
|
|
||||||
if (src == file)
|
if (src == file)
|
||||||
|
@ -432,8 +452,7 @@ namespace neuralpp {
|
||||||
if (xml.FindElem("NETWORK")) {
|
if (xml.FindElem("NETWORK")) {
|
||||||
while (xml.FindChildElem("TRAINING")) {
|
while (xml.FindChildElem("TRAINING")) {
|
||||||
vector<double> input;
|
vector<double> input;
|
||||||
double output;
|
vector<double> output;
|
||||||
bool valid = false;
|
|
||||||
|
|
||||||
xml.IntoElem();
|
xml.IntoElem();
|
||||||
|
|
||||||
|
@ -445,34 +464,20 @@ namespace neuralpp {
|
||||||
xml.OutOfElem();
|
xml.OutOfElem();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (xml.FindChildElem("OUTPUT")) {
|
while (xml.FindChildElem("OUTPUT")) {
|
||||||
xml.IntoElem();
|
xml.IntoElem();
|
||||||
output =
|
output.push_back( atof(xml.GetData().c_str()) );
|
||||||
atof(xml.GetData().c_str());
|
|
||||||
xml.OutOfElem();
|
xml.OutOfElem();
|
||||||
}
|
}
|
||||||
|
|
||||||
xml.OutOfElem();
|
xml.OutOfElem();
|
||||||
|
|
||||||
while (!valid) {
|
setInput(input);
|
||||||
stringstream ss(stringstream::in | stringstream::out);
|
propagate();
|
||||||
|
setExpected(output);
|
||||||
setInput(input);
|
update();
|
||||||
propagate();
|
|
||||||
setExpected(output);
|
|
||||||
update();
|
|
||||||
|
|
||||||
out = getOutput();
|
|
||||||
|
|
||||||
ss << out;
|
|
||||||
|
|
||||||
if (ss.str().find("inf") == string::npos)
|
|
||||||
valid = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void NeuralNet::initXML(string& xml) {
|
void NeuralNet::initXML(string& xml) {
|
||||||
|
|
Loading…
Reference in a new issue