SuperNN  1.0.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 - 2015 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 struct Neuron;
31 
36 struct SUPERNN_EXPORT 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 SUPERNN_EXPORT 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
std::vector< Layer > layers
Neuron layers.
Definition: network.hpp:292
unsigned n_input
Last computed number of neurons in the input layer that aren't biases, computed by run()...
Definition: network.hpp:303
Artificial neural network structure that supports arbitrary feedforward topologies, like multilayer perceptrons and fully connected cascade networks.
Definition: network.hpp:78
ActFuncType
Activation functions built-in in the library.
std::vector< double > Row
Data row.
Definition: data.hpp:90
Array of neurons.
Definition: network.hpp:36
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