82 lines
2.0 KiB
C++
82 lines
2.0 KiB
C++
#include "../src/NeuronNetwork/FeedForward"
|
|
#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) :Problem(a) {}
|
|
X(const std::vector<float> &a):Problem() {data=a;}
|
|
};
|
|
|
|
int main()
|
|
{
|
|
srand(time(NULL));
|
|
|
|
std::vector<Shin::NeuronNetwork::Problem*> p;
|
|
|
|
p.push_back(new X(std::vector<float>({0,0})));
|
|
|
|
p.push_back(new X(std::vector<float>({1,1})));
|
|
|
|
p.push_back(new X(std::vector<float>({1,0})));
|
|
p.push_back(new X(std::vector<float>({0,1})));
|
|
|
|
Shin::NeuronNetwork::FeedForward q({2,1});
|
|
Shin::NeuronNetwork::Learning::Reinforcement b(q);
|
|
int i=0;
|
|
double targetQuality=0.5;
|
|
b.setQualityFunction(
|
|
[](const Shin::NeuronNetwork::Problem &pr,const Shin::NeuronNetwork::Solution &s)->float
|
|
{
|
|
if(pr[0]==1 && pr[1]==1)
|
|
{
|
|
//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||i%1000==0)
|
|
{
|
|
std::cerr << i << " ("<< err <<").\n";
|
|
for(int j=0;j<4;j++)
|
|
{
|
|
std::cerr << j%4 <<". FOR: [" << p[j%4]->operator[](0) << "," <<p[j%4]->operator[](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();*/
|
|
} |