-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfcLayer.cpp
More file actions
46 lines (35 loc) · 1.15 KB
/
fcLayer.cpp
File metadata and controls
46 lines (35 loc) · 1.15 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
#include "fcLayer.h"
#include "matrixOp.h"
#include "srcMeasure.h"
FcLayer::FcLayer(int aInputDimension, int aOutputDimension, dtype aLearningRate) {
mLayerType = FC_LAYER;
mInputDim = aInputDimension;
mOutputDim = aOutputDimension;
mLearningRate = aLearningRate;
mWeight = Matrix(mOutputDim, mInputDim);
random(&mWeight, -0.5, 0.5);
mCumulatedGradient = Matrix(mWeight.row(), mWeight.col());
temp = Matrix(mOutputDim, mInputDim);
}
Matrix* FcLayer::forwardPropagation(Matrix* aInput) {
mInput = *aInput;
if (gradient.col() != aInput->col())
gradient.reshape(mInputDim, aInput->col());
if (mOutput.col() != aInput->col())
mOutput.reshape(mOutputDim, aInput->col());
mOutput.matMult(&mWeight, aInput, &mOutput);
return &mOutput;
}
Matrix* FcLayer::backwardPropagation(Matrix* aGradient) {
gradient.matMult(&mWeight, aGradient, &gradient, true, false);
Matrix::changeCudaStream();
temp.matMult(aGradient, &mInput, &temp, false, true);
mCumulatedGradient += temp;
return &gradient;
}
void FcLayer::updateWeights() {
mCumulatedGradient *= mLearningRate;
mCumulatedGradient /= mOutput.col();
mWeight -= mCumulatedGradient;
mCumulatedGradient(0);
}