NeuralNet 1.0
Loading...
Searching...
No Matches
Network.hpp
1#pragma once
2
3#include <cereal/cereal.hpp> // for defer
4#include <cereal/types/memory.hpp>
5#include <cereal/types/vector.hpp>
6#include <cstdlib>
7#include <memory>
8#include <variant>
9#include <vector>
10
11#include "Model.hpp"
12#include "callbacks/Callback.hpp"
13#include "data/TrainingData.hpp"
14#include "layers/Dense.hpp"
15#include "layers/Dropout.hpp"
16#include "layers/Flatten.hpp"
17#include "layers/Layer.hpp"
18#include "losses/losses.hpp"
19#include "optimizers/Optimizer.hpp"
20#include "optimizers/optimizers.hpp"
21#include "utils/Formatters.hpp"
22#include "utils/Functions.hpp"
23#include "utils/Gauge.hpp"
24#include "utils/Variants.hpp"
25
26namespace NeuralNet {
27class Layer;
28
29class Network : public Model {
30 public:
31 Network();
32
40 void setup(const std::shared_ptr<Optimizer> &optimizer,
41 LOSS loss = LOSS::QUADRATIC);
42
48 void addLayer(std::shared_ptr<Layer> &layer);
49
55 void setLoss(LOSS loss);
56
64 std::shared_ptr<Layer> getLayer(int index) const;
65
72 std::shared_ptr<Layer> getOutputLayer() const;
73
80 size_t getNumLayers() const;
81
88 std::string getSlug() const;
89
103 double train(std::vector<std::vector<double>> X, std::vector<double> y,
104 int epochs = 1,
105 const std::vector<std::shared_ptr<Callback>> callbacks = {},
106 bool progBar = true);
107
121 double train(std::vector<std::vector<std::vector<double>>> X,
122 std::vector<double> y, int epochs = 1,
123 const std::vector<std::shared_ptr<Callback>> callbacks = {},
124 bool progBar = true);
125
138 double train(
139 TrainingData<std::vector<std::vector<double>>, std::vector<double>>
140 trainingData,
141 int epochs = 1,
142 const std::vector<std::shared_ptr<Callback>> callbacks = {},
143 bool progBar = true);
144
157 double train(TrainingData<std::vector<std::vector<std::vector<double>>>,
158 std::vector<double>>
159 trainingData,
160 int epochs = 1,
161 const std::vector<std::shared_ptr<Callback>> callbacks = {},
162 bool progBar = true);
163
171 Eigen::MatrixXd predict(std::vector<std::vector<double>> inputs);
172
180 Eigen::MatrixXd predict(std::vector<std::vector<std::vector<double>>> inputs);
181
187 void to_file(const std::string &filename) override {
188 // Serializing model to a binary file
189 std::ofstream file(filename, std::ios::binary);
190 cereal::BinaryOutputArchive archive(file);
191 archive(*this);
192 }
193
199 void from_file(const std::string &filename) override {
200 // Making sure the file exists and is binary
201 assert(fileExistsWithExtension(filename, ".bin") &&
202 "The file doesn't exists or is not binary '.bin'");
203
204 // Deserializing the model from the binary file
205 std::ifstream file(filename, std::ios::binary);
206 cereal::BinaryInputArchive archive(file);
207 archive(*this);
208 }
209
210 ~Network();
211
212 private:
213 // non-public serialization
214 friend class cereal::access;
215
216 std::vector<std::shared_ptr<Layer>> layers;
217 LOSS lossFunc =
218 LOSS::QUADRATIC; // Storing the loss function for serialization
219 bool progBar = true;
220 double (*cmpLoss)(const Eigen::MatrixXd &, const Eigen::MatrixXd &);
221 Eigen::MatrixXd (*cmpLossGrad)(const Eigen::MatrixXd &,
222 const Eigen::MatrixXd &);
223 std::shared_ptr<Optimizer> optimizer;
224
225 template <class Archive>
226 void save(Archive &archive) const {
227 archive(cereal::base_class<Model>(this), layers, lossFunc);
228 archive.serializeDeferments();
229 };
230
231 template <class Archive>
232 void load(Archive &archive) {
233 archive(cereal::base_class<Model>(this), layers, lossFunc);
234 setLoss(lossFunc);
235 }
236
257 template <typename D1, typename D2>
258 double onlineTraining(std::vector<D1> inputs, std::vector<D2> labels,
259 int epochs,
260 std::vector<std::shared_ptr<Callback>> callbacks = {});
261
279 template <typename D1, typename D2>
280 double trainer(TrainingData<D1, D2> trainingData, int epochs,
281 std::vector<std::shared_ptr<Callback>> callbacks = {});
282
300 template <typename D1, typename D2>
301 double miniBatchTraining(
302 TrainingData<D1, D2> trainingData, int epochs,
303 std::vector<std::shared_ptr<Callback>> callbacks = {});
304
321 template <typename D1, typename D2>
322 double batchTraining(TrainingData<D1, D2> trainingData, int epochs,
323 std::vector<std::shared_ptr<Callback>> callbacks = {});
324
333 Eigen::MatrixXd forwardProp(
334 std::vector<std::vector<std::vector<double>>> &inputs,
335 bool training = false);
336
345 Eigen::MatrixXd forwardProp(std::vector<std::vector<double>> &inputs,
346 bool training = false);
347
356 Eigen::MatrixXd forwardProp(Eigen::MatrixXd &inputs, bool training = false);
357
358 Eigen::MatrixXd feedForward(Eigen::MatrixXd inputs, int startIdx = 0,
359 bool training = false);
360
368 void backProp(Eigen::MatrixXd &outputs, Eigen::MatrixXd &y);
369
379 void trainingCheckpoint(std::string checkpointName,
380 std::vector<std::shared_ptr<Callback>> callbacks);
381
391 double computeAccuracy(Eigen::MatrixXd &outputs, Eigen::MatrixXd &y);
392
398 void updateOptimizerSetup(size_t numLayers);
399};
400} // namespace NeuralNet
401
402CEREAL_REGISTER_TYPE(NeuralNet::Network);
403
404CEREAL_REGISTER_POLYMORPHIC_RELATION(NeuralNet::Model, NeuralNet::Network);
Definition Model.hpp:17
Definition Network.hpp:29
void setLoss(LOSS loss)
This method will set the network's loss function.
Definition Network.cpp:28
size_t getNumLayers() const
This method will get you the number of layers currently in the Network.
Definition Network.cpp:7
void addLayer(std::shared_ptr< Layer > &layer)
Method to add a layer to the network.
Definition Network.cpp:17
std::shared_ptr< Layer > getLayer(int index) const
This method will return the Layer residing at the specified index.
Definition Network.cpp:48
std::string getSlug() const
Get the slug of the network based on it's architecture.
Definition Network.cpp:58
void setup(const std::shared_ptr< Optimizer > &optimizer, LOSS loss=LOSS::QUADRATIC)
Method that sets up the model's hyperparameter.
Definition Network.cpp:9
double train(std::vector< std::vector< double > > X, std::vector< double > y, int epochs=1, const std::vector< std::shared_ptr< Callback > > callbacks={}, bool progBar=true)
This method will Train the model with the given inputs and labels.
Definition Network.cpp:70
void from_file(const std::string &filename) override
Load a model's params from a file.
Definition Network.hpp:199
std::shared_ptr< Layer > getOutputLayer() const
This method will return the output layer (the last layer of the network)
Definition Network.cpp:53
Eigen::MatrixXd predict(std::vector< std::vector< double > > inputs)
This model will try to make predictions based off the inputs passed.
Definition Network.cpp:274
void to_file(const std::string &filename) override
Save the current model to a binary file.
Definition Network.hpp:187
Definition TrainingData.hpp:10