* whitespace cleanup
This commit is contained in:
parent
56c6edab04
commit
70e60a50d2
195 changed files with 1763 additions and 1873 deletions
|
|
@ -41,55 +41,55 @@ namespace qp
|
|||
const real backtrack_step = 0.5;
|
||||
const real me_floor = 0.0001;
|
||||
const real mw_floor = 0.0001;
|
||||
|
||||
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
// neuron
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
|
||||
struct neuron
|
||||
{
|
||||
mlp::neuron* n;
|
||||
real out, delta, ndelta, dbias1, dbias2;
|
||||
vector dweight1, dweight2, dxo;
|
||||
|
||||
neuron(mlp::neuron& _n):
|
||||
n(&_n), out(0), delta(0), ndelta(0), dbias1(0), dbias2(0),
|
||||
dweight1(n->weight.size(), 0),
|
||||
dweight2(n->weight.size(), 0),
|
||||
|
||||
neuron(mlp::neuron& _n):
|
||||
n(&_n), out(0), delta(0), ndelta(0), dbias1(0), dbias2(0),
|
||||
dweight1(n->weight.size(), 0),
|
||||
dweight2(n->weight.size(), 0),
|
||||
dxo(n->weight.size(), 0) {}
|
||||
|
||||
|
||||
void reset()
|
||||
{
|
||||
// underlaying neuron
|
||||
n->reset();
|
||||
|
||||
|
||||
// addons
|
||||
out = delta = ndelta = dbias1 = dbias2 = 0;
|
||||
fill(dweight1.begin(), dweight1.end(), 0);
|
||||
fill(dweight2.begin(), dweight2.end(), 0);
|
||||
fill(dxo.begin(), dxo.end(), 0);
|
||||
}
|
||||
|
||||
|
||||
real operator()(const vector& input)
|
||||
{
|
||||
return out = mlp::sigmoid(n->bias + dbias1 +
|
||||
return out = mlp::sigmoid(n->bias + dbias1 +
|
||||
(n->weight + dweight1) * input);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
std::ostream& operator<<(std::ostream& os, const neuron& n)
|
||||
{
|
||||
return os << *n.n << " " << n.out << " " << n.delta << " "
|
||||
<< n.ndelta << " " << n.dbias1 << " " << n.dbias2 << " "
|
||||
return os << *n.n << " " << n.out << " " << n.delta << " "
|
||||
<< n.ndelta << " " << n.dbias1 << " " << n.dbias2 << " "
|
||||
<< n.dweight1 << " " << n.dweight2 << " " << n.dxo;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
// layer
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class layer: public std::vector<neuron>
|
||||
{
|
||||
public:
|
||||
|
|
@ -102,16 +102,16 @@ namespace qp
|
|||
void reset()
|
||||
{
|
||||
for(iterator n = begin(); n != end(); ++n)
|
||||
n->reset();
|
||||
n->reset();
|
||||
}
|
||||
|
||||
vector operator()(const vector& input)
|
||||
{
|
||||
vector output(size());
|
||||
|
||||
|
||||
for(unsigned i = 0; i < output.size(); ++i)
|
||||
output[i] = (*this)[i](input);
|
||||
|
||||
output[i] = (*this)[i](input);
|
||||
|
||||
return output;
|
||||
}
|
||||
};
|
||||
|
|
@ -120,10 +120,10 @@ namespace qp
|
|||
//---------------------------------------------------------------------------
|
||||
// net
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class net: public std::vector<layer>
|
||||
{
|
||||
public:
|
||||
public:
|
||||
net(mlp::net& n) //: std::vector<layer>(n.begin(), n.end()) { reset(); }
|
||||
{
|
||||
for (mlp::net::iterator l = n.begin(); l != n.end(); ++l)
|
||||
|
|
@ -135,27 +135,27 @@ namespace qp
|
|||
void reset()
|
||||
{
|
||||
for(iterator l = begin(); l != end(); ++l)
|
||||
l->reset();
|
||||
l->reset();
|
||||
}
|
||||
|
||||
real train(const set& ts,
|
||||
unsigned epochs,
|
||||
real target_error,
|
||||
|
||||
real train(const set& ts,
|
||||
unsigned epochs,
|
||||
real target_error,
|
||||
real tolerance,
|
||||
real eta = eta_default,
|
||||
real momentum = alpha_default,
|
||||
real eta = eta_default,
|
||||
real momentum = alpha_default,
|
||||
real lambda = lambda_default)
|
||||
{
|
||||
real error_ = max_real;
|
||||
|
||||
|
||||
while (epochs-- && error_ > target_error)
|
||||
{
|
||||
real last_error = error_;
|
||||
|
||||
|
||||
init_delta();
|
||||
|
||||
error_ = error(ts);
|
||||
|
||||
|
||||
if (error_ < last_error + tolerance)
|
||||
{
|
||||
coeff_adapt(eta, momentum, lambda);
|
||||
|
|
@ -170,10 +170,10 @@ namespace qp
|
|||
error_ = last_error;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return error_;
|
||||
}
|
||||
|
||||
|
||||
virtual real error(const set& ts) = 0;
|
||||
|
||||
// protected:
|
||||
|
|
@ -185,7 +185,7 @@ namespace qp
|
|||
input.swap(tmp);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// private:
|
||||
void init_delta()
|
||||
{
|
||||
|
|
@ -193,11 +193,11 @@ namespace qp
|
|||
for (layer::iterator n = l->begin(); n != l->end(); ++n)
|
||||
fill(n->dxo.begin(), n->dxo.end(), n->ndelta = 0.0);
|
||||
}
|
||||
|
||||
|
||||
void coeff_adapt(real& eta, real& momentum, real& lambda)
|
||||
{
|
||||
real me = 0, mw = 0, ew = 0;
|
||||
|
||||
|
||||
for (iterator l = begin(); l != end(); ++l)
|
||||
for (layer::iterator n = l->begin(); n != l->end(); ++n)
|
||||
{
|
||||
|
|
@ -205,7 +205,7 @@ namespace qp
|
|||
mw += n->dweight1 * n->dweight1;
|
||||
ew += n->dxo * n->dweight1;
|
||||
}
|
||||
|
||||
|
||||
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));
|
||||
|
|
@ -213,11 +213,11 @@ namespace qp
|
|||
lambda = lambda0 * me / mw;
|
||||
momentum = eta * lambda;
|
||||
#ifdef DEBUG
|
||||
std::cout << me << " \t" << mw << " \t" << ew << " \t"
|
||||
std::cout << me << " \t" << mw << " \t" << ew << " \t"
|
||||
<< eta << " \t" << momentum << " \t" << lambda << std::endl;
|
||||
#endif // DEBUG
|
||||
}
|
||||
|
||||
|
||||
void weight_update(unsigned size, bool fire, real eta, real momentum)
|
||||
{
|
||||
for (iterator l = begin(); l != end(); ++l)
|
||||
|
|
@ -239,13 +239,13 @@ namespace qp
|
|||
};
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
|
||||
} // namespace qp
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#endif // qp_h
|
||||
|
||||
// Local Variables:
|
||||
// mode:C++
|
||||
// Local Variables:
|
||||
// mode:C++
|
||||
// End:
|
||||
|
|
|
|||
Reference in a new issue