Files
NeuralNetworkLib/tests/nn-obp-xor.cpp

60 lines
1.6 KiB
C++

#include "../src/NeuralNetwork/FeedForward"
#include "../src/NeuralNetwork/Learning/OpticalBackPropagation"
#include <iostream>
#include <vector>
class X: public Shin::Problem
{
public:
X(const X& a) :Problem(a) {}
X(const std::vector<float> &a):Problem() {data=a;}
};
int main()
{
srand(time(NULL));
for (int test=0;test<2;test++)
{
Shin::NeuralNetwork::FeedForward q({2,40,1});
Shin::NeuralNetwork::Learning::OpticalBackPropagation b(q);
b.setLearningCoeficient(0.1);
std::vector<std::pair<Shin::Problem, Shin::Solution> > set;
set.push_back(std::pair<Shin::Problem, Shin::Solution>(Shin::Problem({0,0}),Shin::Solution({0})));
set.push_back(std::pair<Shin::Problem, Shin::Solution>(Shin::Problem({1,0}),Shin::Solution({1})));
set.push_back(std::pair<Shin::Problem, Shin::Solution>(Shin::Problem({1,1}),Shin::Solution({0})));
set.push_back(std::pair<Shin::Problem, Shin::Solution>(Shin::Problem({0,1}),Shin::Solution({1})));
if(test)
{
std::cerr << "Testing with entropy\n";
b.allowNoise();
}else
{
std::cerr << "Testing without entropy\n";
}
for(int j=0;;j++)
{
double err=b.teachSet(set);
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: [" << set[i%4].first[0] << "," <<set[i%4].first[1] << "] res: " <<
q.solve(set[i%4].first)[0] << " should be " << set[i%4].second[0]<<"\n";
}
}
if(err <0.001)
break;
}
}
}