backpropagation implementation

This commit is contained in:
2016-02-07 21:58:21 +01:00
parent 5bd520cfed
commit 5f43fb8cfb
3 changed files with 131 additions and 0 deletions

44
tests/backpropagation.cpp Normal file
View File

@@ -0,0 +1,44 @@
#include <NeuralNetwork/FeedForward/Network.h>
#include <cassert>
#include <iostream>
#include "../include/NeuralNetwork/Learning/BackPropagation.h"
int main() {
{ // XOR problem
NeuralNetwork::FeedForward::Network n(2);
NeuralNetwork::ActivationFunction::Sigmoid a(-1);
n.appendLayer(2,a);
n.appendLayer(1,a);
n.randomizeWeights();
NeuralNetwork::Learning::BackPropagation prop;
for(int i=0;i<10000;i++) {
prop.teach(n,{1,0},{1});
prop.teach(n,{1,1},{0});
prop.teach(n,{0,0},{0});
prop.teach(n,{0,1},{1});
}
{
std::vector<float> ret =n.computeOutput({1,1});
assert(ret[0] < 0.1);
}
{
std::vector<float> ret =n.computeOutput({0,1});
assert(ret[0] > 0.9);
}
{
std::vector<float> ret =n.computeOutput({1,0});
assert(ret[0] > 0.9);
}
{
std::vector<float> ret =n.computeOutput({0,0});
assert(ret[0] < 0.1);
}
}
}