Added new features

This commit is contained in:
2017-12-20 16:41:32 +01:00
parent 6c3eeac6fb
commit f1b60e4fcb
12 changed files with 323 additions and 140 deletions
+46 -8
View File
@@ -4,6 +4,8 @@
#include "NeuralNetwork.h"
using namespace chrono;
NeuralNetwork::NeuralNetwork()
{
V0 = Mat();
@@ -40,11 +42,18 @@ Mat NeuralNetwork::Train(Mat& ITset, Mat& OTset)
// outputError1: error on output for the current input and new calculated
// weighfactors, i.e. V1, W1
double outputError0, outputError1, sumSqrDiffError = MAX_OUTPUT_ERROR + 1;
double lowestError = 10;
Mat V0l, W0l;
Mat V1, W1;
cout << endl << "Starting neural training..." << endl;
cout << endl << "Starting neural training, this might take a while..." << endl;
int runs = 0;
milliseconds lasttime = timestamp();
milliseconds starttime = timestamp();
bool shown = false;
while ((sumSqrDiffError > MAX_OUTPUT_ERROR) && (runs < MAXRUNS)) {
sumSqrDiffError = 0;
@@ -67,16 +76,40 @@ Mat NeuralNetwork::Train(Mat& ITset, Mat& OTset)
sumSqrDiffError += (outputError1 - outputError0) * (outputError1 - outputError0);
if (sumSqrDiffError < lowestError)
{
lowestError = sumSqrDiffError;
V0l = V0;
W0l = W0;
}
V0 = V1;
W0 = W1;
}
runs++;
if (runs % 1000 == 0)
cout << "Completed " << runs << " runs, still working on it.. (" << sumSqrDiffError << ")" << endl;
if (timestamp() > lasttime + milliseconds(15000) && !shown)
{
cout << "We are still working on it, hold on..." << endl;
shown = true;
}
if (runs % 500 == 0)
{
shown = false;
auto runtime = (timestamp() - lasttime) / 1000;
cout << "Completed " << runs << " runs, still working on it.. (" << sumSqrDiffError << ")(" << runtime.count() << "s)" << endl;
lasttime = timestamp();
}
}
cout << "Training complete in " << runs << " runs" << endl;
V0 = V0l;
W0 = W0l;
auto totalduration = (timestamp() - starttime) / 1000;
cout << "Training finished in " << runs << " runs, it took " << totalduration.count() << "s" << endl;
cout << "The lowest error rate was " << lowestError << ", we are savind the matching factors" << endl;
Mat inputVectorTrainingSet, outputVectorTrainingSet, outputVectorBPN;
@@ -116,9 +149,6 @@ void NeuralNetwork::Read()
void NeuralNetwork::write()
{
for (pair<int, string> p : classes)
cout << p.first << " - " << p.second << endl;
FileStorage fs("factors.yml", FileStorage::WRITE);
fs << "W0" << W0 << "V0" << V0;
fs.release();
@@ -218,5 +248,13 @@ void NeuralNetwork::mat_class(Mat& ref, string& name)
}
}
name = classes[index] + " " + to_string(maxvalue*100) + "%";
name = classes[index] + " - " + to_string(maxvalue*100) + "%";
}
milliseconds NeuralNetwork::timestamp()
{
milliseconds ms = duration_cast<milliseconds>(
system_clock::now().time_since_epoch());
return ms;
}