Copiste  0.1
 All Classes Functions Variables Enumerations Friends Pages
neuralnetwork.h
1 /*
2 * This file is part of Copiste.
3 *
4 * Copiste is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU 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 * Copiste 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 General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with Copiste. If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #ifndef INCLUDEDNEURALNETWORKH
19 #define INCLUDEDNEURALNETWORKH
20 
21 #include <string>
22 
23 // Math functions
24 #include <cmath>
25 #include <cstdlib>
26 
27 // Matrix and vector management
28 #include <boost/numeric/ublas/matrix.hpp>
29 #include <boost/numeric/ublas/matrix_proxy.hpp>
30 #include <boost/numeric/ublas/io.hpp>
31 
32 // Serialization
33 #include <fstream>
34 #include <boost/archive/text_oarchive.hpp>
35 #include <boost/archive/text_iarchive.hpp>
36 #include <boost/serialization/vector.hpp>
37 
38 // Temporaire
39 #include "corpus.h"
40 
41 
42 using namespace boost::numeric;
43 
44 double sigmoid(double x);
45 
48 {
49  public:
51  NeuralNetwork(std::string file = "");
52 
58  void reset(std::vector<int> geometry);
59 
61  bool fromFile(std::string file);
62 
64  bool toFile(std::string file);
65 
67  double train(Corpus &c, double rate, double regularization, int nbIter, bool debug = false);
68 
70  double classify(std::vector<double> input);
71 
73  double accuracy(Corpus &c);
74 
76  void randomize();
77 
79 
81  unsigned int nbLayers() { return mLayers.size(); }
82 
84  unsigned int dimension();
85 
86  private:
87  std::vector< ublas::matrix<double> > mLayers;
88 
89  double gradientDescent(ublas::matrix<double> &ds, ublas::vector<double> &tv, double rate, unsigned int steps);
90 
92  ublas::matrix<double> classify(ublas::matrix<double> input);
93 
95  std::vector< ublas::matrix<double> > gradient(ublas::matrix<double> &ds, ublas::vector<double> &tv, double regularization = 0);
96 
98  std::vector< ublas::matrix<double> > gradientChecking(ublas::matrix<double> &ds, ublas::vector<double> &tv, double regularization = 0, double epsilon = 0.0001);
99 
101  double costFunction(ublas::matrix<double> &ds, ublas::vector<double> &tv, double regularization = 0);
102 
104 
106  static ublas::matrix<double> createDataset(Corpus &c);
107 
109  static ublas::vector<double> createTargetVector(Corpus &c);
110 
112 
114  static ublas::vector<double> addOne(ublas::vector<double> v);
115 
117  static ublas::matrix<double> addOne(ublas::matrix<double> v);
118 
120  static ublas::matrix_range<ublas::matrix<double> > removeOnes(ublas::matrix<double> m);
121 
123  static ublas::matrix_range<ublas::matrix<double> > jthCol(ublas::matrix<double> m, size_t j);
124 
126  static ublas::vector<double> elementWise(ublas::vector<double> v, double (*f)(double));
127 
129  static ublas::matrix<double> elementWise(ublas::matrix<double> v, double (*f)(double));
130 
132  static ublas::matrix<double> vecToMat(ublas::vector<double> v);
133 
135  static ublas::vector<double> unroll(ublas::matrix<double> m);
136 
138  static ublas::matrix<double> roll(ublas::vector<double> m, unsigned int r, unsigned int c);
139 
141  friend class boost::serialization::access;
142  template<class Archive>
143  void serialize(Archive & ar, const unsigned int version);
144 
145 };
146 
147 
148 #endif