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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
#ifndef _CUCRBEDCTFEATURES_H_
#define _CUCRBEDCTFEATURES_H_
#include "cuComponent.h"
#include "cumath.h"
namespace TNet {
/**
* \brief Input features time context expand
*
* \ingroup CuNNMisc
* Expands the time context of the input features according to FrameOffset
*
* in N, out k*N, FrameOffset o_1,o_2,...,o_k
* FrameOffset example 11frames: -5 -4 -3 -2 -1 0 1 2 3 4 5
*/
class CuExpand : public CuComponent
{
public:
CuExpand(size_t nInputs, size_t nOutputs, CuComponent* pPred)
: CuComponent(nInputs,nOutputs,pPred)
{ }
~CuExpand()
{ }
ComponentType GetType() const
{ return EXPAND; }
const char* GetName() const
{ return "<expand>"; }
void ReadFromStream(std::istream& rIn)
{ Vector<int> vec; rIn >> vec; mFrameOffset.CopyFrom(vec); }
void WriteToStream(std::ostream& rOut)
{ Vector<int> vec; mFrameOffset.CopyTo(vec); rOut << vec; }
protected:
void PropagateFnc(const CuMatrix<BaseFloat>& X, CuMatrix<BaseFloat>& Y)
{ CuMath<BaseFloat>::Expand(Y,X,mFrameOffset); }
void BackpropagateFnc(const CuMatrix<BaseFloat>& X, CuMatrix<BaseFloat>& Y)
{ Error("__func__ Nonsense"); }
protected:
CuVector<int> mFrameOffset;
};
/**
* \brief Columns' value shuffling
*
* \ingroup CuNNMisc
* Rearrange the matrix columns according to the indices in CopyFromIndices
*/
class CuCopy : public CuComponent
{
public:
CuCopy(size_t nInputs, size_t nOutputs, CuComponent* pPred)
: CuComponent(nInputs,nOutputs,pPred)
{ }
~CuCopy()
{ }
ComponentType GetType() const
{ return COPY; }
const char* GetName() const
{ return "<copy>"; }
void ReadFromStream(std::istream& rIn)
{ Vector<int> vec; rIn >> vec; vec.Add(-1); mCopyFromIndices.CopyFrom(vec); }
void WriteToStream(std::ostream& rOut)
{ Vector<int> vec; mCopyFromIndices.CopyTo(vec); vec.Add(1); rOut << vec; }
protected:
void PropagateFnc(const CuMatrix<BaseFloat>& X, CuMatrix<BaseFloat>& Y)
{ CuMath<BaseFloat>::Rearrange(Y,X,mCopyFromIndices); }
void BackpropagateFnc(const CuMatrix<BaseFloat>& X, CuMatrix<BaseFloat>& Y)
{ Error("__func__ Nonsense"); }
protected:
CuVector<int> mCopyFromIndices;
};
/**
* \brief Perform Feature transpose
*
* \ingroup CuNNMisc
*/
class CuTranspose : public CuComponent
{
public:
CuTranspose(size_t nInputs, size_t nOutputs, CuComponent* pPred)
: CuComponent(nInputs,nOutputs,pPred), mContext(0)
{ }
~CuTranspose()
{ }
ComponentType GetType() const
{ return TRANSPOSE; }
const char* GetName() const
{ return "<transpose>"; }
void ReadFromStream(std::istream& rIn)
{
rIn >> std::ws >> mContext;
if(GetNInputs() != GetNOutputs()) {
Error("Input dim must be same as output dim");
}
if(GetNInputs() % mContext != 0) {
Error("Number of inputs must be divisible by context length");
}
Vector<int> vec(GetNInputs());
int channels = GetNInputs() / mContext;
for(int i=0, ch=0; ch<channels; ch++) {
for(int idx=ch; idx < (int)GetNInputs(); idx+=channels, i++) {
assert(i < (int)GetNInputs());
vec[i] = idx;
}
}
mCopyFromIndices.CopyFrom(vec);
}
void WriteToStream(std::ostream& rOut)
{ rOut << " " << mContext << "\n"; }
protected:
void PropagateFnc(const CuMatrix<BaseFloat>& X, CuMatrix<BaseFloat>& Y)
{ CuMath<BaseFloat>::Rearrange(Y,X,mCopyFromIndices); }
void BackpropagateFnc(const CuMatrix<BaseFloat>& X, CuMatrix<BaseFloat>& Y)
{ Error("__func__ Nonsense"); }
protected:
int mContext;
CuVector<int> mCopyFromIndices;
};
/**
* \brief Used for the blockwise multiplication by
* DCT transform loaded from disk
*
* \ingroup CuNNMisc
*/
class CuBlockLinearity : public CuComponent
{
public:
CuBlockLinearity(size_t nInputs, size_t nOutputs, CuComponent* pPred)
: CuComponent(nInputs,nOutputs,pPred)
{ }
~CuBlockLinearity()
{ }
ComponentType GetType() const
{ return CuComponent::BLOCK_LINEARITY; }
const char* GetName() const
{ return "<blocklinearity>"; }
void PropagateFnc(const CuMatrix<BaseFloat>& X, CuMatrix<BaseFloat>& Y)
{ CuMath<BaseFloat>::BlockLinearity(Y,X,mBlockLinearity); }
void BackpropagateFnc(const CuMatrix<BaseFloat>& X, CuMatrix<BaseFloat>& Y)
{ Error("__func__ Not implemented"); }
void ReadFromStream(std::istream& rIn)
{
Matrix<BaseFloat> mat;
rIn >> mat;
Matrix<BaseFloat> trans(mat,TRANS);
mBlockLinearity.CopyFrom(trans);
if((GetNOutputs() % mBlockLinearity.Cols() != 0) ||
(GetNInputs() % mBlockLinearity.Rows() != 0) ||
((GetNOutputs() / mBlockLinearity.Cols()) !=
(GetNInputs() / mBlockLinearity.Rows())))
{
Error("BlockLinearity matrix dimensions must divide IO dims");
}
}
void WriteToStream(std::ostream& rOut)
{
Matrix<BaseFloat> mat;
mBlockLinearity.CopyTo(mat);
Matrix<BaseFloat> trans(mat,TRANS);
rOut << trans;
}
private:
CuMatrix<BaseFloat> mBlockLinearity;
};
/**
* \brief Bias layer
*
* \ingroup CuNNMisc
* Implements: \f$ \vec{Y}=\vec{X}+\vec{\beta} \f$
*/
class CuBias : public CuComponent
{
public:
CuBias(size_t nInputs, size_t nOutputs, CuComponent* pPred)
: CuComponent(nInputs,nOutputs,pPred)
{ }
~CuBias()
{ }
ComponentType GetType() const
{ return CuComponent::BIAS; }
const char* GetName() const
{ return "<bias>"; }
void PropagateFnc(const CuMatrix<BaseFloat>& X, CuMatrix<BaseFloat>& Y)
{ Y.CopyFrom(X); Y.AddScaledRow(1.0, mBias, 1.0); }
void BackpropagateFnc(const CuMatrix<BaseFloat>& X, CuMatrix<BaseFloat>& Y)
{ Y.CopyFrom(X); }
void ReadFromStream(std::istream& rIn)
{ Vector<BaseFloat> vec; rIn >> vec; mBias.CopyFrom(vec); }
void WriteToStream(std::ostream& rOut)
{ Vector<BaseFloat> vec; mBias.CopyTo(vec); rOut << vec; }
private:
CuVector<BaseFloat> mBias;
};
/**
* \brief Column Scaling
*
* \ingroup CuNNMisc
* Scaling the inputs with coefficients in scaling CuVector mWindow
*/
class CuWindow : public CuComponent
{
public:
CuWindow(size_t nInputs, size_t nOutputs, CuComponent* pPred)
: CuComponent(nInputs, nOutputs, pPred)
{ }
~CuWindow()
{ }
ComponentType GetType() const
{ return CuComponent::WINDOW; }
const char* GetName() const
{ return "<window>"; }
void PropagateFnc(const CuMatrix<BaseFloat>& X, CuMatrix<BaseFloat>& Y)
{ Y.CopyFrom(X); Y.ScaleCols(mWindow); }
void BackpropagateFnc(const CuMatrix<BaseFloat>& X, CuMatrix<BaseFloat>& Y)
{ Error("__func__ Not implemented"); }
void ReadFromStream(std::istream& rIn)
{ Vector<BaseFloat> vec; rIn >> vec; mWindow.CopyFrom(vec); }
void WriteToStream(std::ostream& rOut)
{ Vector<BaseFloat> vec; mWindow.CopyTo(vec); rOut << vec; }
private:
CuVector<BaseFloat> mWindow; ///< Scaling factors
};
/**
* \brief Perform log transorm
*
* \ingroup CuNNMisc
* Calculate: \f[ \vec{Y}=\ln \vec{X} \f]
*/
class CuLog : public CuComponent
{
public:
CuLog(size_t nInputs, size_t nOutputs, CuComponent* pPred)
: CuComponent(nInputs, nOutputs, pPred)
{ }
~CuLog()
{ }
ComponentType GetType() const
{ return CuComponent::LOG; }
const char* GetName() const
{ return "<log>"; }
void PropagateFnc(const CuMatrix<BaseFloat>& X, CuMatrix<BaseFloat>& Y)
{ Y.CopyFrom(X); Y.ApplyLog(); }
void BackpropagateFnc(const CuMatrix<BaseFloat>& X, CuMatrix<BaseFloat>& Y)
{ Error("__func__ Not implemented"); }
void ReadFromStream(std::istream& rIn)
{ }
void WriteToStream(std::ostream& rOut)
{ }
};
}
#endif
|