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