widen-nnet.cc
Go to the documentation of this file.
1 // nnet2/widen-nnet.cc
2 
3 // Copyright 2012 Johns Hopkins University (author: Daniel Povey)
4 
5 // See ../../COPYING for clarification regarding multiple authors
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
15 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
16 // MERCHANTABLITY OR NON-INFRINGEMENT.
17 // See the Apache 2 License for the specific language governing permissions and
18 // limitations under the License.
19 
20 #include "nnet2/widen-nnet.h"
21 #include "gmm/model-common.h" // for GetSplitTargets()
22 #include <numeric> // for std::accumulate
23 
24 namespace kaldi {
25 namespace nnet2 {
26 
27 
29  BaseFloat param_stddev,
30  BaseFloat bias_stddev,
31  std::vector<NonlinearComponent*> c2, // will usually
32  // have just
33  // one element.
34  AffineComponent *c3) {
35  int32 old_dim = this->OutputDim(), extra_dim = new_dim - old_dim;
36  KALDI_ASSERT(!c2.empty());
37  if (new_dim <= old_dim) {
38  KALDI_WARN << "Not widening component because new dim "
39  << new_dim << " <= old dim " << old_dim;
40  return;
41  }
42 
43  this->bias_params_.Resize(new_dim,
44  kCopyData);
45  this->bias_params_.Range(old_dim, extra_dim).SetRandn();
46  this->bias_params_.Range(old_dim, extra_dim).Scale(bias_stddev);
47 
48  this->linear_params_.Resize(new_dim, InputDim(), kCopyData);
49  this->linear_params_.Range(old_dim, extra_dim,
50  0, InputDim()).SetRandn();
51  this->linear_params_.Range(old_dim, extra_dim,
52  0, InputDim()).Scale(param_stddev);
53 
54  for (size_t i = 0; i < c2.size(); i++) // Change dimension of nonlinear
55  c2[i]->SetDim(new_dim); // components
56 
57  // Change dimension of next affine component [extend with zeros,
58  // so the existing outputs do not change in value]
59  c3->linear_params_.Resize(c3->OutputDim(), new_dim, kCopyData);
60 }
61 
62 void WidenNnet(const NnetWidenConfig &widen_config,
63  Nnet *nnet) {
64 
65  int32 C = nnet->NumComponents();
66  int32 num_widened = 0;
67 
68  for (int32 c = 0; c < C - 3; c++) {
69  AffineComponent *c1 = dynamic_cast<AffineComponent*>(&(nnet->GetComponent(c)));
70  if (c1 == NULL) continue;
71  std::vector<NonlinearComponent*> c2; // normally just one element, but allow two right now.
72  c2.push_back(dynamic_cast<NonlinearComponent*>(&(nnet->GetComponent(c+1))));
73  if (c2.back() == NULL) continue;
74  c2.push_back(dynamic_cast<NonlinearComponent*>(&(nnet->GetComponent(c+2))));
75  AffineComponent *c3;
76  if (c2.back() == NULL) {
77  c2.pop_back();
78  c3 = dynamic_cast<AffineComponent*>(&(nnet->GetComponent(c+2)));
79  } else {
80  if (c + 3 >= C) continue;
81  c3 = dynamic_cast<AffineComponent*>(&(nnet->GetComponent(c+3)));
82  }
83  if (c3 == NULL) continue;
84  BaseFloat param_stddev = widen_config.param_stddev_factor /
85  sqrt(1.0 * c1->InputDim());
86  KALDI_LOG << "Widening component " << c << " from "
87  << c1->OutputDim() << " to " << widen_config.hidden_layer_dim;
88 
89  c1->Widen(widen_config.hidden_layer_dim,
90  param_stddev, widen_config.bias_stddev,
91  c2, c3);
92  num_widened++;
93  }
94  nnet->Check();
95  KALDI_LOG << "Widened " << num_widened << " components.";
96 }
97 
98 
99 } // namespace nnet2
100 } // namespace kaldi
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
const Component & GetComponent(int32 c) const
Definition: nnet-nnet.cc:141
CuVector< BaseFloat > bias_params_
kaldi::int32 int32
int32 NumComponents() const
Returns number of components– think of this as similar to # of layers, but e.g.
Definition: nnet-nnet.h:69
void Widen(int32 new_dimension, BaseFloat param_stddev, BaseFloat bias_stddev, std::vector< NonlinearComponent *> c2, AffineComponent *c3)
This function is implemented in widen-nnet.cc.
Definition: widen-nnet.cc:28
virtual int32 InputDim() const
Get size of input vectors.
virtual int32 OutputDim() const
Get size of output vectors.
#define KALDI_WARN
Definition: kaldi-error.h:150
Configuration class that controls neural net "widening", which means increasing the dimension of the ...
Definition: widen-nnet.h:33
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
void Check() const
Definition: nnet-nnet.cc:271
CuMatrix< BaseFloat > linear_params_
#define KALDI_LOG
Definition: kaldi-error.h:153
void WidenNnet(const NnetWidenConfig &widen_config, Nnet *nnet)
This function widens a neural network by increasing the hidden-layer dimensions to the target...
Definition: widen-nnet.cc:62