-
Notifications
You must be signed in to change notification settings - Fork 29
/
Windowlized.h
158 lines (133 loc) · 4.26 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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#ifndef WINDOWLIZED
#define WINDOWLIZED
#include "tensor.h"
#include "MyLib.h"
using namespace std;
using namespace mshadow;
using namespace mshadow::expr;
using namespace mshadow::utils;
template<typename xpu>
inline void windowlized(const vector<Tensor<xpu, 2, dtype> > &wi, vector<Tensor<xpu, 2, dtype> > &wo, int context)
{
int seqsize = wo.size();
if (wi.size() != seqsize || seqsize == 0 || context < 0) {
std::cerr << "windowlized error: vector size or context size invalid" << std::endl;
}
int dim1 = wi[0].size(0), dim2 = wi[0].size(1);
int odim1 = wo[0].size(0), odim2 = wo[0].size(1);
int computeddim2 = (2 * context + 1) * dim2;
if(computeddim2 != odim2 || dim1 != 1 || odim1 != 1){
std::cerr << "windowlized error: dim size invalid" << std::endl;
}
static int offset;
for (int idx = 0; idx < seqsize; idx++) {
wo[idx] = 0.0;
offset = 0;
for (int idp = idx - context; idp <= idx + context; idp++) {
if (idp < 0 || idp >= seqsize) {
offset += dim2;
} else {
for (int idy = 0; idy < dim2; idy++) {
wo[idx][0][offset] = wi[idp][0][idy];
offset++;
}
}
}
assert(offset == odim2);
}
}
template<typename xpu>
inline void windowlized(Tensor<xpu, 3, dtype> wi, Tensor<xpu, 3, dtype> wo, int context)
{
int seqsize = wo.size(0);
if (wi.size(0) != seqsize || seqsize == 0 || context < 0) {
std::cerr << "windowlized error: vector size or context size invalid" << std::endl;
}
int dim1 = wi.size(1), dim2 = wi.size(2);
int odim1 = wo.size(1), odim2 = wo.size(2);
int computeddim2 = (2 * context + 1) * dim2;
if(computeddim2 != odim2 || dim1 != 1 || odim1 != 1){
std::cerr << "windowlized error: dim size invalid" << std::endl;
}
wo = 0.0;
static int offset;
for (int idx = 0; idx < seqsize; idx++) {
offset = 0;
for (int idp = idx - context; idp <= idx + context; idp++) {
if (idp < 0 || idp >= seqsize) {
offset += dim2;
} else {
for (int idy = 0; idy < dim2; idy++) {
wo[idx][0][offset] = wi[idp][0][idy];
offset++;
}
}
}
assert(offset == odim2);
}
}
template<typename xpu>
inline void windowlized_backward(vector<Tensor<xpu, 2, dtype> > &lwi, const vector<Tensor<xpu, 2, dtype> > &lwo, int context, bool bclear = false)
{
int seqsize = lwo.size();
if (lwi.size() != seqsize || seqsize == 0 || context < 0) {
std::cerr << "windowlized error: vector size or context size invalid" << std::endl;
}
int dim1 = lwi[0].size(0), dim2 = lwi[0].size(1);
int odim1 = lwo[0].size(0), odim2 = lwo[0].size(1);
int computeddim2 = (2 * context + 1) * dim2;
if(computeddim2 != odim2 || dim1 != 1 || odim1 != 1){
std::cerr << "windowlized error: dim size invalid" << std::endl;
}
if(bclear){
for (int idx = 0; idx < seqsize; idx++) {
lwi[idx] = 0.0;
}
}
static int offset;
for (int idx = 0; idx < seqsize; idx++) {
offset = 0;
for (int idp = idx - context; idp <= idx + context; idp++) {
if (idp < 0 || idp >= seqsize) {
offset += dim2;
} else {
for (int idy = 0; idy < dim2; idy++) {
lwi[idp][0][idy] += lwo[idx][0][offset];
offset++;
}
}
}
assert(offset == odim2);
}
}
template<typename xpu>
inline void windowlized_backward(Tensor<xpu, 3, dtype> lwi, Tensor<xpu, 3, dtype> lwo, int context, bool bclear = false)
{
int seqsize = lwo.size(0);
if (lwi.size(0) != seqsize || seqsize == 0 || context < 0) {
std::cerr << "windowlized error: vector size or context size invalid" << std::endl;
}
int dim1 = lwi.size(1), dim2 = lwi.size(2);
int odim1 = lwo.size(1), odim2 = lwo.size(2);
int computeddim2 = (2 * context + 1) * dim2;
if(computeddim2 != odim2 || dim1 != 1 || odim1 != 1){
std::cerr << "windowlized error: dim size invalid" << std::endl;
}
if(bclear) lwi = 0.0;
static int offset;
for (int idx = 0; idx < seqsize; idx++) {
offset = 0;
for (int idp = idx - context; idp <= idx + context; idp++) {
if (idp < 0 || idp >= seqsize) {
offset += dim2;
} else {
for (int idy = 0; idy < dim2; idy++) {
lwi[idp][0][idy] += lwo[idx][0][offset];
offset++;
}
}
}
assert(offset == odim2);
}
}
#endif