diff --git a/CMakeLists.txt b/CMakeLists.txt index 40b66a3..e2cd7d4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -79,4 +79,16 @@ set_property(TEST recurrent PROPERTY LABELS unit) add_test(feedforward_perf tests/feedforward_perf) set_property(TEST feedforward_perf PROPERTY LABELS perf) +add_test(optical_backpropagation tests/optical_backpropagation) +set_property(TEST optical_backpropagation PROPERTY LABELS unit) + + + + +add_test(backpropagation_perf tests/backpropagation_perf) +set_property(TEST backpropagation_perf PROPERTY LABELS perf) + +add_test(recurrent_perf tests/recurrent_perf) +set_property(TEST recurrent_perf PROPERTY LABELS perf) + ENDIF(ENABLE_TESTS) \ No newline at end of file diff --git a/README.md b/README.md index d4294bd..2028189 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,18 @@ Artifitial Neural Network Library ================================= how to build: + mkdir build + cmake .. -make \ No newline at end of file + +make + +Perfomace +--------- + +i5-5300U & 8GB ram + +| date | feedforward_perf | recurrent_perf | backpropagation_perf | +------------ | ---------------- | -------------- | -------------------- | +| 2016/02/07 | 8.27 sec | 7.15 sec | 6.00 sec | \ No newline at end of file diff --git a/include/NeuralNetwork/Learning/BackPropagation.h b/include/NeuralNetwork/Learning/BackPropagation.h index 089bb37..2e3a902 100644 --- a/include/NeuralNetwork/Learning/BackPropagation.h +++ b/include/NeuralNetwork/Learning/BackPropagation.h @@ -24,7 +24,7 @@ namespace Learning { inline virtual void setLearningCoefficient (const float& coefficient) { learningCoefficient=coefficient; } protected: - inline virtual float correction(const float & expected, const float &computed) { + inline virtual float correction(const float & expected, const float &computed) const { return expected-computed; }; float learningCoefficient; diff --git a/include/NeuralNetwork/Learning/OpticalBackPropagation.h b/include/NeuralNetwork/Learning/OpticalBackPropagation.h new file mode 100644 index 0000000..fd75862 --- /dev/null +++ b/include/NeuralNetwork/Learning/OpticalBackPropagation.h @@ -0,0 +1,28 @@ +#pragma once + +#include "./BackPropagation.h" + +namespace NeuralNetwork { +namespace Learning { + + /** @class OpticalBackPropagation + * @brief + */ + class OpticalBackPropagation : public BackPropagation { + + public: + OpticalBackPropagation(): BackPropagation() { + + } + virtual ~OpticalBackPropagation() { + } + + protected: + inline virtual float correction(const float & expected, const float &computed) const override { + register float tmp=(expected-computed); + register float ret=1+exp(tmp*tmp); + return tmp < 0? -ret:ret; + }; + }; +} +} \ No newline at end of file diff --git a/mainpage.dox b/mainpage.dox index 42f715f..08a0374 100644 --- a/mainpage.dox +++ b/mainpage.dox @@ -5,5 +5,4 @@ @author Tomas Cernik (Tom.Cernik@gmail.com) - */ diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 3e6c6d8..d5d1777 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -10,11 +10,20 @@ target_link_libraries(basis NeuralNetwork) add_executable(backpropagation backpropagation.cpp) target_link_libraries(backpropagation NeuralNetwork) +add_executable(backpropagation_perf backpropagation_perf.cpp) +target_link_libraries(backpropagation_perf NeuralNetwork) + add_executable(feedforward feedforward.cpp) target_link_libraries(feedforward NeuralNetwork) add_executable(feedforward_perf feedforward_perf.cpp) target_link_libraries(feedforward_perf NeuralNetwork) +add_executable(optical_backpropagation optical_backpropagation.cpp) +target_link_libraries(optical_backpropagation NeuralNetwork) + add_executable(recurrent recurrent.cpp) -target_link_libraries(recurrent NeuralNetwork) \ No newline at end of file +target_link_libraries(recurrent NeuralNetwork) + +add_executable(recurrent_perf recurrent_perf.cpp) +target_link_libraries(recurrent_perf NeuralNetwork) \ No newline at end of file diff --git a/tests/backpropagation_perf.cpp b/tests/backpropagation_perf.cpp new file mode 100644 index 0000000..1e7e4eb --- /dev/null +++ b/tests/backpropagation_perf.cpp @@ -0,0 +1,26 @@ +#include + +#include +#include +#include "../include/NeuralNetwork/Learning/BackPropagation.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::BackPropagation prop; + for(int i=0;i<100;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}); + } + } +} diff --git a/tests/feedforward_perf.cpp b/tests/feedforward_perf.cpp index 78e39ad..53512a0 100644 --- a/tests/feedforward_perf.cpp +++ b/tests/feedforward_perf.cpp @@ -7,8 +7,8 @@ int main() { { // XOR problem NeuralNetwork::FeedForward::Network n(2); NeuralNetwork::ActivationFunction::Sigmoid a(-1); - n.appendLayer(2000,a); - n.appendLayer(2000,a); + n.appendLayer(5000,a); + n.appendLayer(5000,a); n.appendLayer(1,a); for(int i=0;i<500;i++) { diff --git a/tests/optical_backpropagation.cpp b/tests/optical_backpropagation.cpp new file mode 100644 index 0000000..34e6da0 --- /dev/null +++ b/tests/optical_backpropagation.cpp @@ -0,0 +1,116 @@ +#include + +#include +#include +#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 ret =n.computeOutput({1,1}); + assert(ret[0] < 0.1); + } + + { + std::vector ret =n.computeOutput({0,1}); + assert(ret[0] > 0.9); + } + + { + std::vector ret =n.computeOutput({1,0}); + assert(ret[0] > 0.9); + } + + { + std::vector 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 ret =n.computeOutput({1,1}); + assert(ret[0] > 0.9); + } + + { + std::vector ret =n.computeOutput({0,1}); + assert(ret[0] < 0.1); + } + + { + std::vector ret =n.computeOutput({1,0}); + assert(ret[0] < 0.1); + } + + { + std::vector 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 ret =n.computeOutput({1,1}); + assert(ret[0] < 0.1); + } + + { + std::vector ret =n.computeOutput({0,1}); + assert(ret[0] > 0.9); + } + + { + std::vector ret =n.computeOutput({1,0}); + assert(ret[0] > 0.9); + } + + { + std::vector ret =n.computeOutput({0,0}); + assert(ret[0] > 0.9); + } + } +} diff --git a/tests/recurrent_perf.cpp b/tests/recurrent_perf.cpp new file mode 100644 index 0000000..168844c --- /dev/null +++ b/tests/recurrent_perf.cpp @@ -0,0 +1,12 @@ +#include + +#include +#include + +int main() { + NeuralNetwork::Recurrent::Network a(2,1,1000); + + for(size_t i=0;i<10000;i++) { + a.computeOutput({1,0.7}); + } +} \ No newline at end of file