brought gprop into 21st century
This commit is contained in:
parent
7f62a3f6f2
commit
ab0fd90f46
5 changed files with 77 additions and 77 deletions
|
|
@ -1,7 +1,6 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// gprop
|
||||
//-----------------------------------------------------------------------------
|
||||
using namespace std;
|
||||
|
||||
#include <stdlib.h> // EXIT_SUCCESS EXIT_FAILURE
|
||||
#include <stdexcept> // exception
|
||||
|
|
@ -11,6 +10,8 @@ using namespace std;
|
|||
#include <eo> // all usefull eo stuff
|
||||
#include "gprop.h" // Chrom eoChromInit eoChromMutation eoChromXover eoChromEvaluator
|
||||
|
||||
using namespace std;
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// global variables
|
||||
//-----------------------------------------------------------------------------
|
||||
|
|
|
|||
|
|
@ -72,7 +72,7 @@ struct phenotype
|
|||
|
||||
|
||||
|
||||
friend ostream& operator<<(ostream& os, const phenotype& p)
|
||||
friend std::ostream& operator<<(std::ostream& os, const phenotype& p)
|
||||
{
|
||||
return os << p.trn_ok << "/" << p.trn_max << " "
|
||||
<< p.val_ok << "/" << p.val_max << " "
|
||||
|
|
@ -80,7 +80,7 @@ struct phenotype
|
|||
<< p.mse_error;
|
||||
}
|
||||
|
||||
friend istream& operator>>(istream& is, phenotype& p)
|
||||
friend std::istream& operator>>(std::istream& is, phenotype& p)
|
||||
{
|
||||
return is; // complete me
|
||||
}
|
||||
|
|
@ -107,18 +107,18 @@ extern unsigned in, out, hidden;
|
|||
class Chrom: public EO<phenotype>, public genotype
|
||||
{
|
||||
public:
|
||||
Chrom(): genotype(in, out, vector<unsigned>(hidden < 1 ? 0 : 1, hidden)) {}
|
||||
Chrom(): genotype(in, out, std::vector<unsigned>(hidden < 1 ? 0 : 1, hidden)) {}
|
||||
|
||||
string className() const { return "Chrom"; }
|
||||
std::string className() const { return "Chrom"; }
|
||||
|
||||
void printOn (ostream& os) const
|
||||
void printOn (std::ostream& os) const
|
||||
{
|
||||
os << setprecision(3) << static_cast<genotype>(*this) << " \t"
|
||||
os << std::setprecision(3) << static_cast<genotype>(*this) << " \t"
|
||||
<< fitness();
|
||||
// os << fitness();
|
||||
}
|
||||
|
||||
void readFrom (istream& is)
|
||||
void readFrom (std::istream& is)
|
||||
{
|
||||
invalidate(); // complete me
|
||||
}
|
||||
|
|
@ -152,8 +152,8 @@ void gprop_use_datasets(mlp::set *trn, mlp::set *val, mlp::set *tst) {
|
|||
|
||||
void ensure_datasets_initialized() {
|
||||
if (!trn_set) {
|
||||
cerr << "trn_set is not initialized. Must call gprop_use_datasets before training\n";
|
||||
cerr.flush();
|
||||
std::cerr << "trn_set is not initialized. Must call gprop_use_datasets before training\n";
|
||||
std::cerr.flush();
|
||||
abort();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,7 +19,6 @@
|
|||
|
||||
|
||||
|
||||
|
||||
namespace mlp
|
||||
{
|
||||
using namespace std;
|
||||
|
|
@ -111,7 +110,7 @@ namespace mlp
|
|||
|
||||
void perturb_num(double &num, double magnitude) {
|
||||
double scale = max(num, 0.05) * magnitude;
|
||||
double perturbation = scale * (drand48() - 0.5);
|
||||
double perturbation = scale * (rng.uniform() - 0.5);
|
||||
num += perturbation;
|
||||
}
|
||||
|
||||
|
|
@ -119,9 +118,9 @@ namespace mlp
|
|||
{
|
||||
|
||||
for (vector::iterator w = weight.begin(); w != weight.end(); ++w)
|
||||
if ( probability >= 1.0 || drand48() < probability)
|
||||
if ( probability >= 1.0 || rng.uniform() < probability)
|
||||
perturb_num(*w, magnitude);
|
||||
if ( probability >= 1.0 || drand48() < probability)
|
||||
if ( probability >= 1.0 || rng.uniform() < probability)
|
||||
perturb_num(bias, magnitude);
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -78,7 +78,7 @@ namespace qp
|
|||
}
|
||||
};
|
||||
|
||||
ostream& operator<<(ostream& os, const neuron& n)
|
||||
std::ostream& operator<<(std::ostream& os, const neuron& n)
|
||||
{
|
||||
return os << *n.n << " " << n.out << " " << n.delta << " "
|
||||
<< n.ndelta << " " << n.dbias1 << " " << n.dbias2 << " "
|
||||
|
|
@ -164,7 +164,7 @@ namespace qp
|
|||
else
|
||||
{
|
||||
eta *= backtrack_step;
|
||||
eta = max(eta, eta_floor);
|
||||
eta = std::max(eta, eta_floor);
|
||||
momentum = eta * lambda;
|
||||
weight_update(ts.size(), false, eta, momentum);
|
||||
error_ = last_error;
|
||||
|
|
@ -206,15 +206,15 @@ namespace qp
|
|||
ew += n->dxo * n->dweight1;
|
||||
}
|
||||
|
||||
me = max(static_cast<real>(sqrt(me)), me_floor);
|
||||
mw = max(static_cast<real>(sqrt(mw)), mw_floor);
|
||||
me = std::max(static_cast<real>(sqrt(me)), me_floor);
|
||||
mw = std::max(static_cast<real>(sqrt(mw)), mw_floor);
|
||||
eta *= (1.0 + 0.5 * ew / ( me * mw));
|
||||
eta = max(eta, eta_floor);
|
||||
eta = std::max(eta, eta_floor);
|
||||
lambda = lambda0 * me / mw;
|
||||
momentum = eta * lambda;
|
||||
#ifdef DEBUG
|
||||
cout << me << " \t" << mw << " \t" << ew << " \t"
|
||||
<< eta << " \t" << momentum << " \t" << lambda << endl;
|
||||
std::cout << me << " \t" << mw << " \t" << ew << " \t"
|
||||
<< eta << " \t" << momentum << " \t" << lambda << std::endl;
|
||||
#endif // DEBUG
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -13,139 +13,139 @@
|
|||
#include <numeric> // inner_product
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// vector + vector
|
||||
// std::vector + std::vector
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
template<class T> vector<T> operator+(const vector<T>& v1, const vector<T>& v2)
|
||||
template<class T> std::vector<T> operator+(const std::vector<T>& v1, const std::vector<T>& v2)
|
||||
{
|
||||
vector<T> tmp = v1;
|
||||
transform(tmp.begin(), tmp.end(), v2.begin(), tmp.begin(), plus<T>());
|
||||
std::vector<T> tmp = v1;
|
||||
std::transform(tmp.begin(), tmp.end(), v2.begin(), tmp.begin(), std::plus<T>());
|
||||
return tmp;
|
||||
}
|
||||
|
||||
template<class T> vector<T> operator-(const vector<T>& v1, const vector<T>& v2)
|
||||
template<class T> std::vector<T> operator-(const std::vector<T>& v1, const std::vector<T>& v2)
|
||||
{
|
||||
vector<T> tmp = v1;
|
||||
transform(tmp.begin(), tmp.end(), v2.begin(), tmp.begin(), minus<T>());
|
||||
std::vector<T> tmp = v1;
|
||||
std::transform(tmp.begin(), tmp.end(), v2.begin(), tmp.begin(), std::minus<T>());
|
||||
return tmp;
|
||||
}
|
||||
|
||||
template<class T> T operator*(const vector<T>& v1, const vector<T>& v2)
|
||||
template<class T> T operator*(const std::vector<T>& v1, const std::vector<T>& v2)
|
||||
{
|
||||
return inner_product(v1.begin(), v1.end(), v2.begin(), static_cast<T>(0));
|
||||
}
|
||||
|
||||
template<class T> T operator/(const vector<T>& v1, const vector<T>& v2)
|
||||
template<class T> T operator/(const std::vector<T>& v1, const std::vector<T>& v2)
|
||||
{
|
||||
return inner_product(v1.begin(), v1.end(), v2.begin(), static_cast<T>(0),
|
||||
plus<T>(), divides<T>());
|
||||
std::plus<T>(), std::divides<T>());
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// vector += vector
|
||||
// std::vector += std::vector
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
template<class T> vector<T>& operator+=(vector<T>& v1, const vector<T>& v2)
|
||||
template<class T> std::vector<T>& operator+=(std::vector<T>& v1, const std::vector<T>& v2)
|
||||
{
|
||||
transform(v1.begin(), v1.end(), v2.begin(), v1.begin(), plus<T>());
|
||||
std::transform(v1.begin(), v1.end(), v2.begin(), v1.begin(), std::plus<T>());
|
||||
return v1;
|
||||
}
|
||||
|
||||
template<class T> vector<T>& operator-=(vector<T>& v1, const vector<T>& v2)
|
||||
template<class T> std::vector<T>& operator-=(std::vector<T>& v1, const std::vector<T>& v2)
|
||||
{
|
||||
transform(v1.begin(), v1.end(), v2.begin(), v1.begin(), minus<T>());
|
||||
std::transform(v1.begin(), v1.end(), v2.begin(), v1.begin(), std::minus<T>());
|
||||
return v1;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// vector + number
|
||||
// std::vector + number
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
template<class A, class B> vector<A> operator+(const vector<A>& a, const B& b)
|
||||
template<class A, class B> std::vector<A> operator+(const std::vector<A>& a, const B& b)
|
||||
{
|
||||
vector<A> tmp = a;
|
||||
transform(tmp.begin(), tmp.end(), tmp.begin(), bind2nd(plus<A>(), b));
|
||||
std::vector<A> tmp = a;
|
||||
std::transform(tmp.begin(), tmp.end(), tmp.begin(), std::bind2nd(std::plus<A>(), b));
|
||||
return tmp;
|
||||
}
|
||||
|
||||
template<class A, class B> vector<A> operator-(const vector<A>& a, const B& b)
|
||||
template<class A, class B> std::vector<A> operator-(const std::vector<A>& a, const B& b)
|
||||
{
|
||||
vector<A> tmp = a;
|
||||
transform(tmp.begin(), tmp.end(), tmp.begin(), bind2nd(minus<A>(), b));
|
||||
std::vector<A> tmp = a;
|
||||
std::transform(tmp.begin(), tmp.end(), tmp.begin(), std::bind2nd(std::minus<A>(), b));
|
||||
return tmp;
|
||||
}
|
||||
|
||||
template<class A, class B> vector<A> operator*(const vector<A>& a, const B& b)
|
||||
template<class A, class B> std::vector<A> operator*(const std::vector<A>& a, const B& b)
|
||||
{
|
||||
vector<A> tmp = a;
|
||||
transform(tmp.begin(), tmp.end(), tmp.begin(), bind2nd(multiplies<A>(), b));
|
||||
std::vector<A> tmp = a;
|
||||
std::transform(tmp.begin(), tmp.end(), tmp.begin(), std::bind2nd(std::multiplies<A>(), b));
|
||||
return tmp;
|
||||
}
|
||||
|
||||
template<class A, class B> vector<A> operator/(const vector<A>& a, const B& b)
|
||||
template<class A, class B> std::vector<A> operator/(const std::vector<A>& a, const B& b)
|
||||
{
|
||||
vector<A> tmp = a;
|
||||
transform(tmp.begin(), tmp.end(), tmp.begin(), bind2nd(divides<A>(), b));
|
||||
std::vector<A> tmp = a;
|
||||
std::transform(tmp.begin(), tmp.end(), tmp.begin(), std::bind2nd(std::divides<A>(), b));
|
||||
return tmp;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// number + vector
|
||||
// number + std::vector
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
template<class A, class B> vector<A> operator+(const B& b, const vector<A>& a)
|
||||
template<class A, class B> std::vector<A> operator+(const B& b, const std::vector<A>& a)
|
||||
{
|
||||
vector<A> tmp = a;
|
||||
transform(tmp.begin(), tmp.end(), tmp.begin(), bind2nd(plus<A>(), b));
|
||||
std::vector<A> tmp = a;
|
||||
std::transform(tmp.begin(), tmp.end(), tmp.begin(), std::bind2nd(std::plus<A>(), b));
|
||||
return tmp;
|
||||
}
|
||||
|
||||
template<class A, class B> vector<A> operator-(const B& b, const vector<A>& a)
|
||||
template<class A, class B> std::vector<A> operator-(const B& b, const std::vector<A>& a)
|
||||
{
|
||||
vector<A> tmp(a.size(), b);
|
||||
transform(tmp.begin(), tmp.end(), a.begin(), tmp.begin(), minus<A>());
|
||||
std::vector<A> tmp(a.size(), b);
|
||||
std::transform(tmp.begin(), tmp.end(), a.begin(), tmp.begin(), std::minus<A>());
|
||||
return tmp;
|
||||
}
|
||||
|
||||
template<class A, class B> vector<A> operator*(const B& b, const vector<A>& a)
|
||||
template<class A, class B> std::vector<A> operator*(const B& b, const std::vector<A>& a)
|
||||
{
|
||||
vector<A> tmp = a;
|
||||
transform(tmp.begin(), tmp.end(), tmp.begin(), bind2nd(multiplies<A>(), b));
|
||||
std::vector<A> tmp = a;
|
||||
std::transform(tmp.begin(), tmp.end(), tmp.begin(), bind2nd(std::multiplies<A>(), b));
|
||||
return tmp;
|
||||
}
|
||||
|
||||
template<class A, class B> vector<A> operator/(const B& b, const vector<A>& a)
|
||||
template<class A, class B> std::vector<A> operator/(const B& b, const std::vector<A>& a)
|
||||
{
|
||||
vector<A> tmp(a.size(), b);
|
||||
transform(tmp.begin(), tmp.end(), a.begin(), tmp.begin(), divides<A>());
|
||||
std::vector<A> tmp(a.size(), b);
|
||||
std::transform(tmp.begin(), tmp.end(), a.begin(), tmp.begin(), std::divides<A>());
|
||||
return tmp;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// vector += number
|
||||
// std::vector += number
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
template<class A, class B> vector<A>& operator+=(vector<A>& a, const B& b)
|
||||
template<class A, class B> std::vector<A>& operator+=(std::vector<A>& a, const B& b)
|
||||
{
|
||||
transform(a.begin(), a.end(), a.begin(), bind2nd(plus<A>(), b));
|
||||
std::transform(a.begin(), a.end(), a.begin(), std::bind2nd(std::plus<A>(), b));
|
||||
return a;
|
||||
}
|
||||
|
||||
template<class A, class B> vector<A>& operator-=(vector<A>& a, const B& b)
|
||||
template<class A, class B> std::vector<A>& operator-=(std::vector<A>& a, const B& b)
|
||||
{
|
||||
transform(a.begin(), a.end(), a.begin(), bind2nd(minus<A>(), b));
|
||||
std::transform(a.begin(), a.end(), a.begin(), std::bind2nd(std::minus<A>(), b));
|
||||
return a;
|
||||
}
|
||||
|
||||
template<class A, class B> vector<A>& operator*=(vector<A>& a, const B& b)
|
||||
template<class A, class B> std::vector<A>& operator*=(std::vector<A>& a, const B& b)
|
||||
{
|
||||
transform(a.begin(), a.end(), a.begin(), bind2nd(multiplies<A>(), b));
|
||||
std::transform(a.begin(), a.end(), a.begin(), std::bind2nd(std::multiplies<A>(), b));
|
||||
return a;
|
||||
}
|
||||
|
||||
template<class A, class B> vector<A>& operator/=(vector<A>& a, const B& b)
|
||||
template<class A, class B> std::vector<A>& operator/=(std::vector<A>& a, const B& b)
|
||||
{
|
||||
transform(a.begin(), a.end(), a.begin(), bind2nd(divides<A>(), b));
|
||||
std::transform(a.begin(), a.end(), a.begin(), std::bind2nd(std::divides<A>(), b));
|
||||
return a;
|
||||
}
|
||||
|
||||
|
|
@ -153,25 +153,25 @@ template<class A, class B> vector<A>& operator/=(vector<A>& a, const B& b)
|
|||
// I/O
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
template<class T> ostream& operator<<(ostream& os, const vector<T>& v)
|
||||
template<class T> std::ostream& operator<<(std::ostream& os, const std::vector<T>& v)
|
||||
{
|
||||
os << '<';
|
||||
if (v.size())
|
||||
{
|
||||
copy(v.begin(), v.end() - 1, ostream_iterator<T>(os, " "));
|
||||
std::copy(v.begin(), v.end() - 1, std::ostream_iterator<T>(os, " "));
|
||||
os << v.back();
|
||||
}
|
||||
return os << '>';
|
||||
}
|
||||
|
||||
template<class T> istream& operator>>(istream& is, vector<T>& v)
|
||||
template<class T> std::istream& operator>>(std::istream& is, std::vector<T>& v)
|
||||
{
|
||||
v.clear();
|
||||
|
||||
char c;
|
||||
is >> c;
|
||||
if (!is || c != '<')
|
||||
is.setstate(ios::failbit);
|
||||
is.setstate(std::ios::failbit);
|
||||
else
|
||||
{
|
||||
T t;
|
||||
|
|
@ -194,8 +194,8 @@ template<class T> istream& operator>>(istream& is, vector<T>& v)
|
|||
// euclidean_distance
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
template<class T> T euclidean_distance(const vector<T>& v1,
|
||||
const vector<T>& v2)
|
||||
template<class T> T euclidean_distance(const std::vector<T>& v1,
|
||||
const std::vector<T>& v2)
|
||||
{
|
||||
T sum = 0, tmp;
|
||||
|
||||
|
|
|
|||
Reference in a new issue