BackPropagation works!
This commit is contained in:
@@ -1,47 +1,67 @@
|
||||
#include "../src/NeuronNetwork/FeedForward"
|
||||
#include "../src/NeuronNetwork/FeedForwardQuick"
|
||||
#include "../src/NeuronNetwork/Learning/BackPropagation"
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
class X: public Shin::NeuronNetwork::Problem
|
||||
{
|
||||
protected:
|
||||
public:
|
||||
X(const X& a) :q(a.q) {}
|
||||
X(const std::vector<bool> &a):q(a) {}
|
||||
std::vector<bool> representation() const
|
||||
{
|
||||
return std::vector<bool>({1,1});
|
||||
return q;
|
||||
}
|
||||
protected:
|
||||
std::vector<bool> q;
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
Shin::NeuronNetwork::FeedForwardNetwork n({2,3,2});
|
||||
Shin::NeuronNetwork::Solution s =n.solve(X());
|
||||
Shin::NeuronNetwork::FeedForwardNetworkQuick q({2,3,2});
|
||||
Shin::NeuronNetwork::Solution sq =q.solve(X());
|
||||
if(s.size()!=2)
|
||||
srand(time(NULL));
|
||||
std::vector<Shin::NeuronNetwork::Solution> s;
|
||||
std::vector<X> p;
|
||||
|
||||
//
|
||||
s.push_back(Shin::NeuronNetwork::Solution(std::vector<double>({1})));
|
||||
p.push_back(X(std::vector<bool>({0})));
|
||||
|
||||
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::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++)
|
||||
{
|
||||
std::cout << "1";
|
||||
return 1;
|
||||
}
|
||||
if(s[0]!=1)
|
||||
{
|
||||
std::cout << "2";
|
||||
return 1;
|
||||
}
|
||||
if(s[1]!=1)
|
||||
{
|
||||
std::cout << "3";
|
||||
return 1;
|
||||
}
|
||||
if(s.size()!=sq.size())
|
||||
{
|
||||
std::cout << "3";
|
||||
return 1;
|
||||
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++)
|
||||
if(s[i]!=sq[i])
|
||||
{
|
||||
std::cout << "4 " << i;
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
{
|
||||
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++)
|
||||
{
|
||||
b.teach(p[i%4],s[i%4]);
|
||||
}
|
||||
b.debugOn();
|
||||
std::cerr << "LEARNED\n";
|
||||
for(int i=0;i<4;i++)
|
||||
{
|
||||
b.teach(p[i%4],s[i%4]);
|
||||
std::cerr << 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][0]<<"\n";
|
||||
}
|
||||
*/
|
||||
}
|
||||
@@ -43,17 +43,6 @@ int main()
|
||||
return 1;
|
||||
}
|
||||
|
||||
if(s[0]!=0)
|
||||
{
|
||||
std::cout << "2";
|
||||
return 1;
|
||||
}
|
||||
if(s[1]!=1)
|
||||
{
|
||||
std::cout << "3";
|
||||
return 1;
|
||||
}
|
||||
|
||||
for(int i=0;i<2;i++)
|
||||
{
|
||||
if(s[i]!=sq[i])
|
||||
|
||||
113
tests/nn-03.cpp
113
tests/nn-03.cpp
@@ -1,74 +1,61 @@
|
||||
#include "../src/NeuronNetwork/Network"
|
||||
#include "../src/NeuronNetwork/FeedForward"
|
||||
#include "../src/NeuronNetwork/FeedForwardQuick"
|
||||
#include "../src/NeuronNetwork/Learning/BackPropagation"
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
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;
|
||||
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));
|
||||
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);
|
||||
std::vector<Shin::NeuronNetwork::Solution> s;
|
||||
std::vector<X> p;
|
||||
|
||||
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++;
|
||||
}
|
||||
//
|
||||
s.push_back(Shin::NeuronNetwork::Solution(std::vector<double>({0})));
|
||||
p.push_back(X(std::vector<bool>({1,0})));
|
||||
s.push_back(Shin::NeuronNetwork::Solution(std::vector<double>({0})));
|
||||
p.push_back(X(std::vector<bool>({0,1})));
|
||||
s.push_back(Shin::NeuronNetwork::Solution(std::vector<double>({0})));
|
||||
p.push_back(X(std::vector<bool>({0,0})));
|
||||
s.push_back(Shin::NeuronNetwork::Solution(std::vector<double>({1})));
|
||||
p.push_back(X(std::vector<bool>({1,1})));
|
||||
|
||||
Shin::NeuronNetwork::FeedForwardNetworkQuick q({2,4,1});
|
||||
Shin::NeuronNetwork::Learning::BackPropagation b(q);
|
||||
|
||||
b.debugOn();
|
||||
for(int i=0;i<4;i++)
|
||||
{
|
||||
b.teach(p[i%4],s[i%4]);
|
||||
std::cerr << 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][0]<<"\n";
|
||||
}
|
||||
b.debugOff();
|
||||
|
||||
for(int i=0;i<40;i++)
|
||||
{
|
||||
b.teach(p[i%4],s[i%4]);
|
||||
}
|
||||
b.debugOn();
|
||||
std::cerr << "LEARNED\n";
|
||||
for(int i=0;i<4;i++)
|
||||
{
|
||||
b.teach(p[i%4],s[i%4]);
|
||||
std::cerr << 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][0]<<"\n";
|
||||
}
|
||||
|
||||
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";
|
||||
}
|
||||
Reference in New Issue
Block a user