2015-03-23 18:28:29 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <exception>
|
2015-10-22 14:03:10 +00:00
|
|
|
#include <algorithm>
|
2015-03-23 18:28:29 +00:00
|
|
|
|
2015-10-23 20:16:12 +00:00
|
|
|
#include <assert.h>
|
|
|
|
|
2015-03-23 18:28:29 +00:00
|
|
|
#include "Net.h"
|
|
|
|
|
2015-10-23 20:16:12 +00:00
|
|
|
const double pi = std::acos(-1);
|
|
|
|
|
2015-03-23 18:28:29 +00:00
|
|
|
int main()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
std::cout << "Neuro running" << std::endl;
|
|
|
|
|
2015-10-22 14:03:10 +00:00
|
|
|
Net myNet({ 2, 3, 1 });
|
2015-03-23 18:28:29 +00:00
|
|
|
|
2015-10-23 20:16:12 +00:00
|
|
|
size_t batchSize = 5000;
|
|
|
|
size_t batchIndex = 0;
|
|
|
|
double batchMaxError = 0.0;
|
|
|
|
double batchMeanError = 0.0;
|
|
|
|
|
|
|
|
size_t numIterations = 1000000;
|
2015-10-22 20:09:35 +00:00
|
|
|
for (size_t iteration = 0; iteration < numIterations; ++iteration)
|
2015-10-17 20:05:27 +00:00
|
|
|
{
|
2015-10-22 20:09:35 +00:00
|
|
|
std::vector<double> inputValues =
|
|
|
|
{
|
|
|
|
std::rand() / (double)RAND_MAX,
|
|
|
|
std::rand() / (double)RAND_MAX
|
|
|
|
};
|
2015-10-18 20:05:18 +00:00
|
|
|
|
2015-10-22 20:09:35 +00:00
|
|
|
std::vector<double> targetValues =
|
|
|
|
{
|
2015-10-23 20:16:12 +00:00
|
|
|
(inputValues[0] + inputValues[1]) / 2.0
|
2015-10-22 20:09:35 +00:00
|
|
|
};
|
2015-10-18 20:05:18 +00:00
|
|
|
|
2015-10-22 20:09:35 +00:00
|
|
|
myNet.feedForward(inputValues);
|
2015-10-15 17:18:26 +00:00
|
|
|
|
2015-10-22 20:09:35 +00:00
|
|
|
std::vector<double> outputValues = myNet.getOutput();
|
2015-03-23 18:28:29 +00:00
|
|
|
|
2015-10-22 20:09:35 +00:00
|
|
|
double error = outputValues[0] - targetValues[0];
|
2015-10-18 20:05:18 +00:00
|
|
|
|
2015-10-23 20:16:12 +00:00
|
|
|
batchMeanError += error;
|
|
|
|
batchMaxError = std::max<double>(batchMaxError, error);
|
|
|
|
|
|
|
|
if (batchIndex++ == batchSize)
|
|
|
|
{
|
|
|
|
std::cout << "Batch error (" << batchSize << " iterations, max/mean): ";
|
|
|
|
std::cout << std::abs(batchMaxError);
|
|
|
|
std::cout << " / ";
|
|
|
|
std::cout << std::abs(batchMeanError / batchSize);
|
|
|
|
std::cout << std::endl;
|
|
|
|
|
|
|
|
batchIndex = 0;
|
|
|
|
batchMaxError = 0.0;
|
|
|
|
batchMeanError = 0.0;
|
|
|
|
}
|
2015-10-15 17:18:26 +00:00
|
|
|
|
2015-10-22 20:09:35 +00:00
|
|
|
myNet.backProp(targetValues);
|
|
|
|
}
|
2015-03-23 18:28:29 +00:00
|
|
|
}
|
|
|
|
catch (std::exception &ex)
|
|
|
|
{
|
|
|
|
std::cerr << "Error: " << ex.what() << std::endl;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|