-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDenseLayer.cpp
More file actions
48 lines (35 loc) · 1.13 KB
/
DenseLayer.cpp
File metadata and controls
48 lines (35 loc) · 1.13 KB
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
#include "DenseLayer.h"
DenseLayer::DenseLayer(int input, int output):input_size{input}, output_size{output},weights{Matrix(output_size, input_size)}, dw{Matrix(output_size, input_size)},
input{Matrix()}, output{Matrix()}, biases(Matrix(output_size, 1)), db(Matrix(output_size, 1)) {
weights.random();
dw.zeroes();
biases.zeroes();
db.zeroes();
}
Matrix DenseLayer::forward(const Matrix &input_layer) {
input=input_layer;
output = dot(weights, input) + biases;
return output;
}
Matrix DenseLayer::backward(const Matrix &dz) {
Matrix in_T = input.transpose();
dw = dw + dot(dz, in_T); // accumulate gradients
db = db + dz;
return dot(weights.transpose(), dz); // return gradient to pass backward
}
void DenseLayer::apply(float batch_size, float lr) {
dw = dw / batch_size;
db = db / batch_size;
weights = weights - lr * dw;
biases = biases - lr * db;
dw.zeroes();
db.zeroes();
}
void DenseLayer::zero_gradients() {
dw.zeroes();
db.zeroes();
}
void DenseLayer::accumulate_gradients(const DenseLayer& other) {
dw = dw + other.dw;
db = db + other.db;
}