RapidLib  v2.2.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 
12 #include <vector>
13 #include "baseModel.h"
14 
15 #ifndef EMSCRIPTEN
16 #include "../dependencies/json/json.h"
17 #endif
18 
23 template<typename T>
24 class neuralNetwork final : public baseModel<T>
25 {
26 public:
28  neuralNetwork(const size_t& num_inputs,
29  const std::vector<size_t>& which_inputs,
30  const size_t& num_hidden_layers,
31  const size_t& num_hidden_nodes,
32  const std::vector<T>& weights,
33  const std::vector<T>& wHiddenOutput,
34  const std::vector<T>& inRanges,
35  const std::vector<T>& inBases,
36  const T& outRange,
37  const T& outBase);
38 
48  neuralNetwork(const size_t& num_inputs,
49  const std::vector<size_t>& which_inputs,
50  const size_t& num_hidden_layer,
51  const size_t& num_hidden_nodes);
52 
55 
60  T run(const std::vector<T>& inputVector) override;
61 
62  void reset() override;
63 
64  size_t getNumInputs() const override;
65  std::vector<size_t> getWhichInputs() const override;
66 
67  size_t getNumHiddenLayers() const;
68  void setNumHiddenLayers(size_t num_hidden_layers);
69 
70  size_t getNumHiddenNodes() const;
71  void setNumHiddenNodes(size_t num_hidden_nodes);
72 
73  size_t getEpochs() const;
74  void setEpochs(const size_t& epochs);
75 
76  std::vector<T> getWeights() const;
77  std::vector<T> getWHiddenOutput() const;
78 
79  std::vector<T> getInRanges() const;
80  std::vector<T> getInBases() const;
81  T getOutRange() const;
82  T getOutBase() const;
83 
84 #ifndef EMSCRIPTEN
85  void getJSONDescription(Json::Value& currentModel) override;
86 #endif
87 
88 
89 private:
91  size_t numInputs;
92  std::vector<size_t> whichInputs;
93  size_t numHiddenLayers;
94  size_t numHiddenNodes;
95 
97  std::vector<T> inputNeurons;
98  std::vector<std::vector<T> > hiddenNeurons;
99  T outputNeuron;
100 
102  std::vector<std::vector<std::vector<T> > > weights;
103  std::vector<T> wHiddenOutput;
104 
106  std::vector<T> inRanges;
107  std::vector<T> inBases;
108  T outRange;
109  T outBase;
110 
112  inline T activationFunction(T);
113 
116 
117 public:
123  void train(const std::vector<trainingExampleTemplate<T> >& trainingSet) override;
124 
131  void train(const std::vector<trainingExampleTemplate<T> >& trainingSet, const std::size_t whichOutput) override;
132 
134  size_t getCurrentEpoch() const;
135 
136 private:
138  T learningRate { 0.3 };
139  T momentum { 0.2 };
140  size_t numEpochs { 500 };
141  size_t currentEpoch { 0 };
142 
144  std::vector<std::vector< std::vector<T> > > deltaWeights;
145  std::vector<T> deltaHiddenOutput;
146 
148  T outputErrorGradient;
149  inline T getHiddenErrorGradient(size_t layer, size_t neuron);
150 
151  void initTrainer();
152 
156  void backpropagate(const T& desiredOutput);
157 
159  void updateWeights();
160 };
161 
162 #endif
Definition: baseModel.h:42
Definition: neuralNetwork.h:25
std::vector< T > getInBases() const
Definition: neuralNetwork.cpp:289
T getOutRange() const
Definition: neuralNetwork.cpp:295
std::vector< T > getWeights() const
Definition: neuralNetwork.cpp:260
T getOutBase() const
Definition: neuralNetwork.cpp:301
void train(const std::vector< trainingExampleTemplate< T > > &trainingSet) override
These pertain to the training, and aren't need to run a trained model //.
Definition: neuralNetwork.cpp:402
neuralNetwork(const size_t &num_inputs, const std::vector< size_t > &which_inputs, const size_t &num_hidden_layers, const size_t &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:41
void setNumHiddenNodes(size_t num_hidden_nodes)
Definition: neuralNetwork.cpp:241
void reset() override
Definition: neuralNetwork.cpp:146
size_t getNumInputs() const override
Definition: neuralNetwork.cpp:209
void setNumHiddenLayers(size_t num_hidden_layers)
Definition: neuralNetwork.cpp:227
void getJSONDescription(Json::Value &currentModel) override
Definition: neuralNetwork.cpp:308
std::vector< size_t > getWhichInputs() const override
Definition: neuralNetwork.cpp:215
size_t getNumHiddenNodes() const
Definition: neuralNetwork.cpp:235
T run(const std::vector< T > &inputVector) override
Definition: neuralNetwork.cpp:357
~neuralNetwork()
Definition: neuralNetwork.cpp:141
void setEpochs(const size_t &epochs)
Definition: neuralNetwork.cpp:254
std::vector< T > getInRanges() const
Definition: neuralNetwork.cpp:283
size_t getNumHiddenLayers() const
Definition: neuralNetwork.cpp:221
size_t getEpochs() const
Definition: neuralNetwork.cpp:248
std::vector< T > getWHiddenOutput() const
Definition: neuralNetwork.cpp:277
size_t getCurrentEpoch() const
Definition: neuralNetwork.cpp:526
Definition: trainingExample.h:19