nnet-functions.cc
Go to the documentation of this file.
1 // nnet2/nnet-functions.cc
2 
3 // Copyright 2011-2012 Karel Vesely
4 // Johns Hopkins University (author: Daniel Povey)
5 
6 // See ../../COPYING for clarification regarding multiple authors
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 // http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
16 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
17 // MERCHANTABLITY OR NON-INFRINGEMENT.
18 // See the Apache 2 License for the specific language governing permissions and
19 // limitations under the License.
20 
21 #include "nnet2/nnet-nnet.h"
22 #include "util/stl-utils.h"
23 
24 namespace kaldi {
25 namespace nnet2 {
26 
28  int32 index = -1, nc = nnet.NumComponents();
29  for (int32 c = 0; c < nc; c++) {
30  const Component *component = &(nnet.GetComponent(c));
31  if (dynamic_cast<const SoftmaxComponent*>(component) != NULL) {
32  if (index != -1) return -1; // >1 softmax components.
33  else index = c;
34  }
35  }
36  return index;
37 }
38 
39 void InsertComponents(const Nnet &src_nnet,
40  int32 c_to_insert, // component-index before which to insert.
41  Nnet *dest_nnet) {
42  KALDI_ASSERT(c_to_insert >= 0 && c_to_insert <= dest_nnet->NumComponents());
43  int32 c_tot = dest_nnet->NumComponents() + src_nnet.NumComponents();
44  std::vector<Component*> components(c_tot);
45  for (int32 c = 0; c < c_to_insert; c++)
46  components[c] = dest_nnet->GetComponent(c).Copy();
47  for (int32 c = 0; c < src_nnet.NumComponents(); c++)
48  components[c + c_to_insert] = src_nnet.GetComponent(c).Copy();
49  for (int32 c = c_to_insert; c < dest_nnet->NumComponents(); c++)
50  components[c + src_nnet.NumComponents()] = dest_nnet->GetComponent(c).Copy();
51  // Re-initialize "dest_nnet" from the resulting list of components.
52 
53  // The Init method will take ownership of the pointers in the vector:
54  dest_nnet->Init(&components);
55 }
56 
57 
58 void ReplaceLastComponents(const Nnet &src_nnet,
59  int32 num_to_remove,
60  Nnet *dest_nnet) {
61  KALDI_ASSERT(num_to_remove >= 0 && num_to_remove <= dest_nnet->NumComponents());
62  int32 c_orig = dest_nnet->NumComponents() - num_to_remove;
63 
64  std::vector<Component*> components;
65  for (int32 c = 0; c < c_orig; c++)
66  components.push_back(dest_nnet->GetComponent(c).Copy());
67  for (int32 c = 0; c < src_nnet.NumComponents(); c++)
68  components.push_back(src_nnet.GetComponent(c).Copy());
69 
70  // Re-initialize "dest_nnet" from the resulting list of components.
71  // The Init method will take ownership of the pointers in the vector:
72  dest_nnet->Init(&components);
73 }
74 
75 
76 
77 } // namespace nnet2
78 } // 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
int32 IndexOfSoftmaxLayer(const Nnet &nnet)
If "nnet" has exactly one softmax layer, this function will return its index; otherwise it will retur...
Abstract class, basic element of the network, it is a box with defined inputs, outputs, and tranformation functions interface.
virtual Component * Copy() const =0
Copy component (deep copy).
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 ReplaceLastComponents(const Nnet &src_nnet, int32 num_to_remove, Nnet *dest_nnet)
Removes the last "num_to_remove" components and adds the components from "src_nnet".
void InsertComponents(const Nnet &src_nnet, int32 c_to_insert, Nnet *dest_nnet)
Inserts the components of one neural network into a particular place in the other one...
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
void Init(std::istream &is)
Initialize from config file.
Definition: nnet-nnet.cc:281