NeuralNet 1.0
Loading...
Searching...
No Matches
Layer.hpp
1#pragma once
2
3#include <Eigen/Dense>
4#include <cereal/access.hpp>
5#include <cereal/archives/binary.hpp>
6#include <cereal/archives/portable_binary.hpp>
7#include <cereal/types/base_class.hpp>
8#include <cereal/types/common.hpp>
9#include <cereal/types/polymorphic.hpp>
10#include <cmath>
11#include <cstdlib>
12#include <random>
13#include <string>
14#include <tuple>
15
16#include "Network.hpp"
17#include "activations/activations.hpp"
18#include "utils/Enums.hpp"
19#include "utils/Functions.hpp"
20#include "utils/Serialize.hpp"
21
22namespace NeuralNet {
23// Should be updated when a new layer type is added
24enum class LayerType { DEFAULT, DENSE, FLATTEN, DROPOUT };
25
26class Layer {
27 friend class Network;
28
29 public:
30 Layer(){};
31
37 Eigen::MatrixXd getOutputs() const { return outputs; };
38
44 int getNumNeurons() const { return nNeurons; };
45
51 virtual std::string getSlug() const = 0;
52
56 void printOutputs() {
57 std::cout << this->outputs << "\n";
58 return;
59 };
60
68 virtual Eigen::MatrixXd feedInputs(std::vector<double> inputs,
69 bool training = false) {
70 return this->feedInputs(Eigen::MatrixXd::Map(&inputs[0], inputs.size(), 1));
71 };
72
80 virtual Eigen::MatrixXd feedInputs(Eigen::MatrixXd inputs,
81 bool training = false) = 0;
82
91 virtual void feedInputs(std::vector<std::vector<std::vector<double>>> inputs,
92 bool training = false) {
93 assert(false &&
94 "Cannot feed 3d vectors, a Flatten layer could do it though");
95 return;
96 };
97
104 const std::string typeStr() {
105 static const std::map<LayerType, std::string> typeMap = {
106 {LayerType::DEFAULT, "Base"},
107 {LayerType::DENSE, "Dense"},
108 {LayerType::FLATTEN, "Flatten"},
109 {LayerType::DROPOUT, "Dropout"}};
110
111 auto it = typeMap.find(this->type);
112 if (it != typeMap.end()) return it->second;
113
114 return "Unknown";
115 };
116
117 virtual ~Layer(){};
118
119 private:
120 // non-public serialization
121 friend class cereal::access;
122
123 // Necessary function for serializing Layer
124 template <class Archive>
125 void save(Archive &archive) const {
126 archive(outputs, type, trainingOnly, nNeurons);
127 };
128
129 template <class Archive>
130 void load(Archive &archive) {
131 archive(outputs, type, trainingOnly, nNeurons);
132 }
133
134 protected:
135 int nNeurons;
136 Eigen::MatrixXd outputs;
137 LayerType type = LayerType::DEFAULT;
138 bool trainingOnly = false; // If true skip during inferences
139
143 void setOutputs(Eigen::MatrixXd outputs) // used for the Flatten Layer
144 {
145 this->outputs = outputs;
146 };
147
158 void setOutputs(std::vector<double> outputs) {
159 assert(outputs.size() == nNeurons);
160 this->outputs =
161 Eigen::MatrixXd ::Map(&outputs[0], this->getNumNeurons(), 1);
162 };
163
173 virtual Eigen::MatrixXd computeOutputs(Eigen::MatrixXd inputs,
174 bool training = false) = 0;
175
181 virtual void init(int args) {};
182
183 Layer(std::tuple<int, int> inputShape)
184 : nNeurons(std::get<0>(inputShape) *
185 std::get<1>(inputShape)){}; // Used in Flatten layer
186};
187} // namespace NeuralNet
188
189CEREAL_REGISTER_TYPE(NeuralNet::Layer);
Definition Layer.hpp:26
virtual Eigen::MatrixXd feedInputs(Eigen::MatrixXd inputs, bool training=false)=0
This method is used to feed the inputs to the layer.
virtual Eigen::MatrixXd computeOutputs(Eigen::MatrixXd inputs, bool training=false)=0
This method is used to feed the inputs to the layer.
Eigen::MatrixXd getOutputs() const
This method get the layer's outputs.
Definition Layer.hpp:37
virtual std::string getSlug() const =0
The slug of the layers (name + main parameter value)
virtual Eigen::MatrixXd feedInputs(std::vector< double > inputs, bool training=false)
This method is used to feed the inputs to the layer.
Definition Layer.hpp:68
void setOutputs(Eigen::MatrixXd outputs)
Definition Layer.hpp:143
const std::string typeStr()
Definition Layer.hpp:104
void printOutputs()
Method to print layer's outputs.
Definition Layer.hpp:56
virtual void feedInputs(std::vector< std::vector< std::vector< double > > > inputs, bool training=false)
This method is used to feed the inputs to the layer.
Definition Layer.hpp:91
void setOutputs(std::vector< double > outputs)
This method is used to set the outputs of the layer.
Definition Layer.hpp:158
virtual void init(int args)
Definition Layer.hpp:181
int getNumNeurons() const
This method get the number of neurons actually in the layer.
Definition Layer.hpp:44
Definition Network.hpp:29