test rewritten

This commit is contained in:
2016-04-18 16:03:33 +02:00
parent e6a5882e58
commit 9f1f0fe763
11 changed files with 289 additions and 287 deletions

View File

@@ -16,7 +16,23 @@ 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)
add_executable(perceptron_learning perceptron_learning.cpp)
target_link_libraries(perceptron_learning NeuralNetwork gtest gtest_main)
add_executable(recurrent recurrent.cpp)
target_link_libraries(recurrent NeuralNetwork gtest gtest_main)
add_executable(quickpropagation quickpropagation.cpp)
target_link_libraries(quickpropagation NeuralNetwork gtest gtest_main)
# PERF
add_executable(backpropagation_function_cmp backpropagation_function_cmp.cpp)
target_link_libraries(backpropagation_function_cmp NeuralNetwork)
@@ -27,26 +43,11 @@ target_link_libraries(backpropagation_perf 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(perceptron perceptron.cpp)
target_link_libraries(perceptron NeuralNetwork)
add_executable(perceptron_learning perceptron_learning.cpp)
target_link_libraries(perceptron_learning NeuralNetwork)
add_executable(recurrent recurrent.cpp)
target_link_libraries(recurrent NeuralNetwork)
add_executable(recurrent_perf recurrent_perf.cpp)
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)]]
target_link_libraries(propagation_cmp NeuralNetwork)

View File

