forked from rwth-i6/returnn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CTC.cpp
165 lines (153 loc) · 4.6 KB
/
CTC.cpp
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
class CTC
{
public:
void forwardBackward(CSArrayF& activs, CSArrayI& labellings,
int seqLen, float& err, SArrayF& errSigs, SArrayF& priors)
{
nLabelsInclBlank_ = activs.dim(1);
blankIdx_ = nLabelsInclBlank_ - 1;
T_ = seqLen;
N_ = calcLen(labellings);
M_ = 2 * N_ + 1;
canonical_ = calcCanonical(labellings, N_);
errorSignal(activs, labellings, err, errSigs, priors);
}
private:
void forwardAlgo(CSArrayF& activs, CSArrayI& labellings)
{
fwdTable_.resize(T_, M_);
fwdTable_(0,0) = activs(0, nLabelsInclBlank_ - 1);
fwdTable_(0,1) = activs(0, labellings(0));
for(int t = 1; t < T_; ++t)
{
for(int n = 0; n < M_; ++n)
{
myLog y(activs(t, canonical_[n]));
int start = 0;
if(n == 0 || n == 1)
{
start = 0;
}
else if(canonical_[n] == blankIdx_ || canonical_[n] == canonical_[n-2])
{
start = n-1;
}
else
{
start = n-2;
}
myLog sum = 0;
for(int i = start; i <= n; ++i)
{
sum += fwdTable_(t-1,i);
}
fwdTable_(t,n) = y * sum;
}
}
}
void backwardAlgo(CSArrayF& activs, CSArrayI& labellings)
{
bwdTable_.resize(T_, M_);
bwdTable_(T_-1,M_-1) = 1;
bwdTable_(T_-1,M_-2) = 1;
for(int t = T_-2; t != -1; --t)
{
for(int n = 0; n < M_; ++n)
{
int end = 0;
if(n == M_-1 || n == M_-2)
{
end = M_-1;
}
else if(canonical_[n] == blankIdx_
|| canonical_[n] == canonical_[n+2])
{
end = n+1;
}
else
{
end = n+2;
}
myLog sum = 0;
for(int i = n; i <= end; ++i)
{
myLog y = activs(t+1, canonical_[i]);
sum += bwdTable_(t+1,i) * y;
}
bwdTable_(t,n) = sum;
}
}
}
void errorSignal(CSArrayF& activs, CSArrayI& labellings, float& err, SArrayF& errSigs, SArrayF& priors)
{
forwardAlgo(activs, labellings);
backwardAlgo(activs, labellings);
myLog totalSum = 0;
for(int t = 0; t < T_; ++t)
{
std::vector<myLog> labelSum(nLabelsInclBlank_);
totalSum = 0;
for(int n = 0; n < M_; ++n)
{
myLog prod = fwdTable_(t,n) * bwdTable_(t,n);
labelSum[canonical_[n]] += prod;
totalSum += prod;
}
for(int c = 0; c < nLabelsInclBlank_; ++c)
{
errSigs(t, c) = activs(t, c) - (labelSum[c] / totalSum).expVal();
priors(c) += activs(t, c) - errSigs(t,c);
}
}
err = -totalSum.logVal();
if(err > 1e10)
{
static bool printedWarning = false;
if(!printedWarning)
{
std::cout << "Warning, CTC error of " << err << ", probably output sequence is too short. This warning is only printed once, even if the problem occurs multiple times." << std::endl;
printedWarning = true;
}
err = 0;
for(int t = 0; t < T_; ++t)
{
for(int c = 0; c < nLabelsInclBlank_; ++c)
{
errSigs(t, c) = 0;
priors(c) = 0;
}
}
}
}
int calcLen(CSArrayI& labellings)
{
int len = labellings.dim(0);
for(int j = 0; j < len; ++j)
{
if(labellings(j) == -1)
{
return j;
}
}
return len;
}
std::vector<int> calcCanonical(CSArrayI& labellings, int len)
{
std::vector<int> canonical;
canonical.push_back(blankIdx_);
for(int i = 0; i < len; ++i)
{
canonical.push_back(labellings(i));
canonical.push_back(blankIdx_);
}
return canonical;
}
int T_;
int N_;
int M_;
int nLabelsInclBlank_;
int blankIdx_;
std::vector<int> canonical_;
TwoDArray<myLog> fwdTable_;
TwoDArray<myLog> bwdTable_;
};