forked from kaldi-asr/kaldi
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnnet-utils.cc
More file actions
734 lines (684 loc) · 28.1 KB
/
nnet-utils.cc
File metadata and controls
734 lines (684 loc) · 28.1 KB
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
// nnet3/nnet-utils.cc
// Copyright 2015 Johns Hopkins University (author: Daniel Povey)
// 2016 Daniel Galvez
//
// See ../../COPYING for clarification regarding multiple authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
// WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABLITY OR NON-INFRINGEMENT.
// See the Apache 2 License for the specific language governing permissions and
// limitations under the License.
#include "nnet3/nnet-utils.h"
#include "nnet3/nnet-graph.h"
#include "nnet3/nnet-simple-component.h"
#include "nnet3/nnet-parse.h"
namespace kaldi {
namespace nnet3 {
int32 NumOutputNodes(const Nnet &nnet) {
int32 ans = 0;
for (int32 n = 0; n < nnet.NumNodes(); n++)
if (nnet.IsOutputNode(n))
ans++;
return ans;
}
int32 NumInputNodes(const Nnet &nnet) {
int32 ans = 0;
for (int32 n = 0; n < nnet.NumNodes(); n++)
if (nnet.IsInputNode(n))
ans++;
return ans;
}
bool IsSimpleNnet(const Nnet &nnet) {
// check that we have an output node and called "output".
if (nnet.GetNodeIndex("output") == -1 ||
!nnet.IsOutputNode(nnet.GetNodeIndex("output")))
return false;
// check that there is an input node named "input".
if (nnet.GetNodeIndex("input") == -1 ||
!nnet.IsInputNode(nnet.GetNodeIndex("input")))
return false;
// if there was just one input, then it was named
// "input" and everything checks out.
if (NumInputNodes(nnet) == 1)
return true;
// Otherwise, there should be 2 inputs and one
// should be called "ivector".
return NumInputNodes(nnet) == 2 &&
nnet.GetNodeIndex("ivector") != -1 &&
nnet.IsInputNode(nnet.GetNodeIndex("ivector"));
}
void EvaluateComputationRequest(
const Nnet &nnet,
const ComputationRequest &request,
std::vector<std::vector<bool> > *is_computable) {
ComputationGraph graph;
ComputationGraphBuilder builder(nnet, request, &graph);
builder.Compute();
builder.GetComputableInfo(is_computable);
if (GetVerboseLevel() >= 2) {
std::ostringstream graph_pretty;
graph.Print(graph_pretty, nnet.GetNodeNames());
KALDI_VLOG(2) << "Graph is " << graph_pretty.str();
}
}
// this non-exported function is used in ComputeSimpleNnetContext
// to compute the left and right context of the nnet for a particular
// window size and shift-length.
static void ComputeSimpleNnetContextForShift(
const Nnet &nnet,
int32 input_start,
int32 window_size,
int32 *left_context,
int32 *right_context) {
int32 input_end = input_start + window_size;
IoSpecification input;
input.name = "input";
IoSpecification output;
output.name = "output";
IoSpecification ivector; // we might or might not use this.
ivector.name = "ivector";
int32 n = rand() % 10;
// in the IoSpecification for now we we will request all the same indexes at
// output that we requested at input.
for (int32 t = input_start; t < input_end; t++) {
input.indexes.push_back(Index(n, t));
output.indexes.push_back(Index(n, t));
}
// the assumption here is that the network just requires the ivector at time
// t=0.
ivector.indexes.push_back(Index(n, 0));
ComputationRequest request;
request.inputs.push_back(input);
request.outputs.push_back(output);
if (nnet.GetNodeIndex("ivector") != -1)
request.inputs.push_back(ivector);
std::vector<std::vector<bool> > computable;
EvaluateComputationRequest(nnet, request, &computable);
KALDI_ASSERT(computable.size() == 1);
std::vector<bool> &output_ok = computable[0];
std::vector<bool>::iterator iter =
std::find(output_ok.begin(), output_ok.end(), true);
int32 first_ok = iter - output_ok.begin();
int32 first_not_ok = std::find(iter, output_ok.end(), false) -
output_ok.begin();
if (first_ok == window_size || first_not_ok <= first_ok)
KALDI_ERR << "No outputs were computable (perhaps not a simple nnet?)";
*left_context = first_ok;
*right_context = window_size - first_not_ok;
}
void ComputeSimpleNnetContext(const Nnet &nnet,
int32 *left_context,
int32 *right_context) {
KALDI_ASSERT(IsSimpleNnet(nnet));
int32 modulus = nnet.Modulus();
// modulus >= 1 is a number such that the network ought to be
// invariant to time shifts (of both the input and output) that
// are a multiple of this number. We need to test all shifts modulo
// this number in case the left and right context vary at all within
// this range.
std::vector<int32> left_contexts(modulus + 1);
std::vector<int32> right_contexts(modulus + 1);
// This will crash if the total context (left + right) is greater
// than window_size.
int32 window_size = 100;
// by going "<= modulus" instead of "< modulus" we do one more computation
// than we really need; it becomes a sanity check.
for (int32 input_start = 0; input_start <= modulus; input_start++)
ComputeSimpleNnetContextForShift(nnet, input_start, window_size,
&(left_contexts[input_start]),
&(right_contexts[input_start]));
KALDI_ASSERT(left_contexts[0] == left_contexts[modulus] &&
"nnet does not have the properties we expect.");
KALDI_ASSERT(right_contexts[0] == right_contexts[modulus] &&
"nnet does not have the properties we expect.");
*left_context =
*std::max_element(left_contexts.begin(), left_contexts.end());
*right_context =
*std::max_element(right_contexts.begin(), right_contexts.end());
}
void SetZero(bool is_gradient,
Nnet *nnet) {
for (int32 c = 0; c < nnet->NumComponents(); c++) {
Component *comp = nnet->GetComponent(c);
if (comp->Properties() & kUpdatableComponent) {
UpdatableComponent *u_comp = dynamic_cast<UpdatableComponent*>(comp);
KALDI_ASSERT(u_comp != NULL);
u_comp->SetZero(is_gradient);
}
}
}
void PerturbParams(BaseFloat stddev,
Nnet *nnet) {
for (int32 c = 0; c < nnet->NumComponents(); c++) {
Component *comp = nnet->GetComponent(c);
if (comp->Properties() & kUpdatableComponent) {
UpdatableComponent *u_comp = dynamic_cast<UpdatableComponent*>(comp);
KALDI_ASSERT(u_comp != NULL);
u_comp->PerturbParams(stddev);
}
}
}
void ComponentDotProducts(const Nnet &nnet1,
const Nnet &nnet2,
VectorBase<BaseFloat> *dot_prod) {
KALDI_ASSERT(nnet1.NumComponents() == nnet2.NumComponents());
int32 updatable_c = 0;
for (int32 c = 0; c < nnet1.NumComponents(); c++) {
const Component *comp1 = nnet1.GetComponent(c),
*comp2 = nnet2.GetComponent(c);
if (comp1->Properties() & kUpdatableComponent) {
const UpdatableComponent
*u_comp1 = dynamic_cast<const UpdatableComponent*>(comp1),
*u_comp2 = dynamic_cast<const UpdatableComponent*>(comp2);
KALDI_ASSERT(u_comp1 != NULL && u_comp2 != NULL);
dot_prod->Data()[updatable_c] = u_comp1->DotProduct(*u_comp2);
updatable_c++;
}
}
KALDI_ASSERT(updatable_c == dot_prod->Dim());
}
std::string PrintVectorPerUpdatableComponent(const Nnet &nnet,
const VectorBase<BaseFloat> &vec) {
std::ostringstream os;
os << "[ ";
KALDI_ASSERT(NumUpdatableComponents(nnet) == vec.Dim());
int32 updatable_c = 0;
for (int32 c = 0; c < nnet.NumComponents(); c++) {
const Component *comp = nnet.GetComponent(c);
if (comp->Properties() & kUpdatableComponent) {
const std::string &component_name = nnet.GetComponentName(c);
os << component_name << ':' << vec(updatable_c) << ' ';
updatable_c++;
}
}
KALDI_ASSERT(updatable_c == vec.Dim());
os << ']';
return os.str();
}
BaseFloat DotProduct(const Nnet &nnet1,
const Nnet &nnet2) {
KALDI_ASSERT(nnet1.NumComponents() == nnet2.NumComponents());
BaseFloat ans = 0.0;
for (int32 c = 0; c < nnet1.NumComponents(); c++) {
const Component *comp1 = nnet1.GetComponent(c),
*comp2 = nnet2.GetComponent(c);
if (comp1->Properties() & kUpdatableComponent) {
const UpdatableComponent
*u_comp1 = dynamic_cast<const UpdatableComponent*>(comp1),
*u_comp2 = dynamic_cast<const UpdatableComponent*>(comp2);
KALDI_ASSERT(u_comp1 != NULL && u_comp2 != NULL);
ans += u_comp1->DotProduct(*u_comp2);
}
}
return ans;
}
void ZeroComponentStats(Nnet *nnet) {
for (int32 c = 0; c < nnet->NumComponents(); c++) {
Component *comp = nnet->GetComponent(c);
comp->ZeroStats(); // for some components, this won't do anything.
}
}
void ScaleLearningRate(BaseFloat learning_rate_scale,
Nnet *nnet) {
for (int32 c = 0; c < nnet->NumComponents(); c++) {
Component *comp = nnet->GetComponent(c);
if (comp->Properties() & kUpdatableComponent) {
// For now all updatable components inherit from class UpdatableComponent.
// If that changes in future, we will change this code.
UpdatableComponent *uc = dynamic_cast<UpdatableComponent*>(comp);
if (uc == NULL)
KALDI_ERR << "Updatable component does not inherit from class "
"UpdatableComponent; change this code.";
uc->SetActualLearningRate(uc->LearningRate() * learning_rate_scale);
}
}
}
void SetLearningRate(BaseFloat learning_rate,
Nnet *nnet) {
for (int32 c = 0; c < nnet->NumComponents(); c++) {
Component *comp = nnet->GetComponent(c);
if (comp->Properties() & kUpdatableComponent) {
// For now all updatable components inherit from class UpdatableComponent.
// If that changes in future, we will change this code.
UpdatableComponent *uc = dynamic_cast<UpdatableComponent*>(comp);
if (uc == NULL)
KALDI_ERR << "Updatable component does not inherit from class "
"UpdatableComponent; change this code.";
uc->SetUnderlyingLearningRate(learning_rate);
}
}
}
void SetLearningRates(const Vector<BaseFloat> &learning_rates,
Nnet *nnet) {
int32 i = 0;
for (int32 c = 0; c < nnet->NumComponents(); c++) {
Component *comp = nnet->GetComponent(c);
if (comp->Properties() & kUpdatableComponent) {
// For now all updatable components inherit from class UpdatableComponent.
// If that changes in future, we will change this code.
UpdatableComponent *uc = dynamic_cast<UpdatableComponent*>(comp);
if (uc == NULL)
KALDI_ERR << "Updatable component does not inherit from class "
"UpdatableComponent; change this code.";
KALDI_ASSERT(i < learning_rates.Dim());
uc->SetActualLearningRate(learning_rates(i++));
}
}
KALDI_ASSERT(i == learning_rates.Dim());
}
void GetLearningRates(const Nnet &nnet,
Vector<BaseFloat> *learning_rates) {
learning_rates->Resize(NumUpdatableComponents(nnet));
int32 i = 0;
for (int32 c = 0; c < nnet.NumComponents(); c++) {
const Component *comp = nnet.GetComponent(c);
if (comp->Properties() & kUpdatableComponent) {
// For now all updatable components inherit from class UpdatableComponent.
// If that changes in future, we will change this code.
const UpdatableComponent *uc = dynamic_cast<const UpdatableComponent*>(comp);
if (uc == NULL)
KALDI_ERR << "Updatable component does not inherit from class "
"UpdatableComponent; change this code.";
(*learning_rates)(i++) = uc->LearningRate();
}
}
KALDI_ASSERT(i == learning_rates->Dim());
}
void ScaleNnetComponents(const Vector<BaseFloat> &scale_factors,
Nnet *nnet) {
int32 i = 0;
for (int32 c = 0; c < nnet->NumComponents(); c++) {
Component *comp = nnet->GetComponent(c);
if (comp->Properties() & kUpdatableComponent) {
// For now all updatable components inherit from class UpdatableComponent.
// If that changes in future, we will change this code.
UpdatableComponent *uc = dynamic_cast<UpdatableComponent*>(comp);
if (uc == NULL)
KALDI_ERR << "Updatable component does not inherit from class "
"UpdatableComponent; change this code.";
KALDI_ASSERT(i < scale_factors.Dim());
uc->Scale(scale_factors(i++));
}
}
KALDI_ASSERT(i == scale_factors.Dim());
}
void ScaleNnet(BaseFloat scale, Nnet *nnet) {
if (scale == 1.0) return;
else if (scale == 0.0) {
SetZero(false, nnet);
} else {
for (int32 c = 0; c < nnet->NumComponents(); c++) {
Component *comp = nnet->GetComponent(c);
comp->Scale(scale);
}
}
}
void AddNnetComponents(const Nnet &src, const Vector<BaseFloat> &alphas,
BaseFloat scale, Nnet *dest) {
if (src.NumComponents() != dest->NumComponents())
KALDI_ERR << "Trying to add incompatible nnets.";
int32 i = 0;
for (int32 c = 0; c < src.NumComponents(); c++) {
const Component *src_comp = src.GetComponent(c);
Component *dest_comp = dest->GetComponent(c);
if (src_comp->Properties() & kUpdatableComponent) {
// For now all updatable components inherit from class UpdatableComponent.
// If that changes in future, we will change this code.
const UpdatableComponent *src_uc =
dynamic_cast<const UpdatableComponent*>(src_comp);
UpdatableComponent *dest_uc =
dynamic_cast<UpdatableComponent*>(dest_comp);
if (src_uc == NULL || dest_uc == NULL)
KALDI_ERR << "Updatable component does not inherit from class "
"UpdatableComponent; change this code.";
KALDI_ASSERT(i < alphas.Dim());
dest_uc->Add(alphas(i++), *src_uc);
} else { // add stored stats
dest_comp->Add(scale, *src_comp);
}
}
KALDI_ASSERT(i == alphas.Dim());
}
void AddNnet(const Nnet &src, BaseFloat alpha, Nnet *dest) {
if (src.NumComponents() != dest->NumComponents())
KALDI_ERR << "Trying to add incompatible nnets.";
for (int32 c = 0; c < src.NumComponents(); c++) {
const Component *src_comp = src.GetComponent(c);
Component *dest_comp = dest->GetComponent(c);
dest_comp->Add(alpha, *src_comp);
}
}
int32 NumParameters(const Nnet &src) {
int32 ans = 0;
for (int32 c = 0; c < src.NumComponents(); c++) {
const Component *comp = src.GetComponent(c);
if (comp->Properties() & kUpdatableComponent) {
// For now all updatable components inherit from class UpdatableComponent.
// If that changes in future, we will change this code.
const UpdatableComponent *uc =
dynamic_cast<const UpdatableComponent*>(comp);
if (uc == NULL)
KALDI_ERR << "Updatable component does not inherit from class "
"UpdatableComponent; change this code.";
ans += uc->NumParameters();
}
}
return ans;
}
void VectorizeNnet(const Nnet &src,
VectorBase<BaseFloat> *parameters) {
KALDI_ASSERT(parameters->Dim() == NumParameters(src));
int32 dim_offset = 0;
for (int32 c = 0; c < src.NumComponents(); c++) {
const Component *comp = src.GetComponent(c);
if (comp->Properties() & kUpdatableComponent) {
// For now all updatable components inherit from class UpdatableComponent.
// If that changes in future, we will change this code.
const UpdatableComponent *uc =
dynamic_cast<const UpdatableComponent*>(comp);
if (uc == NULL)
KALDI_ERR << "Updatable component does not inherit from class "
"UpdatableComponent; change this code.";
int32 this_dim = uc->NumParameters();
SubVector<BaseFloat> this_part(*parameters, dim_offset, this_dim);
uc->Vectorize(&this_part);
dim_offset += this_dim;
}
}
}
void UnVectorizeNnet(const VectorBase<BaseFloat> ¶meters,
Nnet *dest) {
KALDI_ASSERT(parameters.Dim() == NumParameters(*dest));
int32 dim_offset = 0;
for (int32 c = 0; c < dest->NumComponents(); c++) {
Component *comp = dest->GetComponent(c);
if (comp->Properties() & kUpdatableComponent) {
// For now all updatable components inherit from class UpdatableComponent.
// If that changes in future, we will change this code.
UpdatableComponent *uc = dynamic_cast<UpdatableComponent*>(comp);
if (uc == NULL)
KALDI_ERR << "Updatable component does not inherit from class "
"UpdatableComponent; change this code.";
int32 this_dim = uc->NumParameters();
const SubVector<BaseFloat> this_part(parameters, dim_offset, this_dim);
uc->UnVectorize(this_part);
dim_offset += this_dim;
}
}
}
int32 NumUpdatableComponents(const Nnet &dest) {
int32 ans = 0;
for (int32 c = 0; c < dest.NumComponents(); c++) {
const Component *comp = dest.GetComponent(c);
if (comp->Properties() & kUpdatableComponent)
ans++;
}
return ans;
}
void ConvertRepeatedToBlockAffine(CompositeComponent *c_component) {
for(int32 i = 0; i < c_component->NumComponents(); i++) {
const Component *c = c_component->GetComponent(i);
KALDI_ASSERT(c->Type() != "CompositeComponent" &&
"Nesting CompositeComponent within CompositeComponent is not allowed.\n"
"(We may change this as more complicated components are introduced.)");
if(c->Type() == "RepeatedAffineComponent" ||
c->Type() == "NaturalGradientRepeatedAffineComponent") {
// N.B.: NaturalGradientRepeatedAffineComponent is a subclass of
// RepeatedAffineComponent.
const RepeatedAffineComponent *rac =
dynamic_cast<const RepeatedAffineComponent*>(c);
KALDI_ASSERT(rac != NULL);
BlockAffineComponent *bac = new BlockAffineComponent(*rac);
// following call deletes rac
c_component->SetComponent(i, bac);
}
}
}
void ConvertRepeatedToBlockAffine(Nnet *nnet) {
for(int32 i = 0; i < nnet->NumComponents(); i++) {
const Component *const_c = nnet->GetComponent(i);
if(const_c->Type() == "RepeatedAffineComponent" ||
const_c->Type() == "NaturalGradientRepeatedAffineComponent") {
// N.B.: NaturalGradientRepeatedAffineComponent is a subclass of
// RepeatedAffineComponent.
const RepeatedAffineComponent *rac =
dynamic_cast<const RepeatedAffineComponent*>(const_c);
KALDI_ASSERT(rac != NULL);
BlockAffineComponent *bac = new BlockAffineComponent(*rac);
// following call deletes rac
nnet->SetComponent(i, bac);
} else if (const_c->Type() == "CompositeComponent") {
// We must modify the composite component, so we use the
// non-const GetComponent() call here.
Component *c = nnet->GetComponent(i);
CompositeComponent *cc = dynamic_cast<CompositeComponent*>(c);
KALDI_ASSERT(cc != NULL);
ConvertRepeatedToBlockAffine(cc);
}
}
}
std::string NnetInfo(const Nnet &nnet) {
std::ostringstream ostr;
if (IsSimpleNnet(nnet)) {
int32 left_context, right_context;
// this call will crash if the nnet is not 'simple'.
ComputeSimpleNnetContext(nnet, &left_context, &right_context);
ostr << "left-context: " << left_context << "\n";
ostr << "right-context: " << right_context << "\n";
}
ostr << "input-dim: " << nnet.InputDim("input") << "\n";
ostr << "ivector-dim: " << nnet.InputDim("ivector") << "\n";
ostr << "output-dim: " << nnet.OutputDim("output") << "\n";
ostr << "# Nnet info follows.\n";
ostr << nnet.Info();
return ostr.str();
}
void SetDropoutProportion(BaseFloat dropout_proportion,
bool dropout_per_frame ,
Nnet *nnet) {
dropout_per_frame = false;
for (int32 c = 0; c < nnet->NumComponents(); c++) {
Component *comp = nnet->GetComponent(c);
DropoutComponent *dc = dynamic_cast<DropoutComponent*>(comp);
if (dc != NULL)
dc->SetDropoutProportion(dropout_proportion, dropout_per_frame);
}
}
void FindOrphanComponents(const Nnet &nnet, std::vector<int32> *components) {
int32 num_components = nnet.NumComponents(), num_nodes = nnet.NumNodes();
std::vector<bool> is_used(num_components, false);
for (int32 i = 0; i < num_nodes; i++) {
if (nnet.IsComponentNode(i)) {
int32 c = nnet.GetNode(i).u.component_index;
KALDI_ASSERT(c >= 0 && c < num_components);
is_used[c] = true;
}
}
components->clear();
for (int32 i = 0; i < num_components; i++)
if (!is_used[i])
components->push_back(i);
}
void FindOrphanNodes(const Nnet &nnet, std::vector<int32> *nodes) {
std::vector<std::vector<int32> > depend_on_graph, dependency_graph;
NnetToDirectedGraph(nnet, &depend_on_graph);
// depend_on_graph[i] is a list of all the nodes that depend on i.
ComputeGraphTranspose(depend_on_graph, &dependency_graph);
// dependency_graph[i] is a list of all the nodes that i depends on,
// to be computed.
// Find all nodes required to produce the outputs.
int32 num_nodes = nnet.NumNodes();
assert(num_nodes == static_cast<int32>(dependency_graph.size()));
std::vector<bool> node_is_required(num_nodes, false);
std::vector<int32> queue;
for (int32 i = 0; i < num_nodes; i++) {
if (nnet.IsOutputNode(i))
queue.push_back(i);
}
while (!queue.empty()) {
int32 i = queue.back();
queue.pop_back();
if (!node_is_required[i]) {
node_is_required[i] = true;
for (size_t j = 0; j < dependency_graph[i].size(); j++)
queue.push_back(dependency_graph[i][j]);
}
}
nodes->clear();
for (int32 i = 0; i < num_nodes; i++) {
if (!node_is_required[i])
nodes->push_back(i);
}
}
void ReadEditConfig(std::istream &edit_config_is, Nnet *nnet) {
std::vector<std::string> lines;
ReadConfigLines(edit_config_is, &lines);
// we process this as a sequence of lines.
std::vector<ConfigLine> config_lines;
ParseConfigLines(lines, &config_lines);
for (size_t i = 0; i < config_lines.size(); i++) {
ConfigLine &config_line = config_lines[i];
const std::string &directive = config_lines[i].FirstToken();
if (directive == "convert-to-fixed-affine") {
std::string name_pattern = "*";
// name_pattern defaults to '*' if none is given. Note: this pattern
// matches names of components, not nodes.
config_line.GetValue("name", &name_pattern);
int32 num_components_changed = 0;
for (int32 c = 0; c < nnet->NumComponents(); c++) {
Component *component = nnet->GetComponent(c);
AffineComponent *affine = NULL;
if (NameMatchesPattern(nnet->GetComponentName(c).c_str(),
name_pattern.c_str()) &&
(affine = dynamic_cast<AffineComponent*>(component))) {
nnet->SetComponent(c, new FixedAffineComponent(*affine));
num_components_changed++;
}
}
KALDI_LOG << "Converted " << num_components_changed
<< " components to FixedAffineComponent.";
} else if (directive == "remove-orphan-nodes") {
bool remove_orphan_inputs = false;
config_line.GetValue("remove-orphan-inputs", &remove_orphan_inputs);
nnet->RemoveOrphanNodes(remove_orphan_inputs);
} else if (directive == "remove-orphan-components") {
nnet->RemoveOrphanComponents();
} else if (directive == "remove-orphans") {
bool remove_orphan_inputs = false;
config_line.GetValue("remove-orphan-inputs", &remove_orphan_inputs);
nnet->RemoveOrphanNodes(remove_orphan_inputs);
nnet->RemoveOrphanComponents();
} else if (directive == "set-learning-rate") {
std::string name_pattern = "*";
// name_pattern defaults to '*' if none is given. This pattern
// matches names of components, not nodes.
config_line.GetValue("name", &name_pattern);
BaseFloat learning_rate = -1;
if (!config_line.GetValue("learning-rate", &learning_rate)) {
KALDI_ERR << "In edits-config, expected learning-rate to be set in line: "
<< config_line.WholeLine();
}
// Note: the learning rate you provide will be multiplied by any
// 'learning-rate-factor' that is defined in the component,
// so if you call SetUnderlyingLearningRate(), the actual learning
// rate (learning_rate_) is set to the value you provide times
// learning_rate_factor_.
UpdatableComponent *component = NULL;
int32 num_learning_rates_set = 0;
for (int32 c = 0; c < nnet->NumComponents(); c++) {
if (NameMatchesPattern(nnet->GetComponentName(c).c_str(),
name_pattern.c_str()) &&
(component =
dynamic_cast<UpdatableComponent*>(nnet->GetComponent(c)))) {
component->SetUnderlyingLearningRate(learning_rate);
num_learning_rates_set++;
}
}
KALDI_LOG << "Set learning rates for " << num_learning_rates_set << " nodes.";
} else if (directive == "rename-node") {
// this is a shallow renaming of a node, and it requires that the name used is
// not the name of another node.
std::string old_name, new_name;
if (!config_line.GetValue("old-name", &old_name) ||
!config_line.GetValue("new-name", &new_name) ||
config_line.HasUnusedValues()) {
KALDI_ERR << "In edits-config, could not make sense of this rename-node "
<< "directive (expect old-name=xxx new-name=xxx) "
<< config_line.WholeLine();
}
if (nnet->GetNodeIndex(old_name) < 0)
KALDI_ERR << "Could not rename node from " << old_name << " to "
<< new_name << " because there is no node called "
<< old_name;
// further checks will happen inside SetNodeName().
nnet->SetNodeName(nnet->GetNodeIndex(old_name), new_name);
} else if (directive == "remove-output-nodes") {
// note: after remove-output-nodes you probably want to do 'remove-orphans'.
std::string name_pattern;
if (!config_line.GetValue("name", &name_pattern) ||
config_line.HasUnusedValues())
KALDI_ERR << "In edits-config, could not make sense of "
<< "remove-output-nodes directive: "
<< config_line.WholeLine();
std::vector<int32> nodes_to_remove;
int32 outputs_remaining = 0;
for (int32 n = 0; n < nnet->NumNodes(); n++) {
if (nnet->IsOutputNode(n)) {
if (NameMatchesPattern(nnet->GetNodeName(n).c_str(),
name_pattern.c_str()))
nodes_to_remove.push_back(n);
else
outputs_remaining++;
}
}
KALDI_LOG << "Removing " << nodes_to_remove.size() << " output nodes.";
if (outputs_remaining == 0)
KALDI_ERR << "All outputs were removed.";
nnet->RemoveSomeNodes(nodes_to_remove);
} else if (directive == "set-dropout-proportion") {
std::string name_pattern = "*";
// name_pattern defaults to '*' if none is given. This pattern
// matches names of components, not nodes.
config_line.GetValue("name", &name_pattern);
BaseFloat proportion = -1;
bool perframe = false;
if (!config_line.GetValue("proportion", &proportion)) {
KALDI_ERR << "In edits-config, expected proportion to be set in line: "
<< config_line.WholeLine();
}
if (!config_line.GetValue("perframe", &perframe)) {
perframe = false;
}
DropoutComponent *component = NULL;
int32 num_dropout_proportions_set = 0;
for (int32 c = 0; c < nnet->NumComponents(); c++) {
if (NameMatchesPattern(nnet->GetComponentName(c).c_str(),
name_pattern.c_str()) &&
(component =
dynamic_cast<DropoutComponent*>(nnet->GetComponent(c)))) {
component->SetDropoutProportion(proportion, perframe);
num_dropout_proportions_set++;
}
}
KALDI_LOG << "Set dropout proportions for "
<< num_dropout_proportions_set << " nodes.";
} else {
KALDI_ERR << "Directive '" << directive << "' is not currently "
"supported (reading edit-config).";
}
if (config_line.HasUnusedValues()) {
KALDI_ERR << "Could not interpret '" << config_line.UnusedValues()
<< "' in edit config line " << config_line.WholeLine();
}
}
}
} // namespace nnet3
} // namespace kaldi