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