forked from sshekh/conv-filters
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter.hpp
More file actions
130 lines (104 loc) · 2.82 KB
/
filter.hpp
File metadata and controls
130 lines (104 loc) · 2.82 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#ifndef CONV_FILTER_H
#define CONV_FILTER_H
#include <cmath>
#include <memory>
#include <execution>
#include <string>
#include "NDGrid.hpp"
/**
* v w(x);
*/
using v = std::vector<double>;
/**
* matrix w(x, v(y));
*/
using matrix = std::vector<std::vector<double>>;
/**
* tensor w(x, matrix(y,v(z)));
*/
using tensor = std::vector<std::vector<std::vector<double>>>;
template <typename F, typename T>
T for_matrix(F f, T m) {
for_each(std::execution::par, m.begin(), m.end(), f);
return m;
}
template <typename F, typename T>
T trans_matrix(F f, T &m) {
transform(std::execution::par, m.begin(), m.end(),
m.begin(), [f](auto v) -> auto {
return f(v);
});
return m;
}
template <typename F, typename T>
T for_tensor(F f, T t) {
for_each(std::execution::par,
t.begin(), t.end(),
[f](auto m) {
for_matrix(f, m);
});
return t;
}
/**
* Tensor reference value.
*/
template <typename F, typename T>
T trans_tensor(F f, T &t) {
transform(std::execution::par,
t.begin(), t.end(),
t.begin(), [f](auto m) -> auto {
return trans_matrix(f, m);
});
return t;
}
template <typename T>
auto sum(T t) {
return reduce(std::execution::par, t.begin(), t.end());
}
// allocate memory for a tensor
tensor get_tensor(int x, int y, int z) {
tensor w(x, matrix(y, v(z)));
return w;
}
class filter {
public:
tensor w;
double b; // krenel matrix, bias term
int window, depth;
static const size_t kDefaultSize = 3;
filter()
: window(kDefaultSize), depth(kDefaultSize) {
tensor w(window, matrix(window, v(depth)));
}
filter(int size)
: window(size), depth(size) {
tensor w(window, matrix(window, v(depth)));
}
filter(int _window, int _depth)
: window(_window), depth(_depth) {
tensor w(window, matrix(window, v(depth)));
}
filter(tensor _w, int _window, int _depth, int _b = 0)
: w(_w), window(_window), depth(_depth), b(_b) {
}
virtual ~filter() = default;
// normalize the tensor
void normalize() {
double sum = 0;
for_each(std::execution::par_unseq, w.begin(), w.end(), [&sum](auto m){
for_each(std::execution::par_unseq, m.begin(), m.end(), [&sum](auto v){
for_each(std::execution::par_unseq, v.begin(), v.end(), [&sum](auto v){
sum += std::abs(v);
});
});
});
for_each(std::execution::par_unseq, w.begin(), w.end(), [sum](auto m){
for_each(std::execution::par_unseq, m.begin(), m.end(), [sum](auto v){
for_each(std::execution::par_unseq, v.begin(), v.end(), [sum](auto v){
v /= sum;
});
});
});
}
};
#endif