@@ -3,7 +3,7 @@
#include <NeuralNetwork/ActivationFunction/HyperbolicTangent.h>
#include <NeuralNetwork/ActivationFunction/Linear.h>
#include "gtest/gtest.h"
#include <gtest/gtest.h>
union SSE {
__m128 sse; // SSE 4 x float vector

View File

@@ -1,7 +1,7 @@
#include <NeuralNetwork/FeedForward/Network.h>
#include <NeuralNetwork/Learning/BackPropagation.h>
#include "gtest/gtest.h"
#include <gtest/gtest.h>
TEST(BackProp,XOR) {
NeuralNetwork::FeedForward::Network n(2);

View File

@@ -2,9 +2,9 @@
#include <cassert>
#include <iostream>
#include "../include/NeuralNetwork/Learning/BackPropagation.h"
#include "../include/NeuralNetwork/Learning/CorrectionFunction/Optical.h"
#include "../include/NeuralNetwork/Learning/CorrectionFunction/ArcTangent.h"
#include <NeuralNetwork/Learning/BackPropagation.h>
#include <NeuralNetwork/Learning/CorrectionFunction/Optical.h>
#include <NeuralNetwork/Learning/CorrectionFunction/ArcTangent.h>
#define LEARN(A,AR,B,BR,C,CR,D,DR,FUN,COEF) \
({\

View File

@@ -1,6 +1,6 @@
#include <NeuralNetwork/FeedForward/Network.h>
#include "gtest/gtest.h"
#include <gtest/gtest.h>
TEST(FeedForward, XOR) {
NeuralNetwork::FeedForward::Network n(2);

View File

@@ -1,116 +1,120 @@
#include <NeuralNetwork/FeedForward/Network.h>
#include <cassert>
#include <iostream>
#include "../include/NeuralNetwork/Learning/OpticalBackPropagation.h"
#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);
#include <gtest/gtest.h>
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});
}
TEST(OpticalBackPropagation,XOR) {
NeuralNetwork::FeedForward::Network n(2);
NeuralNetwork::ActivationFunction::Sigmoid a(-1);
n.appendLayer(2,a);
n.appendLayer(1,a);
{
std::vector<float> ret =n.computeOutput({1,1});
assert(ret[0] < 0.1);
}
n.randomizeWeights();
{
std::vector<float> ret =n.computeOutput({0,1});
assert(ret[0] > 0.9);
}
NeuralNetwork::Learning::OpticalBackPropagation prop(n);
{
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);
}
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});
}
{ // 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(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);
}
{
std::vector<float> ret =n.computeOutput({1,1});
ASSERT_LT(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();
{
std::vector<float> ret =n.computeOutput({0,1});
ASSERT_GT(ret[0], 0.9);
}
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,0});
ASSERT_GT(ret[0], 0.9);
}
{
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);
}
{
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

@@ -1,16 +1,17 @@
#include <NeuralNetwork/FeedForward/Perceptron.h>
#include <assert.h>
#include <iostream>
#include <gtest/gtest.h>
int main() {
TEST(Perceptron,Test) {
NeuralNetwork::FeedForward::Perceptron p(2,1);
p[1].weight(0)=-1.0;
p[1].weight(1)=1.001;
assert(p.computeOutput({1,1})[0] == 1.0);
p[1].weight(1)=0.999;
float ret =p.computeOutput({1,1})[0];
ASSERT_EQ(ret, 1.0);
assert(p.computeOutput({1,1})[0] == 0.0);
p[1].weight(1)=0.999;
ret =p.computeOutput({1,1})[0];
ASSERT_EQ(ret, 0.0);
}

View File

@@ -1,41 +1,39 @@
#include <NeuralNetwork/Learning/PerceptronLearning.h>
#include <cassert>
#include <iostream>
#include <gtest/gtest.h>
int main() {
{ // XOR problem
NeuralNetwork::FeedForward::Perceptron n(2,1);
n.randomizeWeights();
TEST(PerceptronLearning,XOR) {
NeuralNetwork::FeedForward::Perceptron n(2,1);
NeuralNetwork::Learning::PerceptronLearning learn(n);
n.randomizeWeights();
for(int i=0;i<10;i++) {
learn.teach({1,0},{1});
learn.teach({1,1},{1});
learn.teach({0,0},{0});
learn.teach({0,1},{1});
}
NeuralNetwork::Learning::PerceptronLearning learn(n);
{
std::vector<float> ret =n.computeOutput({1,1});
assert(ret[0] > 0.9);
}
for(int i=0;i<10;i++) {
learn.teach({1,0},{1});
learn.teach({1,1},{1});
learn.teach({0,0},{0});
learn.teach({0,1},{1});
}
{
std::vector<float> ret =n.computeOutput({0,1});
assert(ret[0] > 0.9);
}
{
std::vector<float> ret =n.computeOutput({1,1});
ASSERT_GT(ret[0], 0.9);
}
{
std::vector<float> ret =n.computeOutput({1,0});
assert(ret[0] > 0.9);
}
{
std::vector<float> ret =n.computeOutput({0,1});
ASSERT_GT(ret[0], 0.9);
}
{
std::vector<float> ret =n.computeOutput({0,0});
assert(ret[0] < 0.1);
}
{
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);
}
}

View File

@@ -1,116 +1,118 @@
#include <NeuralNetwork/FeedForward/Network.h>
#include <NeuralNetwork/Learning/QuickPropagation.h>
#include <cassert>
#include <iostream>
#include "../include/NeuralNetwork/Learning/QuickPropagation.h"
#include <gtest/gtest.h>
int main() {
{ // XOR problem
NeuralNetwork::FeedForward::Network n(2);
NeuralNetwork::ActivationFunction::Sigmoid a(-1);
n.appendLayer(2,a);
n.appendLayer(1,a);
TEST(QuickPropagation,XOR) {
NeuralNetwork::FeedForward::Network n(2);
NeuralNetwork::ActivationFunction::Sigmoid a(-1);
n.appendLayer(2,a);
n.appendLayer(1,a);
n.randomizeWeights();
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});
}
NeuralNetwork::Learning::QuickPropagation prop(n);
{
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);
}
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});
}
{ // 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);
}
{
std::vector<float> ret =n.computeOutput({1,1});
ASSERT_LT(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();
{
std::vector<float> ret =n.computeOutput({0,1});
ASSERT_GT(ret[0], 0.9);
}
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,0});
ASSERT_GT(ret[0], 0.9);
}
{
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);
}
{
std::vector<float> ret =n.computeOutput({0,0});
ASSERT_LT(ret[0], 0.1);
}
}
TEST(QuickPropagation,AND) {
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_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(QuickPropagation,NOTAND) {
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_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

@@ -1,9 +1,8 @@
#include <NeuralNetwork/Recurrent/Network.h>
#include <assert.h>
#include <iostream>
#include <gtest/gtest.h>
int main() {
TEST(Recurrent, Sample) {
NeuralNetwork::Recurrent::Network a(2,1,1);
a.getNeurons()[4]->weight(1)=0.05;
@@ -15,6 +14,6 @@ int main() {
for(size_t i=0;i<solutions.size();i++) {
float res= a.computeOutput({1,0.7})[0];
assert(res > solutions[i]*0.999 && res < solutions[i]*1.001);
ASSERT_FLOAT_EQ(res, solutions[i]);
}
}