-
Notifications
You must be signed in to change notification settings - Fork 0
/
relu.hpp
42 lines (38 loc) · 1.12 KB
/
relu.hpp
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
#ifndef _RELU_HPP_
#define _RELU_HPP_
//pooling_affine之后的relu层
#include "matrix.hpp"
class Relu{
public:
Matrix<double> *mask = nullptr;
Matrix<double> *forward(Matrix<double> *x){
if(this->mask != nullptr){
delete(this->mask);
}
auto out = x->Copy();
this->mask = x->Copy();
for (int i = 0; i < this->mask->height; ++i) {
for (int j = 0; j < this->mask->width; ++j) {
if(this->mask->Get(i, j) <= 0){
this->mask->Set(i, j, 1);
out->Set(i, j, 0);
} else {
this->mask->Set(i, j, 0);
}
}
}
return out;
}
Matrix<double> * backward(Matrix<double> *dout){
auto temp = dout->Copy();
for (int i = 0; i < this->mask->height; ++i) {
for (int j = 0; j < this->mask->width; ++j) {
if(this->mask->Get(i, j) == 1){
temp->Set(i, j, 0);
}
}
}
return temp;
}
};
#endif //_RELU_HPP_