-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReLULayer.cpp
More file actions
61 lines (56 loc) · 1.69 KB
/
Copy pathReLULayer.cpp
File metadata and controls
61 lines (56 loc) · 1.69 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
51
52
53
54
55
56
57
58
59
60
61
#include "DeepLearning.h"
ReLULayer::ReLULayer(int h, int w){
height = h;
width = w;
for(int i = 0;i < batchSize;i++){
Array tmp(height,width);
output.push_back(tmp);
error.push_back(tmp);
}
}
double ReLULayer::ReLU(double x){
return (x<0 ? leaky*x : x);
}
double ReLULayer::ReLUDer(double x){
return (x<0 ? leaky : 1);
}
void ReLULayer::forward(const vector<Array>& inp){
for(int n = 0;n < batchSize;n++){
if(inp[n].width != width || inp[n].height != height){
printf("Error : Unable to compute ReLU Forward\n");
exit(0);
}
for(int i = 0;i < height;i++){
for(int j = 0;j < width;j++){
output[n].arr[i][j] = ReLU(inp[n].arr[i][j]);
}
}
if(!output[n].CheckFinite()){
fprintf(fpDebug, "In ReLuLayer: Array called output\n");
fflush(fpDebug);
exit(0);
}
}
return;
}
void ReLULayer::backward(const vector<Array>& err){
for(int n = 0;n < batchSize;n++){
if(err[n].width != width || err[n].height != height){
printf("Error : Unable to compute ReLU backward\n");
exit(0);
}
for(int i = 0;i < height;i++){
for(int j = 0;j < width;j++){
error[n].arr[i][j] = ReLUDer(output[n].arr[i][j]);
}
}
if(!error[n].CheckFinite()){
fprintf(fpDebug, "In ReLuLayer: Array called error\n");
fflush(fpDebug);
exit(0);
}
error[n] = DotProduct(error[n],err[n]);
error[n].Bound(minStep,maxStep);
}
return;
}