SANN: Sushi Artificial Neural Network
This is a short library for a simple but efficient neural network
Network.hpp
1 /*******************************************************
2  * *
3  * sann: Neural Network library *
4  * *
5  * NETWORK CLASS HEADER *
6  * *
7  * Giulio Auriemma *
8  * *
9  *******************************************************/
10 
11 #ifndef S_NETWORK_S
12 #define S_NETWORK_S
13 
14 // System libaries include.
15 #include <vector>
16 
17 // My include
18 #include "Layer.hpp"
19 #include "Estimator.hpp"
20 #include "math/Func.hpp"
21 #include "math/Plotter.hpp"
22 
23 namespace sann{
24 
26 class Network{
27 private:
28  // ATTRIBUTES
29 
30  std::vector<Layer> layers;
31  size_t inputSize;
32  std::shared_ptr<std::function<std::vector<double>(const std::vector<double> &target,
33  const std::vector<double> &out)>> errorFunc;
34 
35  // METHODS
36 
37  void trainStep(const std::vector<double> &trainPattern, const std::vector<double> &expectedResults,
38  sann::Estimator &est);
39 
40 public:
41  // STATIC ATTRIBUTES
42  static sann::Estimator &nullEstimator;
43  typedef std::function<std::vector<double>(const std::vector<double> &target,
44  const std::vector<double> &out)> error_func;
45 
46  // CONSTRUCTORS
47 
48  Network();
49  Network(const std::vector<size_t> &layers, const math::Func &activationFunc, const Layer::weights_initializer &init);
50  Network(const std::vector<size_t> &layers, const std::vector<math::Func> &activationFuncs,
51  const Layer::weights_initializer &init);
52  Network(const Network &net);
53 
54  // OPERATORS
55 
56  Network& operator = (const Network &rhs);
57  Network& operator = (Network &&rhs);
58 
59  // METHODS
60 
61  void setParameters(const sann::parameters &hyperP);
62  void setWeights(const std::vector<std::vector<double>> &weights);
63  void setWeights(const std::vector<weightsMatrix> &weights);
64  void setWeights(std::vector<weightsMatrix> &&weights);
65  void setRandomWeights();
66  void setErrorFunction(const error_func &error);
67  std::vector<weightsMatrix> getWeights() const;
68  std::vector<std::size_t> getlayersSizes() const;
69 
70  // Computation
71  std::vector<double> compute(const std::vector<double> &inputs);
72 
73  // Train.
74  void train(const sann::dataSet &trainingSet, sann::Estimator &est, const sann::parameters &hyperPar);
75  void train(const sann::dataSet &trainingSet, const sann::dataSet &testSet, sann::Estimator &trainEst,
76  sann::Estimator &testEst, const sann::parameters &hyperPar);
77 };
78 
79 }
80 
81 
82 
83 #endif
std::vector< double > compute(const std::vector< double > &inputs)
Computes the result for the given inputs.
Definition: Network.cpp:226
This class represent a mathematical function used as activation function.
Definition: Func.hpp:24
void setErrorFunction(const error_func &error)
Sets a new error function to minimize. This error function accepts in input the target value and the ...
Definition: Network.cpp:185
This is the struct that represents the dataset to handle. It has three attributes: ...
Definition: dataStructures.h:27
std::vector< weightsMatrix > getWeights() const
Returns a vector with the matrix of weights of each layer.
Definition: Network.cpp:194
std::vector< std::size_t > getlayersSizes() const
Returns a vector with the size of each layer.
Definition: Network.cpp:208
The parameters object. It holds all the settable hyper-parameters:
Definition: dataStructures.h:78
Definition: Estimator.hpp:28
Definition: constants.h:15
Network & operator=(const Network &rhs)
Copy assignment.
Definition: Network.cpp:120
This is the core class, that represents the whole Neural Network.
Definition: Network.hpp:26
Network()
Creates a new empty network.
Definition: Network.cpp:61
void train(const sann::dataSet &trainingSet, sann::Estimator &est, const sann::parameters &hyperPar)
Trains the network using the training set passed as input.
Definition: Network.cpp:280