117 lines
2.3 KiB
C++
117 lines
2.3 KiB
C++
#include <NeuralNetwork/FeedForward/Network.h>
|
|
|
|
#include <cassert>
|
|
#include <iostream>
|
|
#include "../include/NeuralNetwork/Learning/OpticalBackPropagation.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::OpticalBackPropagation 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);
|
|
}
|
|
}
|
|
{ // AND problem
|
|
NeuralNetwork::FeedForward::Network n(2);
|
|
NeuralNetwork::ActivationFunction::Sigmoid a(-1);
|
|
n.appendLayer(2,a);
|
|
n.appendLayer(1,a);
|
|
|
|
n.randomizeWeights();
|
|
|
|
NeuralNetwork::Learning::OpticalBackPropagation prop;
|
|
for(int i=0;i<10000;i++) {
|
|
prop.teach(n,{1,1},{1});
|
|
prop.teach(n,{0,0},{0});
|
|
prop.teach(n,{0,1},{0});
|
|
prop.teach(n,{1,0},{0});
|
|
}
|
|
|
|
{
|
|
std::vector<float> ret =n.computeOutput({1,1});
|
|
assert(ret[0] > 0.9);
|
|
}
|
|
|
|
{
|
|
std::vector<float> ret =n.computeOutput({0,1});
|
|
assert(ret[0] < 0.1);
|
|
}
|
|
|
|
{
|
|
std::vector<float> ret =n.computeOutput({1,0});
|
|
assert(ret[0] < 0.1);
|
|
}
|
|
|
|
{
|
|
std::vector<float> ret =n.computeOutput({0,0});
|
|
assert(ret[0] < 0.1);
|
|
}
|
|
}
|
|
{ // NOT AND problem
|
|
NeuralNetwork::FeedForward::Network n(2);
|
|
NeuralNetwork::ActivationFunction::Sigmoid a(-1);
|
|
n.appendLayer(2,a);
|
|
n.appendLayer(1,a);
|
|
|
|
n.randomizeWeights();
|
|
|
|
NeuralNetwork::Learning::OpticalBackPropagation prop;
|
|
for(int i=0;i<10000;i++) {
|
|
prop.teach(n,{1,1},{0});
|
|
prop.teach(n,{0,0},{1});
|
|
prop.teach(n,{0,1},{1});
|
|
prop.teach(n,{1,0},{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.9);
|
|
}
|
|
}
|
|
}
|