Modification of BackPropagation added, some fixes and refactoring

This commit is contained in:
2014-11-11 22:06:16 +01:00
parent 42af5a4d2b
commit efbc8a4d1a
21 changed files with 588 additions and 94 deletions

View File

@@ -2,7 +2,11 @@ include ../Makefile.const
LIB_DIR = ../lib
GEN_TESTS=g-01 g-02
NN_TESTS= nn-reinforcement nn-01 nn-02 nn-03 nn-04
NN_TESTS= \
nn-bp-xor \
nn-obp-xor \
nn-rl-xor nn-rl-and \
nn-reinforcement nn-01 nn-02 nn-03 nn-04
ALL_TESTS=$(NN_TESTS) $(GEN_TESTS)
LIBS=$(LIB_DIR)/Genetics.a $(LIB_DIR)/NeuronNetwork.a

View File

@@ -31,25 +31,23 @@ int main()
s.push_back(Shin::NeuronNetwork::Solution(std::vector<double>({0})));
p.push_back(X(std::vector<bool>({1})));
Shin::NeuronNetwork::FeedForwardNetworkQuick q({1,1});
Shin::NeuronNetwork::FeedForwardNetworkQuick q({1,5000,5000,1});
Shin::NeuronNetwork::Learning::BackPropagation b(q);
int i=0;
std::cerr << i%4 <<". FOR: [" << p[i%2].representation()[0] << "] res: " << q.solve(p[i%2])[0] << " should be " << s[i%2][0]<<"\n";
for(int i=0;i<2000;i++)
for(int i=0;i<5;i++)
{
b.teach(p[i%2],s[i%2]);
std::cerr << i%2 <<". FOR: [" << p[i%2].representation()[0] << "] res: " << q.solve(p[i%2])[0] << " should be " << s[i%2][0]<<"\n";
//b.teach(p[i%2],s[i%2]);
// std::cerr << i%2 <<". FOR: [" << p[i%2].representation()[0] << "] res: " << q.solve(p[i%2])[0] << " should be " << s[i%2][0]<<"\n";
}
b.debugOn();
for(int i=0;i<2;i++)
{
b.teach(p[i%2],s[i%2]);
// b.teach(p[i%2],s[i%2]);
std::cerr << i%4 <<". FOR: [" << p[i%4].representation()[0] << "," <<p[i%4].representation()[0] << "] res: " << q.solve(p[i%4])[0] << " should be " <<
s[i%4][0]<<"\n";
}
b.debugOff();
/*
for(int i=0;i<40;i++)
{

74
tests/nn-04.cpp Normal file
View File

@@ -0,0 +1,74 @@
#include "../src/NeuronNetwork/Network"
#include <iostream>
class X: public Shin::NeuronNetwork::Problem
{
public: X(bool x,bool y):x(x),y(y) {}
protected: std::vector<bool> representation() const { return std::vector<bool>({x,y}); }
private:
bool x;
bool y;
};
int main()
{
srand(time(NULL));
int lm=5;
Shin::NeuronNetwork::FeedForwardNetwork net({2,lm,1});
bool x=1;
int prev_err=0;
int err=0;
int l;
int n;
int w;
int pot;
int wei;
int c=0;
std::cout << "\ntest 1 & 1 -" << net.solve(X(1,1))[0];
std::cout << "\ntest 1 & 0 -" << net.solve(X(1,0))[0];
std::cout << "\ntest 0 & 1 - " << net.solve(X(0,1))[0];
std::cout << "\ntest 0 & 0- " << net.solve(X(0,0))[0];
std::cout << "\n---------------------------------------";
do{
if(c%10000 ==1)
{
std::cout << "\nmixed";
srand(time(NULL));
}
err=0;
c++;
l=rand()%2+1;
n=rand()%lm;
w=rand()%2;
if(l==2)
n=0;
pot=net[l]->operator[](n)->getPotential();
net[l]->operator[](n)->setPotential(pot*(rand()%21+90)/100);
wei=net[l]->operator[](n)->getWeight(w);
net[l]->operator[](n)->setWeight(w,wei*(rand()%21+90)/100);
for(int i=0;i<100;i++)
{
bool x= rand()%2;
bool y=rand()%2;
Shin::NeuronNetwork::Solution s =net.solve(X(x,y));
if(s[0]!= (x xor y))
err++;
}
if(err > prev_err)
{
net[l]->operator[](n)->setPotential(pot);
net[l]->operator[](n)->setWeight(w,wei);
};
// std::cout << "C: " << c << " err: " << err << " prev: "<<prev_err << "\n";
prev_err=err;
if(err <1)
x=0;
}while(x);
std::cout << "\ntest 1 & 1 -" << net.solve(X(1,1))[0];
std::cout << "\ntest 1 & 0 -" << net.solve(X(1,0))[0];
std::cout << "\ntest 0 & 1 - " << net.solve(X(0,1))[0];
std::cout << "\ntest 0 & 0- " << net.solve(X(0,0))[0];
std::cout << "\nTotaly: " << c << "\n";
}

77
tests/nn-bp-xor.cpp Normal file
View File

@@ -0,0 +1,77 @@
#include "../src/NeuronNetwork/FeedForwardQuick"
#include "../src/NeuronNetwork/Learning/BackPropagation"
#include <iostream>
#include <vector>
class X: public Shin::NeuronNetwork::Problem
{
public:
X(const X& a) :q(a.q) {}
X(const std::vector<bool> &a):q(a) {}
std::vector<bool> representation() const
{
return q;
}
protected:
std::vector<bool> q;
};
int main()
{
for (int test=0;test<2;test++)
{
Shin::NeuronNetwork::FeedForwardNetworkQuick q({2,4,1});
Shin::NeuronNetwork::Learning::BackPropagation b(q);
srand(time(NULL));
std::vector<Shin::NeuronNetwork::Solution*> s;
std::vector<Shin::NeuronNetwork::Problem*> p;
s.push_back(new Shin::NeuronNetwork::Solution(std::vector<double>({0})));
p.push_back(new X(std::vector<bool>({0,0})));
s.push_back( new Shin::NeuronNetwork::Solution(std::vector<double>({1})));
p.push_back( new X(std::vector<bool>({1,0})));
s.push_back(new Shin::NeuronNetwork::Solution(std::vector<double>({0})));
p.push_back(new X(std::vector<bool>({1,1})));
s.push_back( new Shin::NeuronNetwork::Solution(std::vector<double>({1})));
p.push_back( new X(std::vector<bool>({0,1})));
if(test)
{
std::cerr << "Testing with entropy\n";
b.allowEntropy();
}else
{
std::cerr << "Testing without entropy\n";
}
b.setLearningCoeficient(0.1);//8);
for(int j=0;;j++)
{
double err=b.teachSet(p,s);
if(err <0.3)
{
// b.setLearningCoeficient(5);
}
if(err <0.1)
{
// b.setLearningCoeficient(0.2);
}
if(err <0.001)
{
std::cerr << j << "(" << err <<"):\n";
for(int i=0;i<4;i++)
{
std::cerr << "\t" << i%4 <<". FOR: [" << p[i%4]->representation()[0] << "," <<p[i%4]->representation()[1] << "] res: " <<
q.solve(*p[i%4])[0] << " should be " << s[i%4]->operator[](0)<<"\n";
}
}
if(err <0.001)
break;
}
}
}

78
tests/nn-obp-xor.cpp Normal file
View File

@@ -0,0 +1,78 @@
#include "../src/NeuronNetwork/FeedForwardQuick"
#include "../src/NeuronNetwork/Learning/OpticalBackPropagation"
#include <iostream>
#include <vector>
class X: public Shin::NeuronNetwork::Problem
{
public:
X(const X& a) :q(a.q) {}
X(const std::vector<bool> &a):q(a) {}
std::vector<bool> representation() const
{
return q;
}
protected:
std::vector<bool> q;
};
int main()
{
for (int test=0;test<2;test++)
{
Shin::NeuronNetwork::FeedForwardNetworkQuick q({2,4,1});
Shin::NeuronNetwork::Learning::OpticalBackPropagation b(q);
srand(time(NULL));
std::vector<Shin::NeuronNetwork::Solution*> s;
std::vector<Shin::NeuronNetwork::Problem*> p;
s.push_back(new Shin::NeuronNetwork::Solution(std::vector<double>({0})));
p.push_back(new X(std::vector<bool>({0,0})));
s.push_back( new Shin::NeuronNetwork::Solution(std::vector<double>({1})));
p.push_back( new X(std::vector<bool>({1,0})));
s.push_back(new Shin::NeuronNetwork::Solution(std::vector<double>({0})));
p.push_back(new X(std::vector<bool>({1,1})));
s.push_back( new Shin::NeuronNetwork::Solution(std::vector<double>({1})));
p.push_back( new X(std::vector<bool>({0,1})));
b.debugOn();
if(test)
{
std::cerr << "Testing with entropy\n";
b.allowEntropy();
}else
{
std::cerr << "Testing without entropy\n";
}
b.setLearningCoeficient(0.1);
for(int j=0;;j++)
{
double err=b.teachSet(p,s);
if(err <0.3)
{
// b.setLearningCoeficient(5);
}
if(err <0.1)
{
// b.setLearningCoeficient(0.2);
}
if(err <0.001)
{
std::cerr << j << "(" << err <<"):\n";
for(int i=0;i<4;i++)
{
std::cerr << "\t" << i%4 <<". FOR: [" << p[i%4]->representation()[0] << "," <<p[i%4]->representation()[1] << "] res: " <<
q.solve(*p[i%4])[0] << " should be " << s[i%4]->operator[](0)<<"\n";
}
}
if(err <0.001)
break;
}
}
}

85
tests/nn-rl-and.cpp Normal file
View File

@@ -0,0 +1,85 @@
#include "../src/NeuronNetwork/FeedForwardQuick"
#include "../src/NeuronNetwork/Learning/Reinforcement.h"
#include "../src/NeuronNetwork/Solution.h"
#include <iostream>
#include <vector>
class X: public Shin::NeuronNetwork::Problem
{
public:
X(const X& a) :q(a.q) {}
X(const std::vector<bool> &a):q(a) {}
std::vector<bool> representation() const
{
return q;
}
protected:
std::vector<bool> q;
};
int main()
{
srand(time(NULL));
std::vector<Shin::NeuronNetwork::Problem*> p;
p.push_back(new X(std::vector<bool>({0,0})));
p.push_back(new X(std::vector<bool>({1,1})));
Shin::NeuronNetwork::FeedForwardNetworkQuick q({1,1});
Shin::NeuronNetwork::Learning::Reinforcement b(q);
int i=0;
double targetQuality=1.4;
b.setQualityFunction(
[](const Shin::NeuronNetwork::Problem &pr,const Shin::NeuronNetwork::Solution &s)->double
{
if(pr.representation()[0]==0)
{
//ocekavame 1
int e=(s[0]-0.80)*15.0;//+(abs(s[1])-0.5)*100.0;
return e;
}else
{
//ocekavame 0
int e=(0.20-s[0])*15.0;//+(0.4-abs(s[1]))*100.0;
return e;
}
return 1.0;
});
for(i=0;i < 500000000;i++)
{
double err=b.learnSet(p);
if(i%100000==0)
srand(time(NULL));
if(err > targetQuality)
{
std::cerr << i << " ("<< err <<").\n";
for(int j=0;j<2;j++)
{
std::cerr << j%4 <<". FOR: [" << p[j%4]->representation()[0] << "," <<p[j%4]->representation()[0] << "] res: " << q.solve(*p[j%4])[0] << "\n";
}
}
if(err >targetQuality)
break;
}
/* int i=0;
std::cerr << i%4 <<". FOR: [" << p[i%2].representation()[0] << "] res: " << q.solve(p[i%2])[0] << " should be " << s[i%2][0]<<"\n";
for(int i=0;i<2000;i++)sa
{
b.teach(p[i%2],s[i%2]);
std::cerr << i%2 <<". FOR: [" << p[i%2].representation()[0] << "] res: " << q.solve(p[i%2])[0] << " should be " << s[i%2][0]<<"\n";
}
b.debugOn();
for(int i=0;i<2;i++)
{
b.teach(p[i%2],s[i%2]);
std::cerr << i%4 <<". FOR: [" << p[i%4].representation()[0] << "," <<p[i%4].representation()[0] << "] res: " << q.solve(p[i%4])[0] << " should be " <<
s[i%4][0]<<"\n";
}
b.debugOff();*/
}

