refactored propagation

This commit is contained in:
2016-10-31 15:03:27 +01:00
parent 8749b3eb03
commit 77b38dec65
19 changed files with 285 additions and 548 deletions

View File

@@ -13,9 +13,6 @@ target_link_libraries(backpropagation NeuralNetwork gtest gtest_main)
add_executable(feedforward feedforward.cpp)
target_link_libraries(feedforward NeuralNetwork gtest gtest_main)
add_executable(optical_backpropagation optical_backpropagation.cpp)
target_link_libraries(optical_backpropagation NeuralNetwork gtest gtest_main)
add_executable(perceptron perceptron.cpp)
target_link_libraries(perceptron NeuralNetwork gtest gtest_main)

View File

@@ -48,20 +48,6 @@ TEST(Sigmoid, ParamMinusFive) {
ASSERT_LT(s(0.7), 0.970788);
}
TEST(SigmoidSSE, ParamMinusZeroPointSeven) {
NeuralNetwork::ActivationFunction::Sigmoid s(-0.7);
SSE comp;
comp.floats[0] = 0.1;
comp.floats[1] = 10;
comp.sse = s(comp.sse);
ASSERT_GT(comp.floats[0], 0.517483);
ASSERT_LT(comp.floats[0], 0.51750);
ASSERT_GT(comp.floats[1], 0.998989);
ASSERT_LT(comp.floats[1], 0.999189);
}
TEST(Linear, ParamOne) {
NeuralNetwork::ActivationFunction::Linear s(1.0);
ASSERT_GT(s(0.5), 0.4999);

View File

@@ -47,6 +47,7 @@ TEST(BackProp,XOR) {
}
TEST(BackProp,XORHyperbolicTangent) {
srand(time(NULL));
NeuralNetwork::FeedForward::Network n(2);
NeuralNetwork::ActivationFunction::HyperbolicTangent a(-1);
n.appendLayer(2,a);
@@ -56,7 +57,7 @@ TEST(BackProp,XORHyperbolicTangent) {
NeuralNetwork::Learning::BackPropagation prop(n);
for(int i=0;i<10000;i++) {
for(int i=0;i<1500;i++) {
prop.teach({1,0},{1});
prop.teach({1,1},{0});
prop.teach({0,0},{0});

View File

@@ -45,41 +45,41 @@ int main() {
std::cout << "\tLinear: " <<
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) << "\n";
std::make_shared<NeuralNetwork::Learning::CorrectionFunction::Linear>(),linearCoef) << "\n";
std::cout << "\tOptical: " <<
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::Optical,opticalCoef) << "\n";
std::make_shared<NeuralNetwork::Learning::CorrectionFunction::Optical>(),opticalCoef) << "\n";
std::cout << "\tArcTangent: " <<
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::ArcTangent(arcTangent),arcTangentCoef) << "\n";
std::make_shared<NeuralNetwork::Learning::CorrectionFunction::ArcTangent>(arcTangent),arcTangentCoef) << "\n";
}
{
std::cout << "AND:\n";
std::cout << "\tLinear: " <<
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) << "\n";
std::make_shared<NeuralNetwork::Learning::CorrectionFunction::Linear>(),linearCoef) << "\n";
std::cout << "\tOptical: " <<
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) << "\n";
std::make_shared<NeuralNetwork::Learning::CorrectionFunction::Optical>(),opticalCoef) << "\n";
std::cout << "\tArcTangent: " <<
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::ArcTangent(arcTangent),arcTangentCoef) << "\n";
std::make_shared<NeuralNetwork::Learning::CorrectionFunction::ArcTangent>(arcTangent),arcTangentCoef) << "\n";
}
{
std::cout << "AND:\n";
std::cout << "\tLinear: " <<
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) << "\n";
std::make_shared<NeuralNetwork::Learning::CorrectionFunction::Linear>(),linearCoef) << "\n";
std::cout << "\tOptical: " <<
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) << "\n";
std::make_shared<NeuralNetwork::Learning::CorrectionFunction::Optical>(),opticalCoef) << "\n";
std::cout << "\tArcTangent: " <<
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::ArcTangent(arcTangent),arcTangentCoef) << "\n";
std::make_shared<NeuralNetwork::Learning::CorrectionFunction::ArcTangent>(arcTangent),arcTangentCoef) << "\n";
}
}

View File

@@ -4,6 +4,7 @@
#include <iostream>
#include "../include/NeuralNetwork/Learning/BackPropagation.h"
#include <chrono>
int main() {
{ // XOR problem
NeuralNetwork::FeedForward::Network n(2);
@@ -16,11 +17,18 @@ int main() {
n.randomizeWeights();
NeuralNetwork::Learning::BackPropagation prop(n);
// prop.setBatchSize(20);
// prop.setMomentumWeight(0.8);
auto start = std::chrono::system_clock::now();
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});
}
auto end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_seconds = end -start;
std::cout << elapsed_seconds.count() << "\n";
}
}

View File

