-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathWindowlized.h
92 lines (72 loc) · 1.86 KB
/
Windowlized.h
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
#ifndef WINDOWLIZED
#define WINDOWLIZED
/*
* Windowlized.h:
* a contextual builder, concatenate x[-c]...x[0]...x[c] together
*
* Created on: Apr 22, 2017
* Author: mszhang
*/
#include "MyLib.h"
#include "Node.h"
#include "Concat.h"
#include "Graph.h"
class WindowBuilder {
public:
int _context;
int _window;
int _nSize;
int _inDim;
int _outDim;
vector<ConcatNode> _outputs;
BucketNode _bucket;
public:
WindowBuilder() {
clear();
}
~WindowBuilder() {
clear();
}
inline void resize(int maxsize) {
_outputs.resize(maxsize);
}
inline void clear() {
_outputs.clear();
_context = 0;
_window = 0;
_nSize = 0;
_inDim = 0;
_outDim = 0;
}
inline void init(int inDim, int context) {
_context = context;
_window = 2 * _context + 1;
_inDim = inDim;
_outDim = _window * _inDim;
int maxsize = _outputs.size();
for (int idx = 0; idx < maxsize; idx++) {
_outputs[idx].init(_outDim, -1); // dropout is not supported here
}
_bucket.init(_inDim, -1);
}
public:
inline void forward(Graph *cg, const vector<PNode>& x) {
if (x.size() == 0) {
std::cout << "empty inputs for windowlized operation" << std::endl;
return;
}
_nSize = x.size();
vector<PNode> in_nodes(_window);
_bucket.forward(cg, 0);
for (int idx = 0; idx < _nSize; idx++) {
int offset = 0;
in_nodes[offset++] = x[idx];
for (int j = 1; j <= _context; j++) {
in_nodes[offset++] = idx - j >= 0 ? x[idx - j] : &_bucket;
in_nodes[offset++] = idx + j < _nSize ? x[idx + j] : &_bucket;
}
_outputs[idx].forward(cg, in_nodes);
}
}
};
#endif