22 #include "activation.hpp"
25 #include "network.hpp"
45 reserve(size() + n_neurons);
47 for(
unsigned n = 0; n < n_neurons; ++n)
56 for(
unsigned n = 0, e = size(); n < e; ++n)
57 at(n).set_activation(type, s);
62 for(
unsigned n = 0, e = size(); n < e; ++n)
63 at(n).connect(to_layer, to_neuron);
75 for(
unsigned l = 0; l < n_layers; ++l)
160 net.
layers[0].add_neurons(input);
161 net.
layers[0].add_neurons(1,
true);
163 for(
unsigned l = 1; l <= hidden; ++l)
164 net.
layers[l].add_neurons(1);
166 unsigned last = net.
layers.size() - 1;
167 net.
layers[last].add_neurons(output);
169 for(
unsigned l1 = 0; l1 < last; ++l1)
170 for(
unsigned l2 = l1+1; l2 <= last; ++l2)
178 const unsigned last_layer =
layers.size() - 1;
179 const unsigned in_size = in.size();
185 for(
unsigned i = 0, e =
layers[0].
size(); i < e; ++i)
197 for(
unsigned l = 0; l < last_layer; ++l)
199 for(
unsigned n = 0, e =
layers[l].
size(); n < e; ++n)
203 for(
unsigned c = 0, e = neuron.
size(); c < e; ++c)
211 for(
unsigned n = 0, e =
layers[l + 1].
size(); n < e; ++n)
218 const unsigned n_out =
layers[last_layer].size();
221 for(
unsigned n = 0; n < n_out; ++n)
235 const unsigned n_out =
layers[
layers.size() - 1].size();
238 for(
unsigned p = 0, e = data.size(); p < e; ++p)
240 const Row& r =
run(data[p],
true);
242 for(
unsigned n = 0; n < n_out; ++n)
244 double err = data[p][
n_input + n] - r[n];
256 const unsigned n_out =
layers[
layers.size() - 1].size();
259 for(
unsigned p = 0, e = data.size(); p < e; ++p)
261 const Row& r =
run(data[p],
true);
263 for(
unsigned n = 0; n < n_out; ++n)
265 double err = data[p][
n_input + n] - r[n];
266 mae += std::abs(err);
279 for(
unsigned p = 0, e = data.size(); p < e; ++p)
282 const unsigned last =
layers.size() - 1;
284 const unsigned n_output =
layers[last].size();
286 for(
unsigned n = 0; n < n_output; ++n)
288 if(std::abs(
layers[last][n].out - data[p][
n_input + n]) < limit)
296 return nt * 100 / (double)data.size();
303 for(
unsigned p = 0, e = data.size(); p < e; ++p)
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 for(
unsigned n = 0; n < n_output; ++n)
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 for(
unsigned l = 0, e =
layers.size(); l < e; ++l)
331 for(
unsigned n = 0, e =
layers[l].
size(); n < e; ++n)
335 for(
unsigned c = 0, e = neuron.
size(); c < e; ++c)
343 for(
unsigned l = 0, e =
layers.size(); l < e; ++l)
350 out.open(path.c_str());
355 out.imbue(std::locale(
"C"));
357 out <<
layers.size() << std::endl;
360 for(
unsigned l = 0, e =
layers.size(); l < e; ++l)
362 out <<
layers[l].size() << std::endl;
364 for(
unsigned n = 0, e =
layers[l].
size(); n < e; ++n)
367 out << neuron.
act_func <<
" " << neuron.
steep <<
" " << neuron.
bias << std::endl;
368 out << neuron.
size() << std::endl;
370 for(
unsigned c = 0, e = neuron.
size(); c < e; ++c)
388 inp.open(path.c_str());
393 inp.imbue(std::locale(
"C"));
406 for(
int l = 0; l < n_layers; ++l)
417 layers[l].resize(n_neurons);
419 for(
int n = 0; n < n_neurons; ++n)
423 inp >> act >> neuron.
steep >> bias;
425 neuron.
bias = (bool)bias;
436 neuron.
conns.resize(n_conns);
438 for(
int c = 0; c < n_conns; ++c)
451 for(
unsigned n = 0, e =
layers[to_layer].
size(); n < e; ++n)
459 for(
unsigned n = 0, e =
layers[to_layer].
size(); n < e; ++n)
465 for(
unsigned l = 0, e =
layers.size(); l < e; ++l)
467 for(
unsigned n = 0, e =
layers[l].
size(); n < e; ++n)
479 neuron.
out = neuron.
bias ? 1 : 0;
489 unsigned n_weights = 0;
491 for(
unsigned l = 0, e =
layers.size(); l < e; ++l)
495 for(
unsigned n = 0, e = layer.size(); n < e; ++n)
496 n_weights += layer[n].
size();
504 unsigned n_neurons = 0;
506 for(
unsigned l = 0, e =
layers.size(); l < e; ++l)
514 unsigned n_inputs = 0;
516 for(
unsigned i = 0, e =
layers[0].
size(); i < e; ++i)
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) )
SUPERNN_EXPORT 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.