@@ -1,124 +0,0 @@
#include <NeuralNetwork/FeedForward/Network.h>
#include <NeuralNetwork/Learning/OpticalBackPropagation.h>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#include <gtest/gtest.h>
#pragma GCC diagnostic pop
TEST(OpticalBackPropagation,XOR) {
NeuralNetwork::FeedForward::Network n(2);
NeuralNetwork::ActivationFunction::Sigmoid a(-1);
n.appendLayer(2,a);
n.appendLayer(1,a);
n.randomizeWeights();
NeuralNetwork::Learning::OpticalBackPropagation 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_LT(ret[0], 0.1);
}
{
std::vector<float> ret =n.computeOutput({0,1});
ASSERT_GT(ret[0], 0.9);
}
{
std::vector<float> ret =n.computeOutput({1,0});
ASSERT_GT(ret[0], 0.9);
}
{
std::vector<float> ret =n.computeOutput({0,0});
ASSERT_LT(ret[0], 0.1);
}
}
TEST(OpticalBackPropagation,AND) {
NeuralNetwork::FeedForward::Network n(2);
NeuralNetwork::ActivationFunction::Sigmoid a(-1);
n.appendLayer(2,a);
n.appendLayer(1,a);
n.randomizeWeights();
NeuralNetwork::Learning::OpticalBackPropagation 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_GT(ret[0], 0.9);
}
{
std::vector<float> ret =n.computeOutput({0,1});
ASSERT_LT(ret[0], 0.1);
}
{
std::vector<float> ret =n.computeOutput({1,0});
ASSERT_LT(ret[0], 0.1);
}
{
std::vector<float> ret =n.computeOutput({0,0});
ASSERT_LT(ret[0], 0.1);
}
}
TEST(OpticalBackPropagation,NOTAND) {
NeuralNetwork::FeedForward::Network n(2);
NeuralNetwork::ActivationFunction::Sigmoid a(-1);
n.appendLayer(2,a);
n.appendLayer(1,a);
n.randomizeWeights();
NeuralNetwork::Learning::OpticalBackPropagation 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_LT(ret[0], 0.1);
}
{
std::vector<float> ret =n.computeOutput({0,1});
ASSERT_GT(ret[0], 0.9);
}
{
std::vector<float> ret =n.computeOutput({1,0});
ASSERT_GT(ret[0], 0.9);
}
{
std::vector<float> ret =n.computeOutput({0,0});
ASSERT_GT(ret[0], 0.9);
}
}

View File

@@ -4,6 +4,7 @@
#include <iostream>
#include "../include/NeuralNetwork/Learning/BackPropagation.h"
#include "../include/NeuralNetwork/Learning/QuickPropagation.h"
#include "../include/NeuralNetwork/Learning/RProp.h"
#include "../include/NeuralNetwork/Learning/CorrectionFunction/Optical.h"
#include "../include/NeuralNetwork/Learning/CorrectionFunction/ArcTangent.h"
@@ -16,7 +17,8 @@
n.appendLayer(1,a);\
n.randomizeWeights();\
CLASS prop(n,FUN);\
prop.setLearningCoefficient(COEF);\
prop.setLearningCoefficient(COEF);\
prop.setBatchSize(4); \
int error=1; int steps = 0; \
while(error > 0 && steps <99999) {\
steps++;\
@@ -42,36 +44,47 @@ int main() {
const float arcTangent=1.5;
{
std::cout << "XOR:\n";
std::cout << "XOR Linear:\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::make_shared<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";
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,
std::make_shared<NeuralNetwork::Learning::CorrectionFunction::Linear>(),linearCoef,NeuralNetwork::Learning::QuickPropagation) << "\n";
std::cout << "\tRProp: " <<
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,
std::make_shared<NeuralNetwork::Learning::CorrectionFunction::Linear>(),linearCoef,NeuralNetwork::Learning::RProp) << "\n";
}
{
std::cout << "AND:\n";
std::cout << "AND Optical:\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::make_shared<NeuralNetwork::Learning::CorrectionFunction::Optical>(),opticalCoef,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";
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,
std::make_shared<NeuralNetwork::Learning::CorrectionFunction::Optical>(),opticalCoef,NeuralNetwork::Learning::QuickPropagation) << "\n";
std::cout << "\tRProp: " <<
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,
std::make_shared<NeuralNetwork::Learning::CorrectionFunction::Optical>(),opticalCoef,NeuralNetwork::Learning::RProp) << "\n";
}
{
std::cout << "AND:\n";
std::cout << "XOR Optical:\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::make_shared<NeuralNetwork::Learning::CorrectionFunction::Optical>(),opticalCoef,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";
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,
std::make_shared<NeuralNetwork::Learning::CorrectionFunction::Optical>(),opticalCoef,NeuralNetwork::Learning::QuickPropagation) << "\n";
std::cout << "\tRProp: " <<
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,
std::make_shared<NeuralNetwork::Learning::CorrectionFunction::Optical>(),opticalCoef,NeuralNetwork::Learning::RProp) << "\n";
}
}