22 #include "activation.hpp"
25 #include "network.hpp"
26 #include "foreach.hpp"
46 reserve(size() + n_neurons);
48 foreach(n, 0, n_neurons)
58 at(n).set_activation(type, s);
64 at(n).connect(to_layer, to_neuron);
76 foreach(l, 0, n_layers)
161 net.
layers[0].add_neurons(input);
162 net.
layers[0].add_neurons(1,
true);
164 foreach(l, 1, hidden + 1)
165 net.
layers[l].add_neurons(1);
167 unsigned last = net.
layers.size() - 1;
168 net.
layers[last].add_neurons(output);
171 foreach(l2, l1 + 1, last + 1)
179 const unsigned last_layer =
layers.size() - 1;
180 const unsigned in_size = in.size();
198 foreach(l, 0, last_layer)
204 foreach(c, 0, neuron.
size())
219 const unsigned n_out =
layers[last_layer].size();
236 const unsigned n_out =
layers[
layers.size() - 1].size();
239 foreach(p, 0, data.size())
241 const Row& r =
run(data[p],
true);
245 double err = data[p][
n_input + n] - r[n];
257 const unsigned n_out =
layers[
layers.size() - 1].size();
260 foreach(p, 0, data.size())
262 const Row& r =
run(data[p],
true);
266 double err = data[p][
n_input + n] - r[n];
267 mae += std::abs(err);
280 foreach(p, 0, data.size())
283 const unsigned last =
layers.size() - 1;
285 const unsigned n_output =
layers[last].size();
287 foreach(n, 0, n_output)
289 if(std::abs(
layers[last][n].out - data[p][
n_input + n]) < limit)
296 return nt * 100 / (double)data.size();
303 foreach(p, 0, data.size())
306 const unsigned last =
layers.size() - 1;
307 const unsigned n_output =
layers[last].size();
308 unsigned max1 = -1, max2 = -1;
309 double val1 = -1e30, val2 = -1e30;
311 foreach(n, 0, n_output)
313 if(
layers[last][n].out > val1)
314 val1 =
layers[last][n].out, max1 = n;
316 if(data[p][
n_input + n] > val2)
317 val2 = data[p][
n_input + n], max2 = n;
324 return nt * 100 / (double)data.size();
329 foreach(l, 0,
layers.size())
335 foreach(c, 0, neuron.
size())
343 foreach(l, 0,
layers.size())
350 out.open(path.c_str());
355 out.imbue(std::locale(
"C"));
357 out <<
layers.size() << std::endl;
360 foreach(l, 0,
layers.size())
362 out <<
layers[l].size() << std::endl;
367 out << neuron.
act_func <<
" " << neuron.
steep <<
" " << neuron.
bias << std::endl;
368 out << neuron.
size() << std::endl;
370 foreach(c, 0, neuron.
size())
388 inp.open(path.c_str());
393 inp.imbue(std::locale(
"C"));
406 foreach(l, 0, n_layers)
417 layers[l].resize(n_neurons);
419 foreach(n, 0, n_neurons)
423 inp >> act >> neuron.
steep >> bias;
436 neuron.
conns.resize(n_conns);
438 foreach(c, 0, n_conns)
452 layers[from_layer].connect(to_layer, n);
465 foreach(l, 0,
layers.size())
479 neuron.
out = neuron.
bias ? 1 : 0;
489 unsigned n_weights = 0;
491 foreach(l, 0,
layers.size())
494 foreach(n, 0, layer.size())
495 n_weights += layer[n].
size();
503 unsigned n_neurons = 0;
505 foreach(l, 0,
layers.size())
513 unsigned n_inputs = 0;
Neuron, that can contain connections to neurons in the next layers.
std::vector< Connection > conns
Synaptic connections.
double calc_class(const Data &data, double limit=0.5)
Calculates the classification rate of the network related to a data.
void connect_neuron_to_layer(unsigned from_layer, unsigned from_neuron, unsigned to_layer)
Connects a neuron to all the neurons of another layer.
thrown when a file couldn't be opened
unsigned calc_num_weights() const
Calculates the current number of weights.
void add_neurons(unsigned n_neurons, bool bias=false)
Adds a number of neurons to the layer.
Synaptic connection between two neurons.
void set_activation(ActFuncType type, double s=1)
Sets the activation function for all the neurons currently in the network.
double calc_mse(const Data &data)
Calculates the mean squared error of the network related to a data.
bool delta_ok
Marks if the delta has been calculated for the current iteration.
std::vector< Layer > layers
Neuron layers.
const unsigned file_precision
Precision used when writting floating point number to files.
unsigned calc_num_inputs() const
Calculates the number of neurons on the first layer that aren't biases.
void add_layer(Layer &l)
Adds a layer to the network.
double calc_mae(const Data &data)
Calculates the mean absolute error of the network related to a data.
void add_neuron(Neuron &n)
Adds a neuron to the layer.
static Network make_mlp(unsigned input, unsigned hidden, unsigned output)
Constructs a 'standard' feed forward neural network with one hidden layer.
void add_layers(unsigned n_layers)
Adds a number of layers to the network.
double steep
Activation function steepness.
static double activation(const Neuron &neuron)
Calls the actual activation function.
static Network make_fcc(unsigned input, unsigned hidden, unsigned output)
Constructs a fully connected cascade neural network.
void connect(unsigned to_layer, unsigned to_neuron)
Adds a connection to a neuron.
double out
Last output of the neuron ( g(net) )
double rand_double(double max)
Returns a pseudo-random double.
double delta
Last local error gradient.
unsigned n_input
Last computed number of neurons in the input layer that aren't biases, computed by run()...
void init_weights(double min=-0.5, double max=0.5)
Initializes the weights with pseudo-ramdom numbers.
void connect(unsigned from_layer, unsigned to_layer)
Connects all the neurons from a layer to all the neurons of another layer.
unsigned calc_num_neurons() const
Calculates the current number of neurons.
const Row & run(const Row &in, bool calc_error=false)
Propagates an input in the network.
unsigned size() const
Returns the number of synaptic connections.
double calc_class_higher(const Data &data)
Calculates the classification rate of the network related to a data.
ActFuncType act_func
Used activation function.
Artificial neural network structure that supports arbitrary feedforward topologies, like multilayer perceptrons and fully connected cascade networks.
void set_activation(ActFuncType type, double s=1)
Sets the activation function for all the neurons currently in the layer.
double err
Last error (desired - actual).
const Layer & operator[](unsigned l) const
Returns a const reference to a layer.
unsigned to_neuron
Position of the target neuron in it's layer.
The exception can be identified by the type() method.
ActFuncType
Activation functions built-in in the library.
void connect(unsigned to_layer, unsigned to_neuron)
Connects all the neurons of the layer to a neuron.
void load_file(const std::string &path)
Loads the network contents from a file.
unsigned to_layer
Layer where the target neuron is located.
double net
Last sum of the neuron inputs.
std::vector< double > Row
Data row.
unsigned size() const
Returns the number of layers.
Row last_output
Structure that holds the last output values.
Data used in training, validation and testing.
thrown when a file has invalid contents
bool bias
Marks if it's a bias neuron.
thrown when the dimensions of a Row and the network does not match
void clear_neurons(bool clear_delta, bool clear_run)
Clears the neuron state.
void save_file(const std::string &path) const
Saves the network contents to a file, for latter use.