-
Notifications
You must be signed in to change notification settings - Fork 1
/
layer.cpp
70 lines (61 loc) · 1.81 KB
/
layer.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
#include "layer.h"
Layer::Layer(const LayerConfig& layerConfig) {
if (layerConfig.activation == Activation::Sigmoid) {
W = xavierMatrix(layerConfig.rows, layerConfig.cols, true);
} else if (layerConfig.activation == Activation::Tanh) {
W = xavierMatrix(layerConfig.rows, layerConfig.cols, false);
} else {
int r = layerConfig.rows;
int c = layerConfig.cols;
// http://cs231n.github.io/neural-networks-2/
W = Eigen::MatrixXf::Random(r, c) * sqrt(2.0f/(r + c));
}
W_change = Eigen::MatrixXf::Zero(W.rows(), W.cols());
B = Eigen::VectorXf::Ones(W.cols());
B_change = 0.0f;
switch (layerConfig.activation) {
case Activation::Sigmoid:
activation = sigmoid;
dactivation = dsigmoid;
break;
case Activation::Tanh:
activation = _tanh;
dactivation = _dtanh;
break;
case Activation::ReLU:
activation = relu;
dactivation = drelu;
break;
case Activation::Softmax:
activation = softmax;
dactivation = dsoftmax;
break;
default:
break;
};
}
Layer::~Layer() {
}
void Layer::feedforward(bool testing) {
Eigen::MatrixXf z1 = (X * W).rowwise() + B.transpose();
Y = activation(z1);
}
void Layer::backpropagate() {
m_gradient = D.cwiseProduct(dactivation(Y));
DW = X.transpose() * m_gradient;
DY = m_gradient * W.transpose();
}
void Layer::update(float momentum, float learning_rate) {
Eigen::MatrixXf w_change = W_change * momentum + DW * learning_rate;
float b_change = B_change * momentum + m_gradient.mean() * learning_rate;
W -= w_change;
W_change = w_change;
B.array() -= b_change;
B_change = b_change;
}
void Layer::preEpoch(const int epoch) {
// do nothing
}
void Layer::report() {
// do nothing
}