Rprop implementation
This commit is contained in:
@@ -28,6 +28,9 @@ target_link_libraries(recurrent NeuralNetwork gtest gtest_main)
|
||||
add_executable(quickpropagation quickpropagation.cpp)
|
||||
target_link_libraries(quickpropagation NeuralNetwork gtest gtest_main)
|
||||
|
||||
add_executable(rprop rprop.cpp)
|
||||
target_link_libraries(rprop NeuralNetwork gtest gtest_main)
|
||||
|
||||
# PERF
|
||||
|
||||
add_executable(backpropagation_function_cmp backpropagation_function_cmp.cpp)
|
||||
|
||||
165
tests/rprop.cpp
Normal file
165
tests/rprop.cpp
Normal file
@@ -0,0 +1,165 @@
|
||||
#include <NeuralNetwork/FeedForward/Network.h>
|
||||
#include <NeuralNetwork/Learning/RProp.h>
|
||||
#include <NeuralNetwork/ActivationFunction/HyperbolicTangent.h>
|
||||
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Weffc++"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#pragma GCC diagnostic pop
|
||||
TEST(RProp,XOR) {
|
||||
NeuralNetwork::FeedForward::Network n(2);
|
||||
NeuralNetwork::ActivationFunction::Sigmoid a(-1);
|
||||
n.appendLayer(3,a);
|
||||
n.appendLayer(1,a);
|
||||
|
||||
n.randomizeWeights();
|
||||
|
||||
NeuralNetwork::Learning::RProp prop(n);
|
||||
prop.setBatchSize(4);
|
||||
|
||||
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});
|
||||
}
|
||||
|
||||
{
|
||||
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(RProp,XORHyperbolicTangent) {
|
||||
srand(time(NULL));
|
||||
NeuralNetwork::FeedForward::Network n(2);
|
||||
NeuralNetwork::ActivationFunction::HyperbolicTangent a(-1);
|
||||
n.appendLayer(2,a);
|
||||
n.appendLayer(1,a);
|
||||
|
||||
n.randomizeWeights();
|
||||
|
||||
NeuralNetwork::Learning::RProp prop(n);
|
||||
prop.setBatchSize(4);
|
||||
|
||||
for(int i=0;i<15000;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(RProp,AND) {
|
||||
NeuralNetwork::FeedForward::Network n(2);
|
||||
NeuralNetwork::ActivationFunction::Sigmoid a(-1);
|
||||
n.appendLayer(1,a);
|
||||
|
||||
n.randomizeWeights();
|
||||
|
||||
NeuralNetwork::Learning::RProp prop(n);
|
||||
prop.setBatchSize(4);
|
||||
|
||||
for(int i=0;i<100000;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(RProp,NOTAND) {
|
||||
NeuralNetwork::FeedForward::Network n(2);
|
||||
NeuralNetwork::ActivationFunction::Sigmoid a(-1);
|
||||
n.appendLayer(2,a);
|
||||
n.appendLayer(1,a);
|
||||
|
||||
n.randomizeWeights();
|
||||
|
||||
NeuralNetwork::Learning::RProp prop(n);
|
||||
prop.setBatchSize(4);
|
||||
|
||||
for(int i=0;i<100000;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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user