SuperNN  0.7.0
runner.cpp
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 #include "neuron.hpp"
21 #include "runner.hpp"
22 #include "foreach.hpp"
23 
24 namespace SuperNN
25 {
26 
28 {
29 }
30 
31 Runner::Runner(const std::string &net_path, const std::string &info_path)
32 {
33  net.load_file(net_path);
34  load_info_file(info_path);
35 }
36 
38 {
39 }
40 
42 {
43  const unsigned n_out = net[net.size() - 1].size();
44  unsigned n_inp = from.size() - n_out; // not so smart foreach!
45 
46  inp_data = Data(1, n_inp);
47  out_data = Data(1, n_out);
48 
49  inp_data.add();
50  out_data.add();
51 
52  // input neurons
53  foreach(i, 0, n_inp)
54  {
55  inp_data.from[i] = from[i];
56  inp_data.to[i] = to[i];
57  }
58 
59  // output neurons
60  foreach(i, n_inp, from.size())
61  {
62  unsigned idx = i - n_inp;
63  out_data.from[idx] = from[i];
64  out_data.to[idx] = to[i];
65  }
66 }
67 
68 void Runner::load_info_file(const std::string &info_path)
69 {
70  Bounds from, to;
71  Bounds::load_file(info_path, from, to);
72  set_bounds(from, to);
73 }
74 
75 double &Runner::inp(unsigned idx)
76 {
77  return inp_data[0][idx];
78 }
79 
80 double Runner::out(unsigned idx)
81 {
82  return out_data[0][idx];
83 }
84 
86 {
87  inp_data.scale();
88  out_data[0] = net.run(inp_data[0]);
89  out_data.descale();
90 }
91 
92 }
void set_bounds(Bounds &from, Bounds &to)
Sets the bounds from already built Bound objects.
Definition: runner.cpp:41
void load_info_file(const std::string &info_path)
Loads the bounds info file.
Definition: runner.cpp:68
Bounds from
Original data bounds.
Definition: data.hpp:261
void run()
Runs the network with the current input setted by inp().
Definition: runner.cpp:85
double & inp(unsigned idx)
Accessor to get/set an input value.
Definition: runner.cpp:75
double out(unsigned idx)
Accessor to get an output value.
Definition: runner.cpp:80
void load_file(std::ifstream &inp)
Reads the scaling information from a file stream.
Definition: data.cpp:84
virtual ~Runner()
Definition: runner.cpp:37
void scale()
Scales the data, neuron per neuron, using the current from and to bounds.
Definition: data.cpp:324
Bounds to
Scaled data bounds.
Definition: data.hpp:264
Data scaling information, for all input and output neurons.
Definition: data.hpp:47
const Row & run(const Row &in, bool calc_error=false)
Propagates an input in the network.
Definition: network.cpp:177
Network net
Definition: runner.hpp:89
void load_file(const std::string &path)
Loads the network contents from a file.
Definition: network.cpp:381
unsigned size() const
Returns the number of layers.
Definition: network.cpp:534
Data used in training, validation and testing.
Definition: data.hpp:95
void descale()
Descales the data.
Definition: data.cpp:339
Row & add()
Adds a row to the data, returning a reference to it.
Definition: data.cpp:210