-
Notifications
You must be signed in to change notification settings - Fork 1
/
denselayer.cpp
301 lines (283 loc) · 7.81 KB
/
denselayer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#include "denselayer.hpp"
/**
* @brief Construct a new DenseLayer::DenseLayer object
*
* @details sets the number of nodes and weights in the new dens-layer.
*
* @param[in] num_nodes
* @param[in] num_weights
*/
DenseLayer::DenseLayer(const std::size_t num_nodes,
const std::size_t num_weights)
{
this->resize(num_nodes, num_weights);
}
/**
* @brief Destructor erases all values in each nodes vector.
*
*/
DenseLayer::~DenseLayer()
{
this->clear();
}
/**
* @brief returns the number of nodes for selected dense layer.
*
* @return number of nodes.
*/
std::size_t DenseLayer::num_nodes(void) const
{
return this->output.size();
}
/**
* @brief returns the number of weights per node for selected dense-layer.
*
* @return number of weights.
*/
std::size_t DenseLayer::num_weights(void) const
{
if (this->weights.size() == 0)
{
return 0;
}
else
{
return this->weights[0].size();
}
}
/**
* @brief sets the desired activation function for selected dense layer.
*
* @param[in] af RELU or TANH
*/
void DenseLayer::set_activation(const activation_option ao)
{
this->ao = ao;
}
/**
* @brief Erases all the elements in the vector containers for selected dense layer
*
*/
void DenseLayer::clear(void)
{
this->output.clear();
this->error.clear();
this->bias.clear();
this->weights.clear();
}
/**
* @brief sets the size of all the elements in the vector containers for selected dense layer
*
* @param[in] num_nodes number of nodes
* @param[in] num_weights number of weights per node
*/
void DenseLayer::resize(const std::size_t num_nodes,
const std::size_t num_weights)
{
this->output.resize(num_nodes, 0.0);
this->error.resize(num_nodes, 0.0);
this->bias.resize(num_nodes, 0.0);
this->weights.resize(num_nodes, std::vector<double>(num_weights, 0.0));
for (std::size_t i = 0; i < num_nodes; ++i)
{
this->bias[i] = this->get_random();
for (std::size_t j = 0; j < num_weights; ++j)
{
this->weights[i][j] = this->get_random();
}
}
}
/**
* @brief calculates new output for each node in selected dense-layer
*
* @details output[i] = (bias + (input * weight)) tanh
* | outside | layer |
* [input0] - [weight 0 0] [ ]
* [input1] - [weight 0 1] [ node 0 ]
* [input2] - [weight 0 2] [ ]
* @param[in] input indata from training data or previous layer
*/
void DenseLayer::feedforward(const std::vector<double> &input)
{
for (std::size_t i = 0; i < this->num_nodes(); i++)
{
double sum = bias[i];
for (std::size_t j = 0; j < this->num_weights() && j < input.size(); j++)
{
sum += input[j] * this->weights[i][j];
}
this->output[i] = this->activation(sum);
}
}
/**
* @brief calculates the error for each node in output layer.
*
* @details
* Backpropagation is going backwards.
* ie. hidden_layer_0 <- output_layer
* |
* you are here
*
*
* | layer | outside the layer |
* [ ]
* [ node 0 ] = [reference or train_yref] - [output or y_predict]
* [ error ]
*
* @param[in] reference target value from training data (yref)
*/
void DenseLayer::backpropagate(const std::vector<double> &reference)
{
for (std::size_t i = 0; i < this->num_nodes(); i++)
{
double dev = reference[i] - this->output[i];
this->error[i] = dev * delta_activation(this->output[i]);
}
}
/**
* @brief calculates the error for each node in a dense layer.
* @details
* Backpropagation is going backwards.
* ie. hidden_layer_0 <- output_layer
* | |
* you are here this is next layer
*
*
* | layer | nextlayer |
* [ ] [weight 0 0] * [node 0 error] +
* [ node 0 ] = [weight 1 0] * [node 1 error] +
* [ error ] [weight 2 0] * [node 2 error] ...
* @param[in] next_layer mext dense layer
*/
void DenseLayer::backpropagate(const DenseLayer &next_layer)
{
for (std::size_t i = 0; i < this->num_nodes(); i++)
{
double dev = 0.0;
{
for (std::size_t j = 0; j < next_layer.num_nodes(); j++)
{
dev += next_layer.error[j] * next_layer.weights[j][i];
}
this->error[i] = dev * this->delta_activation(this->output[i]);
}
}
}
/**
* @brief calculates new bias and new weights for the dense layer
*
* @details
* Gradient descent (optimize)
* m1(new) = m1 + e1 * LR
* k11(new) = k11 + e1 * LR * x1
* k = weight, m = bias
*
* | outside | layer |
* [input0] - [weight 0 0] [ ]
* [input1] - [weight 0 1] [ node 0 ]
* [input2] - [weight 0 2] [ ]
* @param[in] input in-data from training data or previous layer
* @param[in] learning_rate amount of error adjustment
*/
void DenseLayer::optimize(const std::vector<double> &input,
const double learning_rate)
{
for (std::size_t i = 0; i < this->num_nodes(); i++)
{
this->bias[i] += this->error[i] * learning_rate;
for (std::size_t j = 0; j < this->num_weights() && j < input.size(); j++)
{
this->weights[i][j] += this->error[i] * learning_rate * input[j];
}
}
}
/**
* @brief returns a value beteween 0 and 1
*
* @return double
*/
inline double DenseLayer::get_random(void)
{
return (double)(std::rand()) / RAND_MAX;
}
/**
* @brief function to choose from ReLU or Tanh in feedforward
*
* @details option "RELU" is a linear function that returns
* sum if sum is over 0.
* option "TANH" is a non-linear function that returns
* a value beween -1 and 1. lower values will be mapped closer
* to 0 while "higher" values will be mapped closer to 1 or -1.
*
* @param[in] sum
* @return sum
*/
inline double DenseLayer::activation(const double sum)
{
if (this->ao == activation_option::TANH)
{
return tanh(sum);
}
else
{
return sum > 0.0 ? sum : 0.0;
}
}
/**
* @brief function to choose from ReLU or Tanh in backproagation.
*
* @details option "RELU" returns 1 if output is higher than 0 (node activated)
* and 0 if output is 0 (node not activated)
* Option "TANH" returns a value between 0 and 1, so error value will
* be smaller compared to using ReLU in backpropagation.
* @param[in] output
* @return output
*/
inline double DenseLayer::delta_activation(const double output)
{
if (this->ao == activation_option::TANH)
{
return 1 - tanh(output) * tanh(output);
}
else
{
return output > 0.0 ? 1.0 : 0.0;
}
}
/**
* @brief prints information about the dense layer
* @details
* LITE FULL
* no of weights per node x x
* no of nodes x x
* activation mode x x
* weight and bias data x
*
* @param[in] po chose print option FULL or LITE
* @param[in] ostream chosen output stream
*/
void DenseLayer::print(print_option po, std::ostream &ostream)
{
ostream << this->num_weights() << " weights per node.\n";
ostream << this->num_nodes() << " nodes.\n";
if (this->ao == activation_option::TANH)
{
ostream << "Activation: TANH \n";
}
if (this->ao == activation_option::RELU)
{
ostream << "Activation: RELU \n";
}
if (po == print_option::FULL)
{
for (std::size_t i = 0; i < this->num_nodes(); i++)
{
ostream << "Node: [" << i << "] bias: " << bias[i] << " weights: ";
for (std::size_t j = 0; j < this->num_weights(); j++)
{
ostream << "[" << j << "] " << weights[i][j] << " , ";
}
ostream << "\n";
}
}
}