-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfilter.hpp
68 lines (62 loc) · 1.48 KB
/
filter.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#ifndef CONV_FILTER_H
#define CONV_FILTER_H
#include <cmath>
// allocate memory for a tensor
double ***get_tensor(int x, int y, int z) {
double ***ret = new double**[x];
for (int i = 0; i < x; i++) {
ret[i] = new double* [y];
for (int j = 0; j < y; ++j) {
ret[i][j] = new double[z];
}
}
return ret;
}
class filter {
public:
double ***w, b; // krenel matrix, bias term
int window, depth;
filter(int _window, int _depth)
: window(_window), depth(_depth) {
w = get_tensor(window, window, depth);
}
filter(double ***_w, int _window, int _depth, int _b = 0)
: window(_window), depth(_depth), b(_b) {
w = get_tensor(window, window, depth);
for (int i = 0; i < window; ++i) {
for (int j = 0; j < window; ++j) {
for (int k = 0; k < depth; ++k) {
w[i][j][k] = _w[i][j][k];
}
}
}
}
~filter() {
for (int i = 0; i < window; ++i) {
for (int j = 0; j < window; ++j) {
delete[] w[i][j];
}
delete[] w[i];
}
delete[] w;
}
// normalize the tensor
void normalize() {
double sum = 0;
for (int i = 0; i < window; ++i) {
for (int j = 0; j < window; ++j) {
for (int k = 0; k < depth; ++k) {
sum += std::abs(w[i][j][k]);
}
}
}
for (int i = 0; i < window; ++i) {
for (int j = 0; j < window; ++j) {
for (int k = 0; k < depth; ++k) {
w[i][j][k] /= sum;
}
}
}
}
};
#endif