2015-10-24 16:03:07 +00:00
|
|
|
#include "netlearner.h"
|
|
|
|
#include "../../Net.h"
|
|
|
|
|
2015-10-26 08:19:48 +00:00
|
|
|
#include <QElapsedTimer>
|
|
|
|
|
2015-10-24 16:03:07 +00:00
|
|
|
void NetLearner::run()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2015-10-26 08:19:48 +00:00
|
|
|
QElapsedTimer timer;
|
|
|
|
|
2015-10-24 16:03:07 +00:00
|
|
|
Net myNet({2, 3, 1});
|
|
|
|
|
|
|
|
size_t batchSize = 5000;
|
|
|
|
size_t batchIndex = 0;
|
|
|
|
double batchMaxError = 0.0;
|
|
|
|
double batchMeanError = 0.0;
|
|
|
|
|
2015-10-26 08:19:48 +00:00
|
|
|
timer.start();
|
|
|
|
|
2015-10-26 07:41:59 +00:00
|
|
|
size_t numIterations = 1000000;
|
2015-10-24 16:03:07 +00:00
|
|
|
for (size_t iteration = 0; iteration < numIterations; ++iteration)
|
|
|
|
{
|
|
|
|
std::vector<double> inputValues =
|
|
|
|
{
|
|
|
|
std::rand() / (double)RAND_MAX,
|
|
|
|
std::rand() / (double)RAND_MAX
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<double> targetValues =
|
|
|
|
{
|
|
|
|
(inputValues[0] + inputValues[1]) / 2.0
|
|
|
|
};
|
|
|
|
|
|
|
|
myNet.feedForward(inputValues);
|
|
|
|
|
|
|
|
std::vector<double> outputValues = myNet.getOutput();
|
|
|
|
|
|
|
|
double error = outputValues[0] - targetValues[0];
|
|
|
|
|
|
|
|
batchMeanError += error;
|
|
|
|
batchMaxError = std::max<double>(batchMaxError, error);
|
|
|
|
|
|
|
|
if (batchIndex++ == batchSize)
|
|
|
|
{
|
|
|
|
QString logString;
|
|
|
|
|
|
|
|
logString.append("Batch error (");
|
|
|
|
logString.append(QString::number(batchSize));
|
|
|
|
logString.append(" iterations, max/mean): ");
|
|
|
|
logString.append(QString::number(std::abs(batchMaxError)));
|
|
|
|
logString.append(" / ");
|
|
|
|
logString.append(QString::number(std::abs(batchMeanError / batchSize)));
|
|
|
|
|
|
|
|
emit logMessage(logString);
|
2015-10-26 07:41:59 +00:00
|
|
|
emit currentNetError(batchMaxError);
|
2015-10-24 16:03:07 +00:00
|
|
|
|
|
|
|
batchIndex = 0;
|
|
|
|
batchMaxError = 0.0;
|
|
|
|
batchMeanError = 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
myNet.backProp(targetValues);
|
2015-10-25 08:51:09 +00:00
|
|
|
|
|
|
|
emit progress((double)iteration / (double)numIterations);
|
2015-10-24 16:03:07 +00:00
|
|
|
}
|
2015-10-25 16:40:22 +00:00
|
|
|
|
2015-10-26 08:19:48 +00:00
|
|
|
QString timerLogString;
|
|
|
|
timerLogString.append("Elapsed time: ");
|
|
|
|
timerLogString.append(QString::number(timer.elapsed() / 1000.0));
|
|
|
|
timerLogString.append(" seconds");
|
|
|
|
|
|
|
|
emit logMessage(timerLogString);
|
|
|
|
|
2015-10-25 16:40:22 +00:00
|
|
|
myNet.save("mynet.nnet");
|
2015-10-24 16:03:07 +00:00
|
|
|
}
|
|
|
|
catch (std::exception &ex)
|
|
|
|
{
|
|
|
|
QString logString("Error: ");
|
|
|
|
logString.append(ex.what());
|
|
|
|
emit logMessage(logString);
|
|
|
|
}
|
|
|
|
}
|