* whitespace cleanup

This commit is contained in:
Caner Candan 2011-05-05 17:15:10 +02:00
commit 70e60a50d2
195 changed files with 1763 additions and 1873 deletions

View file

@ -18,15 +18,14 @@ LINK_DIRECTORIES(${EO_BINARY_DIR}/lib)
SET (GPROP_SOURCES gprop.cpp)
# especially for Visual Studio
IF(NOT WIN32 OR CYGWIN)
ADD_EXECUTABLE(gprop ${GPROP_SOURCES})
IF(NOT WIN32 OR CYGWIN)
ADD_EXECUTABLE(gprop ${GPROP_SOURCES})
ADD_DEPENDENCIES(gprop eo eoutils)
TARGET_LINK_LIBRARIES(gprop eo eoutils)
TARGET_LINK_LIBRARIES(gprop eo eoutils)
SET(GPROP_VERSION ${GLOBAL_VERSION})
SET_TARGET_PROPERTIES(gprop PROPERTIES VERSION "${GPROP_VERSION}")
ENDIF(NOT WIN32 OR CYGWIN)
######################################################################################

View file

@ -207,12 +207,12 @@ int correct(const mlp::net& net, const mlp::set& set)
unsigned partial = 0;
for (unsigned i = 0; i < s->output.size(); ++i)
if ((s->output[i] < 0.5 && net(s->input)[i] < 0.5) ||
(s->output[i] > 0.5 && net(s->input)[i] > 0.5))
++partial;
if ((s->output[i] < 0.5 && net(s->input)[i] < 0.5) ||
(s->output[i] > 0.5 && net(s->input)[i] > 0.5))
++partial;
if (partial == s->output.size())
++sum;
++sum;
}
return sum;

View file

@ -29,7 +29,7 @@ namespace l2
//---------------------------------------------------------------------------
// error
//---------------------------------------------------------------------------
real error(const mlp::net& net, const set& ts)
{
real error_ = 0.0;
@ -37,12 +37,12 @@ namespace l2
for (set::const_iterator s = ts.begin(); s != ts.end(); ++s)
{
vector out = net(s->input);
for (unsigned i = 0; i < out.size(); ++i)
{
real target = s->output[i];
real value = out[i];
error_ -= target * log(value + min_real) +
error_ -= target * log(value + min_real) +
(1.0 - target) * log(1.0 - value + min_real);
}
}
@ -53,25 +53,25 @@ namespace l2
//-------------------------------------------------------------------------
// l2
//-------------------------------------------------------------------------
class net: public qp::net
{
public:
net(mlp::net& n): qp::net(n) {}
real error(const set& ts)
{
real error_ = 0;
for (set::const_iterator s = ts.begin(); s != ts.end(); ++s)
{
forward(s->input);
error_ -= backward(s->input, s->output);
}
return error_;
}
private:
real backward(const vector& input, const vector& output)
{
@ -84,7 +84,7 @@ namespace l2
{
neuron& n = (*current_layer)[j];
real out = output[j];
n.ndelta += n.delta = (out - n.out) /
n.ndelta += n.delta = (out - n.out) /
(n.out * (1.0 - n.out) + min_real) * n.out * (1.0 - n.out);
if (size() == 1) // monolayer
@ -92,21 +92,21 @@ namespace l2
else // multilayer
for (unsigned k = 0; k < n.dxo.size(); ++k)
n.dxo[k] += n.delta * (*backward_layer)[k].out;
error_ += out * log(n.out + min_real) +
error_ += out * log(n.out + min_real) +
(1.0 - out) * log(1.0 - n.out + min_real);
}
// hidden layers
while (++current_layer != rend())
{
reverse_iterator forward_layer = current_layer - 1;
reverse_iterator backward_layer = current_layer + 1;
reverse_iterator backward_layer = current_layer + 1;
for (unsigned j = 0; j < current_layer->size(); ++j)
{
neuron& n = (*current_layer)[j];
real sum = 0;
real sum = 0;
for (unsigned k = 0; k < forward_layer->size(); ++k)
{
neuron& nf = (*forward_layer)[k];
@ -114,7 +114,7 @@ namespace l2
}
n.delta = n.out * (1.0 - n.out) * sum;
n.ndelta += n.delta;
if (backward_layer == rend()) // first hidden layer
n.dxo += n.delta * input;
else // rest of hidden layers
@ -122,19 +122,19 @@ namespace l2
n.dxo[k] += n.delta * (*backward_layer)[k].out;
}
}
return error_;
}
};
//---------------------------------------------------------------------------
} // namespace l2
//-----------------------------------------------------------------------------
#endif // l2_h
// Local Variables:
// mode:C++
// Local Variables:
// mode:C++
// End:

