Deep Learning Algorithm Implementations 1.0.0
C++ implementations of fundamental deep learning algorithms
Loading...
Searching...
No Matches
layers.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <memory>
4#include <vector>
5#include <functional>
6#include "utils/autograd.hpp"
7#include "utils/matrix.hpp"
8
16namespace dl::layers {
17 using utils::Variable;
18 using utils::VariableD;
19 using utils::VariableF;
20 using utils::Matrix;
21 using utils::MatrixD;
22 using utils::MatrixF;
23
27 template<typename T>
28 class Module {
29 public:
30 virtual ~Module() = default;
31
37 virtual Variable<T> forward(const Variable<T>& input) = 0;
38
43 virtual std::vector<Variable<T>*> parameters() = 0;
44
48 virtual void zero_grad() {
49 for (auto* param : parameters()) {
50 param->zero_grad();
51 }
52 }
53
58 virtual void train(bool training = true) {
59 training_ = training;
60 }
61
65 virtual void eval() {
66 train(false);
67 }
68
72 bool is_training() const { return training_; }
73
74 protected:
75 bool training_ = true;
76 };
77
81 template<typename T>
82 class Linear : public Module<T> {
83 public:
90 Linear(size_t in_features, size_t out_features, bool bias = true);
91
97 Variable<T> forward(const Variable<T>& input) override;
98
102 std::vector<Variable<T>*> parameters() override;
103
104 // Getters for parameters
105 Variable<T>& weight() { return weight_; }
106 Variable<T>& bias() { return bias_; }
107 const Variable<T>& weight() const { return weight_; }
108 const Variable<T>& bias() const { return bias_; }
109
110 private:
111 Variable<T> weight_; // Shape: (out_features, in_features)
112 Variable<T> bias_; // Shape: (out_features,)
113 bool has_bias_;
114 size_t in_features_;
115 size_t out_features_;
116
117 void initialize_parameters();
118 };
119
123 template<typename T>
124 class ReLU : public Module<T> {
125 public:
126 Variable<T> forward(const Variable<T>& input) override;
127 std::vector<Variable<T>*> parameters() override { return {}; }
128 };
129
133 template<typename T>
134 class Sigmoid : public Module<T> {
135 public:
136 Variable<T> forward(const Variable<T>& input) override;
137 std::vector<Variable<T>*> parameters() override { return {}; }
138 };
139
143 template<typename T>
144 class Tanh : public Module<T> {
145 public:
146 Variable<T> forward(const Variable<T>& input) override;
147 std::vector<Variable<T>*> parameters() override { return {}; }
148 };
149
153 template<typename T>
154 class Dropout : public Module<T> {
155 public:
160 explicit Dropout(T p = 0.5) : p_(p) {}
161
162 Variable<T> forward(const Variable<T>& input) override;
163 std::vector<Variable<T>*> parameters() override { return {}; }
164
165 private:
166 T p_; // Dropout probability
167 };
168
172 template<typename T>
173 class Sequential : public Module<T> {
174 public:
178 void add_module(std::shared_ptr<Module<T>> module);
179
183 Variable<T> forward(const Variable<T>& input) override;
184
188 std::vector<Variable<T>*> parameters() override;
189
193 void zero_grad() override;
194
198 void train(bool training = true) override;
199
200 private:
201 std::vector<std::shared_ptr<Module<T>>> modules_;
202 };
203
204 // Type aliases for convenience
217
218} // namespace dl::layers
PyTorch-like automatic differentiation engine.
Dropout layer for regularization.
Definition layers.hpp:154
Dropout(T p=0.5)
Constructor.
Definition layers.hpp:160
std::vector< Variable< T > * > parameters() override
Get all parameters of this module.
Definition layers.hpp:163
Variable< T > forward(const Variable< T > &input) override
Forward pass through the module.
Definition layers.cpp:93
Linear (fully connected) layer: y = xW^T + b.
Definition layers.hpp:82
const Variable< T > & bias() const
Definition layers.hpp:108
const Variable< T > & weight() const
Definition layers.hpp:107
Variable< T > & weight()
Definition layers.hpp:105
Variable< T > forward(const Variable< T > &input) override
Forward pass: y = xW^T + b.
Definition layers.cpp:38
Variable< T > & bias()
Definition layers.hpp:106
std::vector< Variable< T > * > parameters() override
Get parameters (weight and bias)
Definition layers.cpp:54
Base class for all neural network modules (PyTorch-like nn.Module)
Definition layers.hpp:28
virtual void eval()
Set evaluation mode.
Definition layers.hpp:65
virtual void train(bool training=true)
Set training mode.
Definition layers.hpp:58
bool is_training() const
Check if module is in training mode.
Definition layers.hpp:72
virtual ~Module()=default
virtual void zero_grad()
Zero gradients of all parameters.
Definition layers.hpp:48
virtual Variable< T > forward(const Variable< T > &input)=0
Forward pass through the module.
virtual std::vector< Variable< T > * > parameters()=0
Get all parameters of this module.
ReLU activation function.
Definition layers.hpp:124
Variable< T > forward(const Variable< T > &input) override
Forward pass through the module.
Definition layers.cpp:68
std::vector< Variable< T > * > parameters() override
Get all parameters of this module.
Definition layers.hpp:127
Sequential container for chaining modules.
Definition layers.hpp:173
Variable< T > forward(const Variable< T > &input) override
Forward pass through all modules in sequence.
Definition layers.cpp:121
void add_module(std::shared_ptr< Module< T > > module)
Add a module to the sequence.
Definition layers.cpp:116
std::vector< Variable< T > * > parameters() override
Get parameters from all modules.
Definition layers.cpp:133
void train(bool training=true) override
Set training mode for all modules.
Definition layers.cpp:150
void zero_grad() override
Zero gradients for all modules.
Definition layers.cpp:143
Sigmoid activation function.
Definition layers.hpp:134
Variable< T > forward(const Variable< T > &input) override
Forward pass through the module.
Definition layers.cpp:75
std::vector< Variable< T > * > parameters() override
Get all parameters of this module.
Definition layers.hpp:137
Tanh activation function.
Definition layers.hpp:144
Variable< T > forward(const Variable< T > &input) override
Forward pass through the module.
Definition layers.cpp:82
std::vector< Variable< T > * > parameters() override
Get all parameters of this module.
Definition layers.hpp:147
Variable class that supports automatic differentiation.
Definition autograd.hpp:58
Matrix utility class for deep learning operations.