nnet-utils-test.cc
Go to the documentation of this file.
1 // nnet3/nnet-utils-test.cc
2 
3 // Copyright 2015 Johns Hopkins University (author: Daniel Povey)
4 // 2016 Daniel Galvez
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 "nnet3/nnet-nnet.h"
23 #include "nnet3/nnet-test-utils.h"
24 
25 namespace kaldi {
26 namespace nnet3 {
27 
28 
30  for (int32 n = 0; n < 20; n++) {
31  struct NnetGenerationOptions gen_config;
32 
33  std::vector<std::string> configs;
34  GenerateConfigSequence(gen_config, &configs);
35  Nnet nnet;
36  std::istringstream is(configs[0]);
37  nnet.ReadConfig(is);
38 
39  // this test doesn't really test anything except that it runs;
40  // we manually inspect the output.
41  int32 left_context, right_context;
42  ComputeSimpleNnetContext(nnet, &left_context, &right_context);
43  KALDI_LOG << "Left,right-context= " << left_context << ","
44  << right_context << " for config: " << configs[0];
45 
46  KALDI_LOG << "Info for nnet is: " << NnetInfo(nnet);
47  }
48 }
49 
51  // a test without a composite component.
52  std::string config =
53  "component name=repeated-affine1 type=RepeatedAffineComponent "
54  "input-dim=100 output-dim=200 num-repeats=20\n"
55  "component name=relu1 type=RectifiedLinearComponent dim=200\n"
56  "component name=block-affine1 type=BlockAffineComponent "
57  "input-dim=200 output-dim=100 num-blocks=10\n"
58  "component name=relu2 type=RectifiedLinearComponent dim=100\n"
59  "component name=repeated-affine2 type=NaturalGradientRepeatedAffineComponent "
60  "input-dim=100 output-dim=200 num-repeats=10\n"
61  "\n"
62  "input-node name=input dim=100\n"
63  "component-node name=repeated-affine1 component=repeated-affine1 input=input\n"
64  "component-node name=relu1 component=relu1 input=repeated-affine1\n"
65  "component-node name=block-affine1 component=block-affine1 input=relu1\n"
66  "component-node name=relu2 component=relu2 component=relu2 input=block-affine1\n"
67  "component-node name=repeated-affine2 component=repeated-affine2 input=relu2\n"
68  "output-node name=output input=repeated-affine2\n";
69 
70  Nnet nnet;
71  std::istringstream is(config);
72  nnet.ReadConfig(is);
74 
75  for(int i = 0; i < nnet.NumComponents(); i++) {
76  Component *c = nnet.GetComponent(i);
77  KALDI_ASSERT(c->Type() != "RepeatedAffineComponent"
78  && c->Type() != "NaturalGradientRepeatedAffineComponent");
79  }
80 }
81 
83  // test that repeated affine components nested within a CompositeComponent
84  // are converted.
85  struct NnetGenerationOptions gen_config;
86  gen_config.output_dim = 0;
87  std::vector<std::string> configs;
88  // this function generates a neural net with one component:
89  // a composite component.
90  GenerateConfigSequenceCompositeBlock(gen_config, &configs);
91  Nnet nnet;
92  std::istringstream is(configs[0]);
93  nnet.ReadConfig(is);
94  KALDI_ASSERT(nnet.NumComponents() == 1);
96  CompositeComponent *cc = dynamic_cast<CompositeComponent*>(nnet.GetComponent(0));
97  for(int i = 0; i < cc->NumComponents(); i++) {
98  const Component *c = cc->GetComponent(i);
99  KALDI_ASSERT(c->Type() == "BlockAffineComponent");
100  }
101 }
102 
103 } // namespace nnet3
104 } // namespace kaldi
105 
106 int main() {
107  using namespace kaldi;
108  using namespace kaldi::nnet3;
109  SetVerboseLevel(2);
110 
114 
115  KALDI_LOG << "Nnet tests succeeded.";
116 
117  return 0;
118 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void ReadConfig(std::istream &config_file)
Definition: nnet-nnet.cc:189
Abstract base-class for neural-net components.
void UnitTestConvertRepeatedToBlockAffine()
This file contains various routines that are useful in test code.
kaldi::int32 int32
CompositeComponent is a component representing a sequence of [simple] components. ...
void ConvertRepeatedToBlockAffine(CompositeComponent *c_component)
Definition: nnet-utils.cc:447
This file contains declarations of components that are "simple", meaning they don&#39;t care about the in...
void SetVerboseLevel(int32 i)
This should be rarely used, except by programs using Kaldi as library; command-line programs set the ...
Definition: kaldi-error.h:64
void ComputeSimpleNnetContext(const Nnet &nnet, int32 *left_context, int32 *right_context)
ComputeSimpleNnetContext computes the left-context and right-context of a nnet.
Definition: nnet-utils.cc:146
void UnitTestConvertRepeatedToBlockAffineComposite()
struct rnnlm::@11::@12 n
std::string NnetInfo(const Nnet &nnet)
This function returns various info about the neural net.
Definition: nnet-utils.cc:492
void UnitTestNnetContext()
Component * GetComponent(int32 c)
Return component indexed c. Not a copy; not owned by caller.
Definition: nnet-nnet.cc:150
const Component * GetComponent(int32 i) const
Gets the ith component in this component.
virtual std::string Type() const =0
Returns a string such as "SigmoidComponent", describing the type of the object.
int32 NumComponents() const
Definition: nnet-nnet.h:124
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
int main()
void GenerateConfigSequence(const NnetGenerationOptions &opts, std::vector< std::string > *configs)
Generates a sequence of at least one config files, output as strings, where the first in the sequence...
#define KALDI_LOG
Definition: kaldi-error.h:153
void GenerateConfigSequenceCompositeBlock(const NnetGenerationOptions &opts, std::vector< std::string > *configs)
Generate a config string with a composite component composed only of block affine, repeated affine, and natural gradient repeated affine components.