94
tests/nn-rl-xor.cpp Normal file
View File

@@ -0,0 +1,94 @@
#include "../src/NeuronNetwork/FeedForwardQuick"
#include "../src/NeuronNetwork/Learning/Reinforcement"
#include <iostream>
#include <vector>
class X: public Shin::NeuronNetwork::Problem
{
public:
X(const X& a) :q(a.q) {}
X(const std::vector<bool> &a):q(a) {}
std::vector<bool> representation() const
{
return q;
}
protected:
std::vector<bool> q;
};
int main()
{
for (int test=0;test<2;test++)
{
Shin::NeuronNetwork::FeedForwardNetworkQuick q({2,6,1});
Shin::NeuronNetwork::Learning::Reinforcement b(q);
b.setQualityFunction(
[](const Shin::NeuronNetwork::Problem &pr,const Shin::NeuronNetwork::Solution &s)->double
{
std::vector <bool> p=pr;
double expect=0.0;
if(p[0] && p[1])
expect=0;
else if(p[0] && !p[1])
expect=1;
else if(!p[0] && !p[1])
expect=0;
else if(!p[0] && p[1])
expect=1;
// std::cerr << "expected: " << expect << " got " << s[0];
if(expect==0)
{
expect=0.35-s[0];
}else
{
expect=s[0]-0.65;
}
// std::cerr << " returnning " << expect*5.0 << "\n";
return expect*5.0;
});
srand(time(NULL));
std::vector<Shin::NeuronNetwork::Problem*> p;
p.push_back(new X(std::vector<bool>({0,0})));
p.push_back( new X(std::vector<bool>({1,0})));
p.push_back( new X(std::vector<bool>({0,1})));
p.push_back(new X(std::vector<bool>({1,1})));
if(test)
{
std::cerr << "Testing with entropy ...\n";
b.getPropagator().allowEntropy();
}else
{
std::cerr << "Testing without entropy ...\n";
}
double targetQuality =1.5;
for(int i=0;i < 500000000;i++)
{
double err=b.learnSet(p);
if(i%100000==0)
srand(time(NULL));
if(i%20000==0 || err > targetQuality)
{
std::cerr << i << " ("<< err <<").\n";
for(int j=0;j<4;j++)
{
std::cerr << "\t" << j%4 << ". FOR: [" << p[j%4]->representation()[0] << "," <<p[j%4]->representation()[1] << "] res: " <<
q.solve(*p[j%4])[0] << "\n";
}
}
if(err >targetQuality)
break;
}
}
}