View file

@ -45,7 +45,7 @@ namespace std {
istream& operator>>(istream& is, mlp::vector& v)
{
for (mlp::vector::iterator vi = v.begin() ; vi != v.end() ; vi++) {
is >> *vi;
is >> *vi;
}
return is;
}
@ -133,15 +133,15 @@ namespace mlp
#ifdef HAVE_LIBYAML_CPP
YAML_SERIALIZABLE_AUTO(neuron)
void emit_yaml(YAML::Emitter&out) const {
out << YAML::BeginMap;
out << YAML::Key << "Class" << YAML::Value << "mlp::neuron";
YAML_EMIT_MEMBER(out,bias);
YAML_EMIT_MEMBER(out,weight);
out << YAML::EndMap;
out << YAML::BeginMap;
out << YAML::Key << "Class" << YAML::Value << "mlp::neuron";
YAML_EMIT_MEMBER(out,bias);
YAML_EMIT_MEMBER(out,weight);
out << YAML::EndMap;
}
void load_yaml(const YAML::Node& node) {
YAML_LOAD_MEMBER(node, bias);
YAML_LOAD_MEMBER(node, weight);
YAML_LOAD_MEMBER(node, bias);
YAML_LOAD_MEMBER(node, weight);
}
#endif
};
@ -213,17 +213,17 @@ namespace mlp {
}
#ifdef HAVE_LIBYAML_CPP
friend ostream& operator<<(YAML::Emitter& e, const layer &l) {
e << ((std::vector<neuron>)l);
e << ((std::vector<neuron>)l);
}
friend void operator>>(const YAML::Node& n, layer &l) {
// These temporary variable shenanegins are necessary because
// the compiler gets very confused about which template operator>>
// function to use.
// The following does not work: n >> l;
// So we use a temporary variable thusly:
std::vector<mlp::neuron> *obviously_a_vector = &l;
n >> *obviously_a_vector;
// These temporary variable shenanegins are necessary because
// the compiler gets very confused about which template operator>>
// function to use.
// The following does not work: n >> l;
// So we use a temporary variable thusly:
std::vector<mlp::neuron> *obviously_a_vector = &l;
n >> *obviously_a_vector;
}
#endif
@ -243,7 +243,7 @@ namespace std {
istream& operator>>(istream& is, mlp::layer& l)
{
for (mlp::layer::iterator li = l.begin() ; li != l.end() ; li++) {
is >> *li;
is >> *li;
}
return is;
}
@ -277,15 +277,15 @@ namespace mlp {
#ifdef HAVE_LIBYAML_CPP
YAML_SERIALIZABLE_AUTO(net)
void emit_members(YAML::Emitter&out) const {
const std::vector<layer>* me_as_layer_vector = this;
out << YAML::Key << "layers" << YAML::Value << *me_as_layer_vector;
const std::vector<layer>* me_as_layer_vector = this;
out << YAML::Key << "layers" << YAML::Value << *me_as_layer_vector;
}
void load_members(const YAML::Node& node) {
std::vector<layer>* me_as_layer_vector = this;
node["layers"] >> *me_as_layer_vector;
std::vector<layer>* me_as_layer_vector = this;
node["layers"] >> *me_as_layer_vector;
}
#endif // HAVE_LIBYAML_CPP
#endif // HAVE_LIBYAML_CPP
/** Virtual destructor */
virtual ~net() {};
@ -303,14 +303,14 @@ namespace mlp {
is >> layer_size;
layer_sizes.push_back(layer_size);
}
unsigned check_outputs;
unsigned check_outputs;
is >> check_outputs;
assert (check_outputs == num_outputs);
init (num_inputs,num_outputs,layer_sizes);
// skip forward to pass up opening '<' char
char c=' ';
while (c!='<' && !is.eof()) { is >> c;}
for (iterator l =begin() ; l != end(); l++) {
// skip forward to pass up opening '<' char
char c=' ';
while (c!='<' && !is.eof()) { is >> c;}
for (iterator l =begin() ; l != end(); l++) {
is >> *l;
}
do { is >> c; } while (c == ' ' && !is.eof());
@ -351,15 +351,15 @@ namespace mlp {
}
void save(ostream &os) const {
// Save the number of inputs, number of outputs, and number of hidden layers
// Save the number of inputs, number of outputs, and number of hidden layers
os << num_inputs() << "\n" << num_outputs() << "\n" << num_hidden_layers() << "\n";
for(const_iterator l = begin(); l != end(); ++l)
for(const_iterator l = begin(); l != end(); ++l)
os << l->size() << " ";
os << "\n";
os << "< ";
for(const_iterator l = begin(); l != end(); ++l)
os << "\n";
os << "< ";
for(const_iterator l = begin(); l != end(); ++l)
os << *l << " ";
os << ">\n";
os << ">\n";
}
@ -454,7 +454,7 @@ namespace mlp {
void load(istream &is) {
unsigned input_size, output_size;
is >> input_size >> output_size;
is >> input_size >> output_size;
sample samp(input_size, output_size);;
while (is >> samp) { push_back(samp); }
}

View file

@ -28,7 +28,7 @@ namespace mse
//---------------------------------------------------------------------------
// error
//---------------------------------------------------------------------------
real error(const mlp::net& net, const set& ts)
{
real error_ = 0.0;
@ -36,7 +36,7 @@ namespace mse
for (set::const_iterator s = ts.begin(); s != ts.end(); ++s)
{
vector out = net(s->input);
for (unsigned i = 0; i < out.size(); ++i)
{
real diff = s->output[i] - out[i];
@ -49,26 +49,26 @@ namespace mse
//-------------------------------------------------------------------------
// mse
//-------------------------------------------------------------------------
class net: public qp::net
{
public:
net(mlp::net& n): qp::net(n) {}
real error(const set& ts)
{
real error_ = 0;
for (set::const_iterator s = ts.begin(); s != ts.end(); ++s)
{
forward(s->input);
error_ += backward(s->input, s->output);
}
error_ /= ts.size();
return error_;
}
private:
real backward(const vector& input, const vector& output)
{
@ -89,22 +89,22 @@ namespace mse
else // multilayer
for (unsigned k = 0; k < n.dxo.size(); ++k)
n.dxo[k] += n.delta * (*backward_layer)[k].out;
error_ += diff * diff;
}
// hidden layers
while (++current_layer != rend())
{
reverse_iterator forward_layer = current_layer - 1;
reverse_iterator backward_layer = current_layer + 1;
for (unsigned j = 0; j < current_layer->size(); ++j)
{
neuron& n = (*current_layer)[j];
real sum = 0;
for (unsigned k = 0; k < forward_layer->size(); ++k)
{
neuron& nf = (*forward_layer)[k];
@ -113,8 +113,8 @@ namespace mse
n.delta = n.out * (1.0 - n.out) * sum;
n.ndelta += n.delta;
if (backward_layer == rend()) // first hidden layer
n.dxo += n.delta * input;
else // rest of hidden layers
@ -122,19 +122,19 @@ namespace mse
n.dxo[k] += n.delta * (*backward_layer)[k].out;
}
}
return error_;
}
};
//---------------------------------------------------------------------------
} // namespace mse
//-----------------------------------------------------------------------------
#endif // mse_h
// Local Variables:
// mode:C++
// Local Variables:
// mode:C++
// End:

View file

@ -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:

View file

@ -160,14 +160,14 @@ template<class T> std::ostream& operator<<(std::ostream& os, const std::vector<T
{
std::copy(v.begin(), v.end() - 1, std::ostream_iterator<T>(os, " "));
os << v.back();
}
}
return os << '>';
}
template<class T> std::istream& operator>>(std::istream& is, std::vector<T>& v)
{
v.clear();
char c;
is >> c;
if (!is || c != '<')
@ -186,7 +186,7 @@ template<class T> std::istream& operator>>(std::istream& is, std::vector<T>& v)
}
} while (is && c != '>');
}
return is;
}
@ -194,11 +194,11 @@ template<class T> std::istream& operator>>(std::istream& is, std::vector<T>& v)
// euclidean_distance
//-----------------------------------------------------------------------------
template<class T> T euclidean_distance(const std::vector<T>& v1,
template<class T> T euclidean_distance(const std::vector<T>& v1,
const std::vector<T>& v2)
{
T sum = 0, tmp;
for (unsigned i = 0; i < v1.size(); ++i)
{
tmp = v1[i] - v2[i];
@ -211,4 +211,3 @@ template<class T> T euclidean_distance(const std::vector<T>& v1,
//-----------------------------------------------------------------------------
#endif