SuperNN  1.0.0
data.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_DATA_HPP
21 #define SUPERNN_DATA_HPP
22 
23 #include <vector>
24 #include <string>
25 #include <fstream>
26 #include "utils.hpp"
27 
28 namespace SuperNN
29 {
30 static const double FSMALL = 1e-9;
31 
33 struct SUPERNN_EXPORT SInfo
34 {
35  SInfo();
36  ~SInfo();
37 
38  SInfo(double _min, double _max);
39 
40  double scale(const SInfo &to, double value) const;
41  double min, max;
42 };
43 
47 struct SUPERNN_EXPORT Bounds : public std::vector<SInfo>
48 {
49  Bounds();
50  virtual ~Bounds();
51 
60  void merge_with(const Bounds &other);
61 
67  void save_file(std::ofstream &out) const;
68 
74  void load_file(std::ifstream &inp);
75 
84  static void load_file(const std::string &path, Bounds &from, Bounds &to);
85 };
86 
90 typedef std::vector<double> Row;
91 
95 struct SUPERNN_EXPORT Data : public std::vector<Row>
96 {
97  Data();
98  virtual ~Data();
99 
106  Data(unsigned rows, unsigned cols);
107 
114  Data drop_column(unsigned col) const;
115 
121  Row &add();
122 
130  Data(unsigned rows, unsigned cols, const double st[]);
131 
139  Data(unsigned rows, unsigned cols, const Row &row);
140 
144  void shuffle();
145 
153  Data sample(unsigned first, unsigned last) const;
154 
162  unsigned load_file(const std::string &path);
163 
170  void save_file(const std::string &path) const;
171 
178  void save_info_file(const std::string &path) const;
179 
186  Bounds &calc_bounds();
187 
195  void scale_column(unsigned n, const SInfo &curv, const SInfo &newv);
196 
202  void scale();
203 
213  void scale(double min, double max);
214 
218  void descale();
219 
232  void k_fold(unsigned n, unsigned k, Data &p, Data &l) const;
233 
242  inline double get(unsigned row, unsigned col) const
243  {
244  return at(row)[col];
245  }
246 
255  inline void set(unsigned row, unsigned col, double val)
256  {
257  at(row)[col] = val;
258  }
259 
262 
265 
267  size_t n_total;
268 };
269 }
270 
271 #endif
Bounds from
Original data bounds.
Definition: data.hpp:261
Minimum / maximum scaling information.
Definition: data.hpp:33
size_t n_total
Number of inputs + outputs per row.
Definition: data.hpp:267
Bounds to
Scaled data bounds.
Definition: data.hpp:264
Data scaling information, for all input and output neurons.
Definition: data.hpp:47
void set(unsigned row, unsigned col, double val)
Sets the value in the (row,col) cell to value.
Definition: data.hpp:255
double min
Definition: data.hpp:41
std::vector< double > Row
Data row.
Definition: data.hpp:90
Data used in training, validation and testing.
Definition: data.hpp:95