-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOutputAct.cpp
More file actions
50 lines (42 loc) · 1.1 KB
/
Copy pathOutputAct.cpp
File metadata and controls
50 lines (42 loc) · 1.1 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
49
50
#include "OutputAct.h"
#include "MathNN.h"
OutputAct::OutputAct(int size, v2d* downstream) : downstream(downstream) {
this->upstream = nullptr;
this->gradient = v2d();
}
void OutputAct::setUpstream(v2d* upstream) {
this->upstream = upstream;
}
void OutputAct::setDownstream(v2d* downstream) {
this->downstream = downstream;
}
v2d* OutputAct::getActivations() {
return &activations;
}
v2d* OutputAct::getGradient() {
return &gradient;
}
void OutputAct::forward() {
if (this->loss == Loss::crossEntropy) {
int width = (*downstream)[0].size();
int height = downstream->size();
activations = v2d(height, v1d(width));
for (unsigned int j = 0; j < width; j++) {
float sum = 0;
for (unsigned int i = 0; i < height; i++) {
sum += exp((*downstream)[i][j]);
}
for (unsigned int i = 0; i < height; i++) {
activations[i][j] = exp((*downstream)[i][j]) / sum;
}
}
} else if (this->loss == Loss::meanSquared) {
activations = *downstream;
}
}
void OutputAct::setLoss(Loss loss) {
this->loss = loss;
}
void OutputAct::backward(v2d* labels) {
gradient = MathNN::MMsub(&activations, labels);
}