SuperNN  0.7.0
network.hpp
1 /*
2  This file is part of SuperNN.
3 
4  SuperNN is free software: you can redistribute it and/or modify
5  it under the terms of the GNU Lesser General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  SuperNN is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU Lesser General Public License for more details.
13 
14  You should have received a copy of the GNU Lesser General Public License
15  along with SuperNN. If not, see <http://www.gnu.org/licenses/>.
16 
17  Copyright (C) 2010 - 2014 Lucas Hermann Negri
18 */
19 
20 #ifndef SUPERNN_NETWORK_HPP
21 #define SUPERNN_NETWORK_HPP
22 
23 #include <vector>
24 
25 #include "data.hpp"
26 #include "activation_type.hpp"
27 
28 namespace SuperNN
29 {
30 class Neuron;
31 
36 struct Layer : public std::vector<Neuron>
37 {
38  Layer();
39  virtual ~Layer();
40 
46  void add_neuron(Neuron &n);
47 
54  void add_neurons(unsigned n_neurons, bool bias = false);
55 
63  void set_activation(ActFuncType type, double s = 1);
64 
71  void connect(unsigned to_layer, unsigned to_neuron);
72 };
73 
78 struct Network
79 {
89  static Network make_mlp(unsigned input, unsigned hidden, unsigned output);
90 
99  static Network make_mlp(unsigned input, unsigned output);
100 
111  static Network make_mlp(unsigned input, unsigned hidden1, unsigned hidden2, unsigned output);
112 
121  static Network make_fcc(unsigned input, unsigned hidden, unsigned output);
122 
123  Network();
124  virtual ~Network();
125 
131  void add_layer(Layer &l);
132 
138  void add_layers(unsigned n_layers);
139 
148  const Row &run(const Row &in, bool calc_error = false);
149 
157  double calc_mse(const Data &data);
158 
166  double calc_mae(const Data &data);
167 
177  double calc_class(const Data &data, double limit = 0.5);
178 
188  double calc_class_higher(const Data &data);
189 
196  void init_weights(double min = -0.5, double max = 0.5);
197 
205  void set_activation(ActFuncType type, double s = 1);
206 
213  void save_file(const std::string &path) const;
214 
221  void load_file(const std::string &path);
222 
230  void connect(unsigned from_layer, unsigned to_layer);
231 
239  void connect_neuron_to_layer(unsigned from_layer, unsigned from_neuron, unsigned to_layer);
240 
247  void clear_neurons(bool clear_delta, bool clear_run);
248 
254  unsigned calc_num_weights() const;
255 
261  unsigned calc_num_neurons() const;
262 
266  unsigned calc_num_inputs() const;
267 
274  const Layer &operator[](unsigned l) const;
275 
282  Layer &operator[](unsigned l);
283 
289  unsigned size() const;
290 
292  std::vector<Layer> layers;
293 
296 
297 protected:
303  unsigned n_input;
304 };
305 }
306 
307 #endif
Neuron, that can contain connections to neurons in the next layers.
Definition: neuron.hpp:70
double calc_class(const Data &data, double limit=0.5)
Calculates the classification rate of the network related to a data.
Definition: network.cpp:276
void connect_neuron_to_layer(unsigned from_layer, unsigned from_neuron, unsigned to_layer)
Connects a neuron to all the neurons of another layer.
Definition: network.cpp:455
unsigned calc_num_weights() const
Calculates the current number of weights.
Definition: network.cpp:487
virtual ~Layer()
Definition: network.cpp:35
void add_neurons(unsigned n_neurons, bool bias=false)
Adds a number of neurons to the layer.
Definition: network.cpp:44
void set_activation(ActFuncType type, double s=1)
Sets the activation function for all the neurons currently in the network.
Definition: network.cpp:341
double calc_mse(const Data &data)
Calculates the mean squared error of the network related to a data.
Definition: network.cpp:234
std::vector< Layer > layers
Neuron layers.
Definition: network.hpp:292
unsigned calc_num_inputs() const
Calculates the number of neurons on the first layer that aren't biases.
Definition: network.cpp:511
void add_layer(Layer &l)
Adds a layer to the network.
Definition: network.cpp:67
double calc_mae(const Data &data)
Calculates the mean absolute error of the network related to a data.
Definition: network.cpp:255
void add_neuron(Neuron &n)
Adds a neuron to the layer.
Definition: network.cpp:39
static Network make_mlp(unsigned input, unsigned hidden, unsigned output)
Constructs a 'standard' feed forward neural network with one hidden layer.
Definition: network.cpp:91
void add_layers(unsigned n_layers)
Adds a number of layers to the network.
Definition: network.cpp:72
static Network make_fcc(unsigned input, unsigned hidden, unsigned output)
Constructs a fully connected cascade neural network.
Definition: network.cpp:156
unsigned n_input
Last computed number of neurons in the input layer that aren't biases, computed by run()...
Definition: network.hpp:303
void init_weights(double min=-0.5, double max=0.5)
Initializes the weights with pseudo-ramdom numbers.
Definition: network.cpp:327
void connect(unsigned from_layer, unsigned to_layer)
Connects all the neurons from a layer to all the neurons of another layer.
Definition: network.cpp:449
unsigned calc_num_neurons() const
Calculates the current number of neurons.
Definition: network.cpp:501
const Row & run(const Row &in, bool calc_error=false)
Propagates an input in the network.
Definition: network.cpp:177
double calc_class_higher(const Data &data)
Calculates the classification rate of the network related to a data.
Definition: network.cpp:299
Artificial neural network structure that supports arbitrary feedforward topologies, like multilayer perceptrons and fully connected cascade networks.
Definition: network.hpp:78
void set_activation(ActFuncType type, double s=1)
Sets the activation function for all the neurons currently in the layer.
Definition: network.cpp:55
const Layer & operator[](unsigned l) const
Returns a const reference to a layer.
Definition: network.cpp:524
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.
Definition: network.cpp:61
void load_file(const std::string &path)
Loads the network contents from a file.
Definition: network.cpp:381
std::vector< double > Row
Data row.
Definition: data.hpp:90
Array of neurons.
Definition: network.hpp:36
unsigned size() const
Returns the number of layers.
Definition: network.cpp:534
Row last_output
Structure that holds the last output values.
Definition: network.hpp:295
Data used in training, validation and testing.
Definition: data.hpp:95
void clear_neurons(bool clear_delta, bool clear_run)
Clears the neuron state.
Definition: network.cpp:463
void save_file(const std::string &path) const
Saves the network contents to a file, for latter use.
Definition: network.cpp:347
virtual ~Network()
Definition: network.cpp:87