-
Notifications
You must be signed in to change notification settings - Fork 1
/
ctc.hpp
293 lines (271 loc) · 8.96 KB
/
ctc.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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
Original Author:
Alex Graves (2009-2010)
Modified and refactored by
Amir Ahooye Atashin (2015-2016)
*/
#include <iostream>
#include <vector>
#include <algorithm>
#include "dataset.h"
#include "Log.hpp"
using namespace std;
// ref: http://stackoverflow.com/questions/9694838/how-to-implement-2d-vector-array
#define vector_double vector<double>
#define matrix_double vector<vector_double>
#define LogD Log<double>
#define vector_logd vector<LogD>
#define matrix_logd vector<vector_logd>
const Log<double> logOne1(1);
struct InputDataCTC
{
//data
vector<int> targetLabelSeq;
matrix_logd inputs;
matrix_double outputErrors;
//matrix_double outputErrorsE;
int blank_id;
void LoadDataFrom_HCRF_Toolbox(DataSequence &ds, dMatrix* probabilities)
{
for(int i = 0; i < ds.getStateLabels()->getLength(); ++i)
{
targetLabelSeq.push_back(ds.getStateLabels(i));
}
int row = probabilities->getWidth();
int col = probabilities->getHeight();
blank_id = col - 1;
inputs.resize(row, vector_logd(col, LogD(0)));
outputErrors.resize(row, vector_double(col, 0));
//outputErrorsE.resize(row - 1, vector_double((col - 1) * (col - 1), 0));
for(int i = 0; i < row; ++i)
for (int j = 0; j < col; ++j) {
double p = 0;
//if (j != blank_id)
p = probabilities->getValue(j, i);
//cerr << p << endl;
LogD t(p);
inputs[i][j] = t;
}
}
/*
void LoadDataFrom_RNN_Lib(const DataSequence &ds, SeqBuffer<LogD> actv)
{
int row = actv.seq_size();
int col = actv.depth;
//cerr << row << endl;
inputs.resize(row, vector_logd(col, LogD(0)));
outputErrors.resize(row, vector_double(col, 0));
for(int i = 0; i < row; ++i)
for (int j = 0; j < col; ++j) {
inputs[i][j] = actv[i][j];
}
targetLabelSeq = ds.targetLabelSeq;
}
*/
};
class CTC
{
matrix_logd forwardVariables;
matrix_logd backwardVariables;
int blank;
int totalSegments;
int totalTime;
vector_logd dEdYTerms;
vector<int> outputLabelSeq;
public:
CTC()
{
blank = -1;
}
CTC(int blank_ind)
{
blank = blank_ind;
}
double calculate_errors(InputDataCTC &seq, bool fullEx)
{
if(blank == -1)
blank = seq.blank_id;
totalTime = (int)seq.inputs.size();
int outSize = (int)seq.inputs[0].size();
int len = (int)seq.targetLabelSeq.size();
/*
int requiredTime = len;
int oldLabel = -1;
for (int i = 0; i < len; ++i)
{
if (seq.targetLabelSeq[i] == oldLabel)
{
++requiredTime;
}
oldLabel = seq.targetLabelSeq[i];
}
if (totalTime < requiredTime)
{
std::cerr << "Error: seq data has requiredTime " << requiredTime << " > totalTime " << totalTime << endl;
return -1.0;
}
*/
totalSegments = ((int)seq.targetLabelSeq.size() * 2) + 1;
/* calculate the forward variables*/
forwardVariables.resize(totalTime, vector_logd(totalSegments, LogD(0)));
forwardVariables[0][0] = seq.inputs[0][blank];
//cerr << forwardVariables[0][0] << endl;
if (totalSegments > 1)
{
forwardVariables[0][1] = seq.inputs[0][seq.targetLabelSeq[0]];
//cerr << forwardVariables[0][1] << endl;
}
for (int t = 1; t < totalTime; ++t)
{
vector_logd logActs = seq.inputs[t];
vector_logd oldFvars = forwardVariables[t - 1];
vector_logd fvars = forwardVariables[t];
vector<int> srange = segment_range(t);
int s;
for(int i = 0; i < srange.size(); ++i)
{
s = srange[i];
LogD fv(0);
//if(t == 2) cerr << "S:" << s << endl;
//s odd (label output)
if (s & 1)
{
int labelIndex = s / 2;
int labelNum = seq.targetLabelSeq[labelIndex];
fv += oldFvars[s];
fv += oldFvars[s - 1];
//if(s == 1) cerr << "G:" << fv.log() << endl;
if (s > 1)// && labelNum != seq.targetLabelSeq[labelIndex - 1])
{
fv += oldFvars[s - 2];
}
fv *= (logActs[labelNum] *prior_label_prob(labelIndex));
//if(s == 1) cerr << "G:" << fv.log() << endl;
}
//s even (blank output)
else
{
fv = oldFvars[s];
if (s)
{
fv += oldFvars[s - 1];
}
fv *= logActs[blank];
}
fvars[s] = fv;
}
forwardVariables[t] = fvars;
}
vector_logd lastFvs = forwardVariables[totalTime - 1];
LogD logProb = lastFvs.back();
if (totalSegments > 1)
{
logProb += lastFvs[lastFvs.size()-2];
}
double ctcError = -logProb.log();
if (!fullEx)
return ctcError;
//cerr << "d " << logProb.log() << endl;
//calculate the backward variables
backwardVariables.resize(totalTime, vector_logd(totalSegments, LogD(0)));
vector_logd lastBvs = backwardVariables[totalTime - 1];
lastBvs.back() = LogD(1);
if (totalSegments > 1)
{
lastBvs[lastBvs.size()-2] = LogD(1);
}
backwardVariables[totalTime - 1] = lastBvs;
//LOOP over time, calculating backward variables recursively
for (int t = totalTime - 2; t >= 0; --t)
{
vector_logd oldLogActs = seq.inputs[t + 1];
vector_logd oldBvars = backwardVariables[t + 1];
vector_logd bvars = backwardVariables[t];
vector<int> srange = segment_range(t);
int s;
for(int i = 0; i < srange.size(); ++i)
{
LogD bv(0);
s = srange[i];
//s odd (label output)
if (s & 1)
{
int labelIndex = s / 2;
int labelNum = seq.targetLabelSeq[labelIndex];
bv = (oldBvars[s] * oldLogActs[labelNum] *prior_label_prob(labelIndex)) + (oldBvars[s + 1] * oldLogActs[blank]);
if (s < (totalSegments - 2))
{
int nextLabelNum = seq.targetLabelSeq[labelIndex + 1];
//if (labelNum != nextLabelNum)
{
bv += (oldBvars[s + 2] * oldLogActs[nextLabelNum] *prior_label_prob(labelIndex + 1));
}
}
//if(s == 1) cerr << "B:" << bv.log() << endl;
}
//s even (blank output)
else
{
bv = oldBvars[s] * oldLogActs[blank];
if (s < (totalSegments - 1))
{
bv += (oldBvars[s + 1] * oldLogActs[seq.targetLabelSeq[s / 2]] *prior_label_prob(s / 2));
}
}
bvars[s] = bv;
}
backwardVariables[t] = bvars;
}
for(int time = 0; time < totalTime; ++time)
{
vector_logd fvars = forwardVariables[time];
vector_logd bvars = backwardVariables[time];
dEdYTerms.resize(outSize, LogD(0));
for (int s = 0; s < totalSegments; s++)
{
//k = blank for even s, target label for odd s
int k = (s & 1) ? seq.targetLabelSeq[s / 2] : blank;
dEdYTerms[k] += (fvars[s] * bvars[s]);
}
//if(time == 2) cerr << "safasf" << endl;
for (int j = 0; j < outSize; j++)
{
seq.outputErrors[time][j] = (dEdYTerms[j] / (logProb * seq.inputs[time][j])).exp();
}
/*
vector_logd dEdYTerms_prev;
if (time != 0)
{
for (int j = 0; j < outSize - 1; j++)
{
for (int k = 0; k < outSize - 1; k++)
{
seq.outputErrorsE[time - 1][j* (outSize - 1) + k] = ( (dEdYTerms_prev[j] / seq.inputs[time - 1][j] + dEdYTerms[k]/ seq.inputs[time][k]) / logProb ).exp();
}
}
}
dEdYTerms_prev = dEdYTerms;
*/
dEdYTerms.clear();
}
return ctcError;
}
virtual const LogD& prior_label_prob(int label)
{
return logOne1;
}
vector<int> segment_range(int time, int totalSegs = -1) const
{
if (totalSegs < 0)
{
totalSegs = totalSegments;
}
int start = (int)fmax(0, totalSegs - (2 * (totalTime - time)));
int end = (int)fmin(totalSegs, 2 * (time + 1));
vector<int> range(end - start);
int k = 0;
for (int i = start; i < end; i++)
range[k++] = i;
return range;
}
};