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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
|
#include <algorithm>
//#include <locale>
#include <cctype>
#include "Nnet.h"
#include "CRBEDctFeat.h"
#include "BlockArray.h"
namespace TNet {
void Network::Feedforward(const Matrix<BaseFloat>& in, Matrix<BaseFloat>& out,
size_t start_frm_ext, size_t end_frm_ext) {
//empty network: copy input to output
if(mNnet.size() == 0) {
if(out.Rows() != in.Rows() || out.Cols() != in.Cols()) {
out.Init(in.Rows(),in.Cols());
}
out.Copy(in);
return;
}
//short input: propagate in one block
if(in.Rows() < 5000) {
Propagate(in,out);
} else {//long input: propagate per parts
//initialize
out.Init(in.Rows(),GetNOutputs());
Matrix<BaseFloat> tmp_in, tmp_out;
int done=0, block=1024;
//propagate first part
tmp_in.Init(block+end_frm_ext,in.Cols());
tmp_in.Copy(in.Range(0,block+end_frm_ext,0,in.Cols()));
Propagate(tmp_in,tmp_out);
out.Range(0,block,0,tmp_out.Cols()).Copy(
tmp_out.Range(0,block,0,tmp_out.Cols())
);
done += block;
//propagate middle parts
while((done+2*block) < in.Rows()) {
tmp_in.Init(block+start_frm_ext+end_frm_ext,in.Cols());
tmp_in.Copy(in.Range(done-start_frm_ext, block+start_frm_ext+end_frm_ext, 0,in.Cols())); Propagate(tmp_in,tmp_out);
out.Range(done,block,0,tmp_out.Cols()).Copy(
tmp_out.Range(start_frm_ext,block,0,tmp_out.Cols())
);
done += block;
}
//propagate last part
tmp_in.Init(in.Rows()-done+start_frm_ext,in.Cols());
tmp_in.Copy(in.Range(done-start_frm_ext,in.Rows()-done+start_frm_ext,0,in.Cols()));
Propagate(tmp_in,tmp_out);
out.Range(done,out.Rows()-done,0,out.Cols()).Copy(
tmp_out.Range(start_frm_ext,tmp_out.Rows()-start_frm_ext,0,tmp_out.Cols())
);
done += tmp_out.Rows()-start_frm_ext;
assert(done == out.Rows());
}
}
void Network::Propagate(const Matrix<BaseFloat>& in, Matrix<BaseFloat>& out) {
//empty network: copy input to output
if(mNnet.size() == 0) {
if(out.Rows() != in.Rows() || out.Cols() != in.Cols()) {
out.Init(in.Rows(),in.Cols());
}
out.Copy(in);
return;
}
//this will keep pointer to matrix 'in', for backprop
mNnet.front()->SetInput(in);
//propagate
LayeredType::iterator it;
for(it=mNnet.begin(); it!=mNnet.end(); ++it) {
(*it)->Propagate();
}
//copy the output matrix
const Matrix<BaseFloat>& mat = mNnet.back()->GetOutput();
if(out.Rows() != mat.Rows() || out.Cols() != mat.Cols()) {
out.Init(mat.Rows(),mat.Cols());
}
out.Copy(mat);
}
void Network::Backpropagate(const Matrix<BaseFloat>& globerr) {
//pass matrix to last component
mNnet.back()->SetErrorInput(globerr);
// back-propagation : reversed order,
LayeredType::reverse_iterator it;
for(it=mNnet.rbegin(); it!=mNnet.rend(); ++it) {
//first component does not backpropagate error (no predecessors)
if(*it != mNnet.front()) {
(*it)->Backpropagate();
}
//compute gradient if updatable component
if((*it)->IsUpdatable()) {
UpdatableComponent& comp = dynamic_cast<UpdatableComponent&>(**it);
comp.Gradient(); //compute gradient
}
}
}
void Network::AccuGradient(const Network& src, int thr, int thrN) {
LayeredType::iterator it;
LayeredType::const_iterator it2;
for(it=mNnet.begin(), it2=src.mNnet.begin(); it!=mNnet.end(); ++it,++it2) {
if((*it)->IsUpdatable()) {
UpdatableComponent& comp = dynamic_cast<UpdatableComponent&>(**it);
const UpdatableComponent& comp2 = dynamic_cast<const UpdatableComponent&>(**it2);
comp.AccuGradient(comp2,thr,thrN);
}
}
}
void Network::Update(int thr, int thrN) {
LayeredType::iterator it;
for(it=mNnet.begin(); it!=mNnet.end(); ++it) {
if((*it)->IsUpdatable()) {
UpdatableComponent& comp = dynamic_cast<UpdatableComponent&>(**it);
comp.Update(thr,thrN);
}
}
}
Network* Network::Clone() {
Network* net = new Network;
LayeredType::iterator it;
for(it = mNnet.begin(); it != mNnet.end(); ++it) {
//clone
net->mNnet.push_back((*it)->Clone());
//connect network
if(net->mNnet.size() > 1) {
Component* last = *(net->mNnet.end()-1);
Component* prev = *(net->mNnet.end()-2);
last->SetInput(prev->GetOutput());
prev->SetErrorInput(last->GetErrorOutput());
}
}
//copy the learning rate
//net->SetLearnRate(GetLearnRate());
return net;
}
void Network::ReadNetwork(const char* pSrc) {
std::ifstream in(pSrc);
if(!in.good()) {
Error(std::string("Error, cannot read model: ")+pSrc);
}
ReadNetwork(in);
in.close();
}
void Network::ReadNetwork(std::istream& rIn) {
//get the network elements from a factory
Component *pComp;
while(NULL != (pComp = ComponentFactory(rIn)))
mNnet.push_back(pComp);
}
void Network::WriteNetwork(const char* pDst) {
std::ofstream out(pDst);
if(!out.good()) {
Error(std::string("Error, cannot write model: ")+pDst);
}
WriteNetwork(out);
out.close();
}
void Network::WriteNetwork(std::ostream& rOut) {
//dump all the componetns
LayeredType::iterator it;
for(it=mNnet.begin(); it!=mNnet.end(); ++it) {
ComponentDumper(rOut, **it);
}
}
Component*
Network::
ComponentFactory(std::istream& rIn)
{
rIn >> std::ws;
if(rIn.eof()) return NULL;
Component* pRet=NULL;
Component* pPred=NULL;
std::string componentTag;
size_t nInputs, nOutputs;
rIn >> std::ws;
rIn >> componentTag;
if(componentTag == "") return NULL; //nothing left in the file
//make it lowercase
std::transform(componentTag.begin(), componentTag.end(),
componentTag.begin(), tolower);
//the 'endblock' tag terminates the network
if(componentTag == "<endblock>") return NULL;
if(componentTag[0] != '<' || componentTag[componentTag.size()-1] != '>') {
Error(std::string("Invalid component tag:")+componentTag);
}
rIn >> std::ws;
rIn >> nOutputs;
rIn >> std::ws;
rIn >> nInputs;
assert(nInputs > 0 && nOutputs > 0);
//make coupling with predecessor
if(mNnet.size() == 0) {
pPred = NULL;
} else {
pPred = mNnet.back();
}
//array with list of component tags
static const std::string TAGS[] = {
"<biasedlinearity>",
"<sharedlinearity>",
"<sigmoid>",
"<softmax>",
"<blocksoftmax>",
"<expand>",
"<copy>",
"<transpose>",
"<blocklinearity>",
"<bias>",
"<window>",
"<log>",
"<blockarray>",
};
static const int n_tags = sizeof(TAGS) / sizeof(TAGS[0]);
int i = 0;
for(i=0; i<n_tags; i++) {
if(componentTag == TAGS[i]) break;
}
//switch according to position in array TAGS
switch(i) {
case 0: pRet = new BiasedLinearity(nInputs,nOutputs,pPred); break;
case 1: pRet = new SharedLinearity(nInputs,nOutputs,pPred); break;
case 2: pRet = new Sigmoid(nInputs,nOutputs,pPred); break;
case 3: pRet = new Softmax(nInputs,nOutputs,pPred); break;
case 4: pRet = new BlockSoftmax(nInputs,nOutputs,pPred); break;
case 5: pRet = new Expand(nInputs,nOutputs,pPred); break;
case 6: pRet = new Copy(nInputs,nOutputs,pPred); break;
case 7: pRet = new Transpose(nInputs,nOutputs,pPred); break;
case 8: pRet = new BlockLinearity(nInputs,nOutputs,pPred); break;
case 9: pRet = new Bias(nInputs,nOutputs,pPred); break;
case 10: pRet = new Window(nInputs,nOutputs,pPred); break;
case 11: pRet = new Log(nInputs,nOutputs,pPred); break;
case 12: pRet = new BlockArray(nInputs,nOutputs,pPred); break;
default: Error(std::string("Unknown Component tag:")+componentTag);
}
//read params if it is updatable component
pRet->ReadFromStream(rIn);
//return
return pRet;
}
void
Network::
ComponentDumper(std::ostream& rOut, Component& rComp)
{
//use tags of all the components; or the identification codes
//array with list of component tags
static const Component::ComponentType TYPES[] = {
Component::BIASED_LINEARITY,
Component::SHARED_LINEARITY,
Component::SIGMOID,
Component::SOFTMAX,
Component::BLOCK_SOFTMAX,
Component::EXPAND,
Component::COPY,
Component::TRANSPOSE,
Component::BLOCK_LINEARITY,
Component::BIAS,
Component::WINDOW,
Component::LOG,
Component::BLOCK_ARRAY,
};
static const std::string TAGS[] = {
"<biasedlinearity>",
"<sharedlinearity>",
"<sigmoid>",
"<softmax>",
"<blocksoftmax>",
"<expand>",
"<copy>",
"<transpose>",
"<blocklinearity>",
"<bias>",
"<window>",
"<log>",
"<blockarray>",
};
static const int MAX = sizeof TYPES / sizeof TYPES[0];
int i;
for(i=0; i<MAX; ++i) {
if(TYPES[i] == rComp.GetType()) break;
}
if(i == MAX) Error("Unknown ComponentType");
//dump the component tag
rOut << TAGS[i] << " "
<< rComp.GetNOutputs() << " "
<< rComp.GetNInputs() << std::endl;
//dump the parameters (if any)
rComp.WriteToStream(rOut);
}
} //namespace
|