nnet-randomizer.h
Go to the documentation of this file.
1 // nnet/nnet-randomizer.h
2 
3 // Copyright 2013 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 
21 #ifndef KALDI_NNET_NNET_RANDOMIZER_H_
22 #define KALDI_NNET_NNET_RANDOMIZER_H_
23 
24 #include <utility>
25 #include <vector>
26 
27 #include "base/kaldi-math.h"
28 #include "itf/options-itf.h"
29 #include "cudamatrix/cu-matrix.h"
30 #include "cudamatrix/cu-math.h"
31 
32 namespace kaldi {
33 namespace nnet1 {
34 
42 
44  randomizer_size(32768),
45  randomizer_seed(777),
46  minibatch_size(256)
47  { }
48 
49  void Register(OptionsItf *opts) {
50  opts->Register("randomizer-size", &randomizer_size,
51  "Capacity of randomizer, length of concatenated utterances which, "
52  "are used for frame-level shuffling (in frames, affects memory "
53  "consumption, max 8000000).");
54  opts->Register("randomizer-seed", &randomizer_seed,
55  "Seed value for srand, sets fixed order of frame-level shuffling");
56  opts->Register("minibatch-size", &minibatch_size, "Size of a minibatch.");
57  }
58 };
59 
60 
65  public:
67  { }
68 
69  explicit RandomizerMask(const NnetDataRandomizerOptions &conf) {
70  Init(conf);
71  }
72 
74  void Init(const NnetDataRandomizerOptions& conf);
75 
77  const std::vector<int32>& Generate(int32 mask_size);
78 
79  private:
80  std::vector<int32> mask_;
81 };
82 
83 
88  public:
90  data_begin_(0),
91  data_end_(0)
92  { }
93 
95  data_begin_(0),
96  data_end_(0)
97  {
98  Init(conf);
99  }
100 
102  void Init(const NnetDataRandomizerOptions& conf) {
103  conf_ = conf;
104  }
105 
107  void AddData(const CuMatrixBase<BaseFloat>& m);
108 
110  bool IsFull() {
111  return ((data_begin_ == 0) && (data_end_ > conf_.randomizer_size ));
112  }
113 
116  return data_end_;
117  }
118 
120  void Randomize(const std::vector<int32>& mask);
121 
123  bool Done() {
124  return (data_end_ - data_begin_ < conf_.minibatch_size);
125  }
126 
128  void Next();
129 
131  const CuMatrixBase<BaseFloat>& Value();
132 
133  private:
134  CuMatrix<BaseFloat> data_; // can be larger than 'randomizer_size'
135  CuMatrix<BaseFloat> data_aux_; // auxiliary buffer for shuffling
136  CuMatrix<BaseFloat> minibatch_; // buffer for mini-batch
137 
142 
144 };
145 
146 
149  public:
151  data_begin_(0),
152  data_end_(0)
153  { }
154 
156  data_begin_(0),
157  data_end_(0)
158  {
159  Init(conf);
160  }
161 
163  void Init(const NnetDataRandomizerOptions& conf) {
164  conf_ = conf;
165  }
166 
168  void AddData(const Vector<BaseFloat>& v);
169 
171  bool IsFull() {
172  return ((data_begin_ == 0) && (data_end_ > conf_.randomizer_size ));
173  }
174 
177  return data_end_;
178  }
179 
181  void Randomize(const std::vector<int32>& mask);
182 
184  bool Done() {
185  return (data_end_ - data_begin_ < conf_.minibatch_size);
186  }
187 
189  void Next();
190 
192  const Vector<BaseFloat>& Value();
193 
194  private:
195  Vector<BaseFloat> data_; // can be larger than 'randomizer_size'
196  Vector<BaseFloat> minibatch_; // buffer for mini-batch
197 
202 
204 };
205 
206 
208 template<typename T>
210  public:
212  data_begin_(0),
213  data_end_(0)
214  { }
215 
217  data_begin_(0),
218  data_end_(0)
219  {
220  Init(conf);
221  }
222 
224  void Init(const NnetDataRandomizerOptions& conf) {
225  conf_ = conf;
226  }
227 
229  void AddData(const std::vector<T>& v);
230 
232  bool IsFull() {
233  return ((data_begin_ == 0) && (data_end_ > conf_.randomizer_size ));
234  }
235 
238  return data_end_;
239  }
240 
242  void Randomize(const std::vector<int32>& mask);
243 
245  bool Done() {
246  return (data_end_ - data_begin_ < conf_.minibatch_size);
247  }
248 
250  void Next();
251 
253  const std::vector<T>& Value();
254 
255  private:
256  std::vector<T> data_; // can be larger than 'randomizer_size'
257  std::vector<T> minibatch_; // buffer for mini-batch
258 
263 
265 };
266 
269 
270 
271 } // namespace nnet1
272 } // namespace kaldi
273 
274 #endif // KALDI_NNET_NNET_RANDOMIZER_H_
StdVectorRandomizer< std::vector< std::pair< int32, BaseFloat > > > PosteriorRandomizer
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
int32 data_begin_
A cursor, pointing to the &#39;row&#39; where the next mini-batch begins,.
int32 data_end_
A cursor, pointing to the &#39;row&#39; after the end of data,.
bool IsFull()
Returns true, when capacity is full.
NnetDataRandomizerOptions conf_
MatrixRandomizer(const NnetDataRandomizerOptions &conf)
bool Done()
Returns true, if no more data for another mini-batch (after current one)
NnetDataRandomizerOptions conf_
int32 data_end_
A cursor, pointing to the &#39;row&#39; after the end of data,.
void Randomize(const CuMatrixBase< Real > &src, const CuArray< int32 > &copy_from_idx, CuMatrixBase< Real > *tgt)
Copies a permutation of src into tgt.
Definition: cu-math.cc:80
Generates randomly ordered vector of indices,.
int32 data_begin_
A cursor, pointing to the &#39;row&#39; where the next mini-batch begins,.
bool Done()
Returns true, if no more data for another mini-batch (after current one)
Randomizes elements of a vector according to a mask.
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
int32 data_begin_
A cursor, pointing to the &#39;row&#39; where the next mini-batch begins,.
void Init(const NnetDataRandomizerOptions &conf)
Set the randomizer parameters (size)
StdVectorRandomizer(const NnetDataRandomizerOptions &conf)
VectorRandomizer(const NnetDataRandomizerOptions &conf)
bool Done()
Returns true, if no more data for another mini-batch (after current one)
int32 randomizer_size
Maximum number of samples we have in memory,.
virtual void Register(const std::string &name, bool *ptr, const std::string &doc)=0
int32 NumFrames()
Number of frames stored inside the Randomizer.
int32 NumFrames()
Number of frames stored inside the Randomizer.
std::vector< int32 > mask_
CuMatrix< BaseFloat > data_
Configuration variables that affect how frame-level shuffling is done.
RandomizerMask(const NnetDataRandomizerOptions &conf)
Vector< BaseFloat > minibatch_
void Init(const NnetDataRandomizerOptions &conf)
Set the randomizer parameters (size)
StdVectorRandomizer< int32 > Int32VectorRandomizer
int32 NumFrames()
Number of frames stored inside the Randomizer.
int32 data_end_
A cursor, pointing to the &#39;row&#39; after the end of data,.
NnetDataRandomizerOptions conf_
Shuffles rows of a matrix according to the indices in the mask,.
Matrix for CUDA computing.
Definition: matrix-common.h:69
A class representing a vector.
Definition: kaldi-vector.h:406
bool IsFull()
Returns true, when capacity is full.
CuMatrix< BaseFloat > data_aux_
void Init(const NnetDataRandomizerOptions &conf)
Set the randomizer parameters (size)
Randomizes elements of a vector according to a mask.
bool IsFull()
Returns true, when capacity is full.
CuMatrix< BaseFloat > minibatch_