RapidLib  v2.1.0
A simple library for interactive machine learning
neuralNetwork.h
Go to the documentation of this file.
1 
9 #ifndef neuralNetwork_h
10 #define neuralNetwork_h
11 #include <vector>
12 #include "baseModel.h"
13 
14 #ifndef EMSCRIPTEN
15 #include "../dependencies/json/json.h"
16 #endif
17 
18 #define LEARNING_RATE 0.3
19 #define MOMENTUM 0.2
20 #define NUM_EPOCHS 500
21 
26 template<typename T>
27 class neuralNetwork : public baseModel<T> {
28 
29 public:
31  neuralNetwork(const int &num_inputs,
32  const std::vector<int> &which_inputs,
33  const int &num_hidden_layers,
34  const int &num_hidden_nodes,
35  const std::vector<T> &weights,
36  const std::vector<T> &wHiddenOutput,
37  const std::vector<T> &inRanges,
38  const std::vector<T> &inBases,
39  const T &outRange,
40  const T &outBase);
41 
51  neuralNetwork(const int &num_inputs,
52  const std::vector<int> &which_inputs,
53  const int &num_hidden_layer,
54  const int &num_hidden_nodes);
55 
58 
63  T run(const std::vector<T> &inputVector);
64 
65  void reset();
66 
67  int getNumInputs() const;
68  std::vector<int> getWhichInputs() const;
69 
70  int getNumHiddenLayers() const;
71  void setNumHiddenLayers(int num_hidden_layers);
72 
73  int getNumHiddenNodes() const;
74 
75  void setEpochs(const int &epochs);
76 
77  std::vector<T> getWeights() const;
78  std::vector<T> getWHiddenOutput() const;
79 
80  std::vector<T> getInRanges() const;
81  std::vector<T> getInBases() const;
82  T getOutRange() const;
83  T getOutBase() const;
84 
85 #ifndef EMSCRIPTEN
86  void getJSONDescription(Json::Value &currentModel);
87 #endif
88 
89 
90 private:
92  int numInputs;
93  std::vector<int> whichInputs;
94  int numHiddenLayers;
95  int numHiddenNodes;
96 
98  std::vector<T> inputNeurons;
99  std::vector<std::vector<T> > hiddenNeurons;
100  T outputNeuron;
101 
103  std::vector<std::vector<std::vector<T> > > weights;
104  std::vector<T> wHiddenOutput;
105 
107  std::vector<T> inRanges;
108  std::vector<T> inBases;
109  T outRange;
110  T outBase;
111 
113  inline T activationFunction(T);
114 
117 
118 public:
124  void train(const std::vector<trainingExampleTemplate<T> > &trainingSet);
125 
126 private:
128  T learningRate;
129  T momentum;
130  int numEpochs;
131 
133  std::vector<std::vector< std::vector<T> > > deltaWeights;
134  std::vector<T> deltaHiddenOutput;
135 
137  T outputErrorGradient;
138  inline T getHiddenErrorGradient(int layer, int neuron);
139 
140  void initTrainer();
141 
145  void backpropagate(const T &desiredOutput);
146 
148  void updateWeights();
149 };
150 
151 #endif
~neuralNetwork()
Definition: neuralNetwork.cpp:127
Definition: trainingExample.h:18
T getOutBase() const
Definition: neuralNetwork.cpp:252
T getOutRange() const
Definition: neuralNetwork.cpp:247
int getNumHiddenLayers() const
Definition: neuralNetwork.cpp:197
std::vector< int > getWhichInputs() const
Definition: neuralNetwork.cpp:193
void reset()
Definition: neuralNetwork.cpp:131
std::vector< T > getInBases() const
Definition: neuralNetwork.cpp:242
void setEpochs(const int &epochs)
Definition: neuralNetwork.cpp:214
neuralNetwork(const int &num_inputs, const std::vector< int > &which_inputs, const int &num_hidden_layers, const int &num_hidden_nodes, const std::vector< T > &weights, const std::vector< T > &wHiddenOutput, const std::vector< T > &inRanges, const std::vector< T > &inBases, const T &outRange, const T &outBase)
Definition: neuralNetwork.cpp:30
int getNumHiddenNodes() const
Definition: neuralNetwork.cpp:209
T run(const std::vector< T > &inputVector)
Definition: neuralNetwork.cpp:302
void setNumHiddenLayers(int num_hidden_layers)
Definition: neuralNetwork.cpp:202
std::vector< T > getWHiddenOutput() const
Definition: neuralNetwork.cpp:232
Definition: baseModel.h:23
void getJSONDescription(Json::Value &currentModel)
Definition: neuralNetwork.cpp:258
Definition: neuralNetwork.h:27
std::vector< T > getWeights() const
Definition: neuralNetwork.cpp:219
std::vector< T > getInRanges() const
Definition: neuralNetwork.cpp:237
int getNumInputs() const
Definition: neuralNetwork.cpp:188
void train(const std::vector< trainingExampleTemplate< T > > &trainingSet)
These pertain to the training, and aren&#39;t need to run a trained model //.
Definition: neuralNetwork.cpp:346