Modification of BackPropagation added, some fixes and refactoring
This commit is contained in:
94
tests/nn-rl-xor.cpp
Normal file
94
tests/nn-rl-xor.cpp
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user