Deep Learning Algorithm Implementations 1.0.0
C++ implementations of fundamental deep learning algorithms
Loading...
Searching...
No Matches
layers.cpp
Go to the documentation of this file.
2#include <stdexcept>
3#include <random>
4#include <algorithm>
5
6namespace dl::layers {
7
8 // ============================================================================
9 // Linear Layer Implementation
10 // ============================================================================
11
12 template<typename T>
13 Linear<T>::Linear(size_t in_features, size_t out_features, bool bias)
14 : in_features_(in_features), out_features_(out_features), has_bias_(bias),
15 weight_(Matrix<T>(out_features, in_features), true),
16 bias_(Matrix<T>(out_features, 1), true) {
17 initialize_parameters();
18 }
19
20 template<typename T>
22 // TODO: Implement Xavier/He initialization
23 // Hint: Use normal distribution with appropriate variance
24 // Xavier: std = sqrt(2.0 / (in_features + out_features))
25 // He: std = sqrt(2.0 / in_features)
26
27 // Placeholder: Initialize with small random values
28 std::random_device rd;
29 std::mt19937 gen(rd());
30 T std_dev = std::sqrt(2.0 / (in_features_ + out_features_));
31 std::normal_distribution<T> dist(0.0, std_dev);
32
33 // TODO: Fill weight_ matrix with random values
34 // TODO: Initialize bias_ to zeros if has_bias_ is true
35 }
36
37 template<typename T>
39 // TODO: Implement forward pass: y = xW^T + b
40 // Steps:
41 // 1. Compute input.dot(weight_.transpose())
42 // 2. Add bias if has_bias_ is true
43 // 3. Return result
44
45 // Placeholder implementation
46 Variable<T> output = input.dot(weight_.transpose());
47 if (has_bias_) {
48 output = output + bias_;
49 }
50 return output;
51 }
52
53 template<typename T>
54 std::vector<Variable<T>*> Linear<T>::parameters() {
55 std::vector<Variable<T>*> params;
56 params.push_back(&weight_);
57 if (has_bias_) {
58 params.push_back(&bias_);
59 }
60 return params;
61 }
62
63 // ============================================================================
64 // Activation Functions Implementation
65 // ============================================================================
66
67 template<typename T>
69 // TODO: Implement ReLU activation
70 // Hint: Use input.relu() method from autograd
71 return input.relu();
72 }
73
74 template<typename T>
76 // TODO: Implement Sigmoid activation
77 // Hint: Use input.sigmoid() method from autograd
78 return input.sigmoid();
79 }
80
81 template<typename T>
83 // TODO: Implement Tanh activation
84 // Hint: Use input.tanh() method from autograd
85 return input.tanh();
86 }
87
88 // ============================================================================
89 // Dropout Implementation
90 // ============================================================================
91
92 template<typename T>
94 // TODO: Implement dropout
95 // During training:
96 // - Generate random mask with probability p_
97 // - Multiply input by mask
98 // - Scale by 1/(1-p_) to maintain expected value
99 // During evaluation:
100 // - Return input unchanged
101
102 if (!this->is_training()) {
103 return input;
104 }
105
106 // TODO: Implement training mode dropout
107 // Placeholder: return input unchanged
108 return input;
109 }
110
111 // ============================================================================
112 // Sequential Container Implementation
113 // ============================================================================
114
115 template<typename T>
116 void Sequential<T>::add_module(std::shared_ptr<Module<T>> module) {
117 modules_.push_back(module);
118 }
119
120 template<typename T>
122 // TODO: Implement sequential forward pass
123 // Apply each module in sequence to the input
124
125 Variable<T> output = input;
126 for (auto& module : modules_) {
127 output = module->forward(output);
128 }
129 return output;
130 }
131
132 template<typename T>
133 std::vector<Variable<T>*> Sequential<T>::parameters() {
134 std::vector<Variable<T>*> all_params;
135 for (auto& module : modules_) {
136 auto module_params = module->parameters();
137 all_params.insert(all_params.end(), module_params.begin(), module_params.end());
138 }
139 return all_params;
140 }
141
142 template<typename T>
144 for (auto& module : modules_) {
145 module->zero_grad();
146 }
147 }
148
149 template<typename T>
150 void Sequential<T>::train(bool training) {
151 Module<T>::train(training);
152 for (auto& module : modules_) {
153 module->train(training);
154 }
155 }
156
157 // ============================================================================
158 // Explicit Template Instantiations
159 // ============================================================================
160
161 template class Linear<float>;
162 template class Linear<double>;
163 template class ReLU<float>;
164 template class ReLU<double>;
165 template class Sigmoid<float>;
166 template class Sigmoid<double>;
167 template class Tanh<float>;
168 template class Tanh<double>;
169 template class Dropout<float>;
170 template class Dropout<double>;
171 template class Sequential<float>;
172 template class Sequential<double>;
173
174} // namespace dl::layers
Dropout layer for regularization.
Definition layers.hpp:154
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
Linear(size_t in_features, size_t out_features, bool bias=true)
Constructor.
Definition layers.cpp:13
Variable< T > forward(const Variable< T > &input) override
Forward pass: y = xW^T + b.
Definition layers.cpp:38
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 train(bool training=true)
Set training mode.
Definition layers.hpp:58
ReLU activation function.
Definition layers.hpp:124
Variable< T > forward(const Variable< T > &input) override
Forward pass through the module.
Definition layers.cpp:68
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
Tanh activation function.
Definition layers.hpp:144
Variable< T > forward(const Variable< T > &input) override
Forward pass through the module.
Definition layers.cpp:82
Variable class that supports automatic differentiation.
Definition autograd.hpp:58
Variable< T > sigmoid() const
Definition autograd.cpp:113
Variable< T > dot(const Variable< T > &other) const
Definition autograd.cpp:69
Variable< T > tanh() const
Definition autograd.cpp:124
Variable< T > relu() const
Definition autograd.cpp:136
PyTorch-like neural network layers with automatic differentiation.