quickProapagtion and tests

This commit is contained in:
2016-02-24 20:23:16 +01:00
parent c45f12f53c
commit 3c924d01f3
9 changed files with 359 additions and 44 deletions

View File

@@ -29,4 +29,13 @@ add_executable(recurrent recurrent.cpp)
target_link_libraries(recurrent NeuralNetwork)
add_executable(recurrent_perf recurrent_perf.cpp)
target_link_libraries(recurrent_perf NeuralNetwork)
target_link_libraries(recurrent_perf NeuralNetwork)
add_executable(quickpropagation quickpropagation.cpp)
target_link_libraries(quickpropagation NeuralNetwork)
add_executable(quickpropagation_perf quickpropagation_perf.cpp)
target_link_libraries(quickpropagation_perf NeuralNetwork)
add_executable(propagation_cmp propagation_cmp.cpp)
target_link_libraries(propagation_cmp NeuralNetwork)

77
tests/propagation_cmp.cpp Normal file
View File

@@ -0,0 +1,77 @@
#include <NeuralNetwork/FeedForward/Network.h>
#include <cassert>
#include <iostream>
#include "../include/NeuralNetwork/Learning/BackPropagation.h"
#include "../include/NeuralNetwork/Learning/QuickPropagation.h"
#include "../include/NeuralNetwork/Learning/CorrectionFunction/Optical.h"
#include "../include/NeuralNetwork/Learning/CorrectionFunction/ArcTangent.h"
#define LEARN(A,AR,B,BR,C,CR,D,DR,FUN,COEF,CLASS) \
({\
srand(rand);\
NeuralNetwork::FeedForward::Network n(2);\
NeuralNetwork::ActivationFunction::Sigmoid a(-1);\
n.appendLayer(2,a);\
n.appendLayer(1,a);\
n.randomizeWeights();\
CLASS prop(n,FUN);\
prop.setLearningCoefficient(COEF);\
int error=1; int steps = 0; \
while(error > 0 && steps <99999) {\
steps++;\
error=0;\
prop.teach(A,{AR});\
prop.teach(B,{BR});\
prop.teach(C,{CR});\
prop.teach(D,{DR});\
error+=fabs(n.computeOutput(A)[0]-AR) > 0.1 ? 1:0;\
error+=fabs(n.computeOutput(B)[0]-BR) > 0.1 ? 1:0;\
error+=fabs(n.computeOutput(C)[0]-CR) > 0.1 ? 1:0;\
error+=fabs(n.computeOutput(D)[0]-DR) > 0.1 ? 1:0;\
}\
steps;\
})
int main() {
long rand=(time(NULL));
const float linearCoef=0.7;
const float opticalCoef=0.11;
const float arcTangentCoef=0.6;
const float arcTangent=1.5;
{
std::cout << "XOR:\n";
std::cout << "\tBP: " <<
LEARN(std::vector<float>({1,0}),1,std::vector<float>({1,1}),0,std::vector<float>({0,0}),0,std::vector<float>({0,1}),1,
new NeuralNetwork::Learning::CorrectionFunction::Linear,linearCoef,NeuralNetwork::Learning::BackPropagation) << "\n";
std::cout << "\tQP: " <<
LEARN(std::vector<float>({1,0}),1,std::vector<float>({1,1}),0,std::vector<float>({0,0}),0,std::vector<float>({0,1}),1,
new NeuralNetwork::Learning::CorrectionFunction::Linear,linearCoef,NeuralNetwork::Learning::QuickPropagation) << "\n";
}
{
std::cout << "AND:\n";
std::cout << "\tBP: " <<
LEARN(std::vector<float>({1,0}),0,std::vector<float>({1,1}),1,std::vector<float>({0,0}),0,std::vector<float>({0,1}),0,
new NeuralNetwork::Learning::CorrectionFunction::Linear,linearCoef,NeuralNetwork::Learning::BackPropagation) << "\n";
std::cout << "\tQP: " <<
LEARN(std::vector<float>({1,0}),0,std::vector<float>({1,1}),1,std::vector<float>({0,0}),0,std::vector<float>({0,1}),0,
new NeuralNetwork::Learning::CorrectionFunction::Optical,opticalCoef,NeuralNetwork::Learning::QuickPropagation) << "\n";
}
{
std::cout << "AND:\n";
std::cout << "\tBP: " <<
LEARN(std::vector<float>({1,0}),1,std::vector<float>({1,1}),0,std::vector<float>({0,0}),1,std::vector<float>({0,1}),1,
new NeuralNetwork::Learning::CorrectionFunction::Linear,linearCoef,NeuralNetwork::Learning::BackPropagation) << "\n";
std::cout << "\tQP: " <<
LEARN(std::vector<float>({1,0}),1,std::vector<float>({1,1}),0,std::vector<float>({0,0}),1,std::vector<float>({0,1}),1,
new NeuralNetwork::Learning::CorrectionFunction::Optical,opticalCoef,NeuralNetwork::Learning::QuickPropagation) << "\n";
}
}

116
tests/quickpropagation.cpp Normal file
View File

@@ -0,0 +1,116 @@
#include <NeuralNetwork/FeedForward/Network.h>
#include <cassert>
#include <iostream>
#include "../include/NeuralNetwork/Learning/QuickPropagation.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::QuickPropagation prop(n);
for(int i=0;i<10000;i++) {
prop.teach({1,0},{1});
prop.teach({1,1},{0});
prop.teach({0,0},{0});
prop.teach({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::QuickPropagation prop(n);
for(int i=0;i<10000;i++) {
prop.teach({1,1},{1});
prop.teach({0,0},{0});
prop.teach({0,1},{0});
prop.teach({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::QuickPropagation prop(n);
for(int i=0;i<10000;i++) {
prop.teach({1,1},{0});
prop.teach({0,0},{1});
prop.teach({0,1},{1});
prop.teach({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);
}
}
}

View File

@@ -0,0 +1,26 @@
#include <NeuralNetwork/FeedForward/Network.h>
#include <cassert>
#include <iostream>
#include "../include/NeuralNetwork/Learning/QuickPropagation.h"
int main() {
{ // XOR problem
NeuralNetwork::FeedForward::Network n(2);
NeuralNetwork::ActivationFunction::Sigmoid a(-1);
n.appendLayer(200,a);
n.appendLayer(500,a);
n.appendLayer(900,a);
n.appendLayer(1,a);
n.randomizeWeights();
NeuralNetwork::Learning::QuickPropagation prop(n);
for(int i=0;i<100;i++) {
prop.teach({1,0},{1});
prop.teach({1,1},{0});
prop.teach({0,0},{0});
prop.teach({0,1},{1});
}
}
}