nnet-nnet.h
Go to the documentation of this file.
1 // nnet/nnet-nnet.h
2 
3 // Copyright 2011-2016 Brno University of Technology (Author: Karel Vesely)
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 #ifndef KALDI_NNET_NNET_NNET_H_
21 #define KALDI_NNET_NNET_NNET_H_
22 
23 #include <string>
24 #include <vector>
25 #include <iostream>
26 #include <sstream>
27 
28 #include "base/kaldi-common.h"
29 #include "util/kaldi-io.h"
30 #include "matrix/matrix-lib.h"
31 #include "nnet/nnet-trnopts.h"
32 #include "nnet/nnet-component.h"
33 
34 namespace kaldi {
35 namespace nnet1 {
36 
37 class Nnet {
38  public:
39  Nnet();
40  ~Nnet();
41 
42  Nnet(const Nnet& other); // Allow copy constructor.
43  Nnet& operator= (const Nnet& other); // Allow assignment operator.
44 
45  public:
47  void Propagate(const CuMatrixBase<BaseFloat> &in,
48  CuMatrix<BaseFloat> *out);
50  void Backpropagate(const CuMatrixBase<BaseFloat> &out_diff,
51  CuMatrix<BaseFloat> *in_diff);
53  void Feedforward(const CuMatrixBase<BaseFloat> &in,
54  CuMatrix<BaseFloat> *out);
55 
57  int32 InputDim() const;
59  int32 OutputDim() const;
60 
66  int32 NumComponents() const {
67  return components_.size();
68  }
69 
71  const Component& GetComponent(int32 c) const;
72 
75 
77  const Component& GetLastComponent() const;
78 
81 
83  void ReplaceComponent(int32 c, const Component& comp);
84 
86  void SwapComponent(int32 c, Component** comp);
87 
89  void AppendComponent(const Component& comp);
90 
93  void AppendComponentPointer(Component *dynamically_allocated_comp);
94 
96  void AppendNnet(const Nnet& nnet_to_append);
97 
99  void RemoveComponent(int32 c);
100 
102  void RemoveLastComponent();
103 
105  const std::vector<CuMatrix<BaseFloat> >& PropagateBuffer() const {
106  return propagate_buf_;
107  }
109  const std::vector<CuMatrix<BaseFloat> >& BackpropagateBuffer() const {
110  return backpropagate_buf_;
111  }
112 
114  int32 NumParams() const;
115 
117  void GetGradient(Vector<BaseFloat>* gradient) const;
118 
120  void GetParams(Vector<BaseFloat>* params) const;
121 
123  void SetParams(const VectorBase<BaseFloat>& params);
124 
126  void SetDropoutRate(BaseFloat r);
127 
129  void ResetStreams(const std::vector<int32> &stream_reset_flag);
130 
132  void SetSeqLengths(const std::vector<int32> &sequence_lengths);
133 
135  void Init(const std::string &proto_file);
136 
138  void Read(const std::string &rxfilename);
140  void Read(std::istream &in, bool binary);
141 
143  void Write(const std::string &wxfilename, bool binary) const;
145  void Write(std::ostream &out, bool binary) const;
146 
148  std::string Info() const;
150  std::string InfoGradient(bool header = true) const;
152  std::string InfoPropagate(bool header = true) const;
154  std::string InfoBackPropagate(bool header = true) const;
156  void Check() const;
158  void Destroy();
159 
161  void SetTrainOptions(const NnetTrainOptions& opts);
164  return opts_;
165  }
166 
167  private:
170  std::vector<Component*> components_;
171 
173  std::vector<CuMatrix<BaseFloat> > propagate_buf_;
175  std::vector<CuMatrix<BaseFloat> > backpropagate_buf_;
176 
179 };
180 
181 } // namespace nnet1
182 } // namespace kaldi
183 
184 #endif // KALDI_NNET_NNET_NNET_H_
185 
186 
void Backpropagate(const CuMatrixBase< BaseFloat > &out_diff, CuMatrix< BaseFloat > *in_diff)
Perform backward pass through the network,.
Definition: nnet-nnet.cc:96
void RemoveLastComponent()
Remove the last of the Components,.
Definition: nnet-nnet.cc:206
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void ResetStreams(const std::vector< int32 > &stream_reset_flag)
Reset streams in multi-stream training,.
Definition: nnet-nnet.cc:281
void SetParams(const VectorBase< BaseFloat > &params)
Set the network weights from a supervector,.
Definition: nnet-nnet.cc:253
void AppendComponentPointer(Component *dynamically_allocated_comp)
Append Component* to &#39;this&#39; instance of Nnet by a shallow copy (&#39;this&#39; instance of Nnet over-takes th...
Definition: nnet-nnet.cc:187
void ReplaceComponent(int32 c, const Component &comp)
Replace c&#39;th component in &#39;this&#39; Nnet (deep copy),.
Definition: nnet-nnet.cc:169
void Propagate(const CuMatrixBase< BaseFloat > &in, CuMatrix< BaseFloat > *out)
Perform forward pass through the network,.
Definition: nnet-nnet.cc:70
void GetParams(Vector< BaseFloat > *params) const
Get the network weights in a supervector,.
Definition: nnet-nnet.cc:237
int32 NumParams() const
Get the number of parameters in the network,.
Definition: nnet-nnet.cc:210
const std::vector< CuMatrix< BaseFloat > > & PropagateBuffer() const
Access to the forward-pass buffers.
Definition: nnet-nnet.h:105
void SetSeqLengths(const std::vector< int32 > &sequence_lengths)
Set sequence length in LSTM multi-stream training,.
Definition: nnet-nnet.cc:291
int32 NumComponents() const
Returns the number of &#39;Components&#39; which form the NN.
Definition: nnet-nnet.h:66
const std::vector< CuMatrix< BaseFloat > > & BackpropagateBuffer() const
Access to the backward-pass buffers.
Definition: nnet-nnet.h:109
void Write(const std::string &wxfilename, bool binary) const
Write Nnet to &#39;wxfilename&#39;,.
Definition: nnet-nnet.cc:367
int32 InputDim() const
Dimensionality on network input (input feature dim.),.
Definition: nnet-nnet.cc:148
const NnetTrainOptions & GetTrainOptions() const
Get training hyper-parameters from the network,.
Definition: nnet-nnet.h:163
kaldi::int32 int32
This class represents a matrix that&#39;s stored on the GPU if we have one, and in memory if not...
Definition: matrix-common.h:71
const Component & GetLastComponent() const
LastComponent accessor,.
Definition: nnet-nnet.cc:161
void GetGradient(Vector< BaseFloat > *gradient) const
Get the gradient stored in the network,.
Definition: nnet-nnet.cc:221
int32 OutputDim() const
Dimensionality of network outputs (posteriors | bn-features | etc.),.
Definition: nnet-nnet.cc:143
void SwapComponent(int32 c, Component **comp)
Swap c&#39;th component with the pointer,.
Definition: nnet-nnet.cc:175
std::string InfoBackPropagate(bool header=true) const
Create string with back-propagation-buffer statistics,.
Definition: nnet-nnet.cc:443
void AppendNnet(const Nnet &nnet_to_append)
Append other Nnet to the &#39;this&#39; Nnet (copy all its components),.
Definition: nnet-nnet.cc:192
void Read(const std::string &rxfilename)
Read Nnet from &#39;rxfilename&#39;,.
Definition: nnet-nnet.cc:333
void Check() const
Consistency check,.
Definition: nnet-nnet.cc:467
void Destroy()
Relese the memory,.
Definition: nnet-nnet.cc:498
std::vector< CuMatrix< BaseFloat > > propagate_buf_
Buffers for forward pass (on demand initialization),.
Definition: nnet-nnet.h:173
void Feedforward(const CuMatrixBase< BaseFloat > &in, CuMatrix< BaseFloat > *out)
Perform forward pass through the network (with 2 swapping buffers),.
Definition: nnet-nnet.cc:131
std::vector< Component * > components_
Vector which contains all the components composing the neural network, the components are for example...
Definition: nnet-nnet.h:170
Matrix for CUDA computing.
Definition: matrix-common.h:69
void Init(const std::string &proto_file)
Initialize the Nnet from the prototype,.
Definition: nnet-nnet.cc:301
A class representing a vector.
Definition: kaldi-vector.h:406
void SetDropoutRate(BaseFloat r)
Set the dropout rate.
Definition: nnet-nnet.cc:268
std::string InfoGradient(bool header=true) const
Create string with per-component gradient statistics,.
Definition: nnet-nnet.cc:407
std::string InfoPropagate(bool header=true) const
Create string with propagation-buffer statistics,.
Definition: nnet-nnet.cc:420
std::string Info() const
Create string with human readable description of the nnet,.
Definition: nnet-nnet.cc:386
void RemoveComponent(int32 c)
Remove c&#39;th component,.
Definition: nnet-nnet.cc:199
const Component & GetComponent(int32 c) const
Component accessor,.
Definition: nnet-nnet.cc:153
void SetTrainOptions(const NnetTrainOptions &opts)
Set hyper-parameters of the training (pushes to all UpdatableComponents),.
Definition: nnet-nnet.cc:508
Abstract class, building block of the network.
NnetTrainOptions opts_
Option class with hyper-parameters passed to UpdatableComponent(s)
Definition: nnet-nnet.h:178
std::vector< CuMatrix< BaseFloat > > backpropagate_buf_
Buffers for backward pass (on demand initialization),.
Definition: nnet-nnet.h:175
Provides a vector abstraction class.
Definition: kaldi-vector.h:41
void AppendComponent(const Component &comp)
Append Component to &#39;this&#39; instance of Nnet (deep copy),.
Definition: nnet-nnet.cc:182
Nnet & operator=(const Nnet &other)
Definition: nnet-nnet.cc:51