backpropagation implementation
This commit is contained in:
44
tests/backpropagation.cpp
Normal file
44
tests/backpropagation.cpp
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user