-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbiasLayer.cpp
43 lines (30 loc) · 945 Bytes
/
biasLayer.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
#include "biasLayer.h"
#include "matrixOp.h"
BiasLayer::BiasLayer(int aDimension, dtype aLearningRate) {
mLayerType = BIAS_LAYER;
mDimenstion = aDimension;
mLearningRate = aLearningRate;
mBias = Matrix(aDimension, 1);
random(&mBias, -0.5, 0.5);
}
Matrix* BiasLayer::forwardPropagation(Matrix* aInput) {
mInput = *aInput;
Matrix::changeCudaStream();
mOutput = mBias.repeat(1, aInput->col());
mOutput += (*aInput); // Calculate output
return &mOutput;
}
Matrix* BiasLayer::backwardPropagation(Matrix* aGradient) {
gradient = (*aGradient);
Matrix::changeCudaStream();
if (mCumulatedGradient.col() != aGradient->col())
mCumulatedGradient.reshape(mBias.row(), aGradient->col());
mCumulatedGradient += (*aGradient);
return &gradient;
}
void BiasLayer::updateWeights() {
mCumulatedGradient *= mLearningRate;
mCumulatedGradient /= mCumulatedGradient.col();
mBias -= mCumulatedGradient.colSum();
mCumulatedGradient(0);
}