nnet-randomizer-test.cc File Reference
#include "nnet/nnet-randomizer.h"
#include <numeric>
#include <vector>
#include <algorithm>
Include dependency graph for nnet-randomizer-test.cc:

Go to the source code of this file.

Functions

template<class Real >
static void InitRand (VectorBase< Real > *v)
 
template<class Real >
static void InitRand (MatrixBase< Real > *M)
 
template<class Real >
static void AssertEqual (const VectorBase< Real > &A, const VectorBase< Real > &B, float tol=0.001)
 
template<class RandomAccessIterator >
static void AssertEqual (RandomAccessIterator begin1, RandomAccessIterator end1, RandomAccessIterator begin2, RandomAccessIterator end2)
 
void UnitTestRandomizerMask ()
 
void UnitTestMatrixRandomizer ()
 
void UnitTestVectorRandomizer ()
 
void UnitTestStdVectorRandomizer ()
 
int main ()
 

Function Documentation

◆ AssertEqual() [1/2]

static void AssertEqual ( const VectorBase< Real > &  A,
const VectorBase< Real > &  B,
float  tol = 0.001 
)
static

Definition at line 48 of file nnet-randomizer-test.cc.

References VectorBase< Real >::Dim(), rnnlm::i, and KALDI_ASSERT.

50  {
51  KALDI_ASSERT(A.Dim() == B.Dim());
52  for (MatrixIndexT i = 0; i < A.Dim(); i++) {
53  KALDI_ASSERT(std::abs(A(i)-B(i)) < tol);
54  }
55 }
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:64
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ AssertEqual() [2/2]

static void AssertEqual ( RandomAccessIterator  begin1,
RandomAccessIterator  end1,
RandomAccessIterator  begin2,
RandomAccessIterator  end2 
)
static

Definition at line 59 of file nnet-randomizer-test.cc.

References KALDI_ASSERT.

60  {
61  KALDI_ASSERT((end1 - begin1) == (end2 - begin2));
62  KALDI_ASSERT(end1 > begin1);
63  for ( ; begin1 < end1; ++begin1, ++begin2) {
64  KALDI_ASSERT(*begin1 == *begin2);
65  }
66 }
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ InitRand() [1/2]

static void InitRand ( VectorBase< Real > *  v)
static

Definition at line 32 of file nnet-randomizer-test.cc.

References VectorBase< Real >::Dim(), rnnlm::i, and kaldi::nnet1::RandGauss().

32  {
33  for (MatrixIndexT i = 0;i < v->Dim();i++)
34  (*v)(i) = RandGauss();
35 }
float RandGauss(struct RandomState *state=NULL)
Definition: kaldi-math.h:155
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:64

◆ InitRand() [2/2]

static void InitRand ( MatrixBase< Real > *  M)
static

Definition at line 38 of file nnet-randomizer-test.cc.

References MatrixBase< Real >::Cond(), rnnlm::i, rnnlm::j, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), and kaldi::nnet1::RandGauss().

38  {
39  do {
40  for (MatrixIndexT i = 0;i < M->NumRows();i++)
41  for (MatrixIndexT j = 0;j < M->NumCols();j++)
42  (*M)(i, j) = RandGauss();
43  } while (M->NumRows() != 0 && M->Cond() > 100);
44 }
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
Real Cond() const
Returns condition number by computing Svd.
float RandGauss(struct RandomState *state=NULL)
Definition: kaldi-math.h:155
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64

◆ main()

int main ( )

Definition at line 232 of file nnet-randomizer-test.cc.

References UnitTestMatrixRandomizer(), UnitTestRandomizerMask(), UnitTestStdVectorRandomizer(), and UnitTestVectorRandomizer().

232  {
237 
238  std::cout << "Tests succeeded.\n";
239 }
void UnitTestMatrixRandomizer()
void UnitTestVectorRandomizer()
void UnitTestRandomizerMask()
void UnitTestStdVectorRandomizer()

◆ UnitTestMatrixRandomizer()

void UnitTestMatrixRandomizer ( )

Definition at line 81 of file nnet-randomizer-test.cc.

References MatrixRandomizer::AddData(), kaldi::AssertEqual(), CuMatrixBase< Real >::CopyToMat(), MatrixRandomizer::Done(), rnnlm::i, MatrixRandomizer::Init(), kaldi::InitRand(), MatrixRandomizer::IsFull(), KALDI_ASSERT, KALDI_LOG, NnetDataRandomizerOptions::minibatch_size, MatrixRandomizer::Next(), CuMatrixBase< Real >::NumCols(), MatrixRandomizer::NumFrames(), CuMatrixBase< Real >::NumRows(), MatrixRandomizer::Randomize(), NnetDataRandomizerOptions::randomizer_size, MatrixBase< Real >::RowRange(), and MatrixRandomizer::Value().

Referenced by main().

81  {
82  Matrix<BaseFloat> m(1111, 10);
83  InitRand(&m);
84  CuMatrix<BaseFloat> m2(m);
85  // config
87  c.randomizer_size = 1000;
88  c.minibatch_size = 100;
89  // randomizer
91  r.Init(c);
92  r.AddData(m2);
93  KALDI_ASSERT(r.IsFull());
94  // create vector with consecutive indices
95  std::vector<int32> mask(1111);
96  for (int32 i = 0; i < 1111; i++) {
97  mask[i] = i;
98  }
99  r.Randomize(mask); // no shuffling
100  // make sure we get same data we put to randomizer
101  int32 i=0;
102  for ( ; !r.Done(); r.Next(), i++) {
103  KALDI_LOG << i;
104  const CuMatrixBase<BaseFloat> &m3 = r.Value();
105  Matrix<BaseFloat> m4(m3.NumRows(), m3.NumCols());
106  m3.CopyToMat(&m4);
107  AssertEqual(m4, m.RowRange(i * c.minibatch_size, c.minibatch_size));
108  }
109  KALDI_ASSERT(i == 11); // 11 minibatches
110 
111  KALDI_LOG << "Filling for 2nd time";
112  // try to fill buffer one more time, and empty it
113  KALDI_ASSERT(!r.IsFull());
114  r.AddData(m2);
115  KALDI_ASSERT(r.IsFull());
116  KALDI_ASSERT(r.NumFrames() == 11 + 1111);
117  { // check last 11 rows were copied to the front in the buffer
118  const CuMatrixBase<BaseFloat> &m3 = r.Value();
119  Matrix<BaseFloat> m4(m3.NumRows(), m3.NumCols());
120  m3.CopyToMat(&m4);
121  AssertEqual(m4.RowRange(0, 11), m.RowRange(1100, 11));
122  }
123  KALDI_ASSERT(!r.Done());
124  for ( ; !r.Done(); r.Next(), i++) {
125  KALDI_LOG << i;
126  const CuMatrixBase<BaseFloat>& m3 = r.Value();
127  static_cast<const void>(m3); // variable no longer unused,
128  }
129  KALDI_ASSERT(i == 22); // 22 minibatches
130 }
void CopyToMat(MatrixBase< OtherReal > *dst, MatrixTransposeType trans=kNoTrans) const
Definition: cu-matrix.cc:447
void Next()
Sets cursor to next mini-batch.
bool Done()
Returns true, if no more data for another mini-batch (after current one)
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 randomizer_size
Maximum number of samples we have in memory,.
int32 NumFrames()
Number of frames stored inside the Randomizer.
Configuration variables that affect how frame-level shuffling is done.
void Init(const NnetDataRandomizerOptions &conf)
Set the randomizer parameters (size)
static void InitRand(VectorBase< Real > *v)
static void AssertEqual(const VectorBase< Real > &A, const VectorBase< Real > &B, float tol=0.001)
void Randomize(const std::vector< int32 > &mask)
Randomize matrix row-order using mask.
const CuMatrixBase< BaseFloat > & Value()
Returns matrix-window with next mini-batch.
Shuffles rows of a matrix according to the indices in the mask,.
Matrix for CUDA computing.
Definition: matrix-common.h:69
MatrixIndexT NumCols() const
Definition: cu-matrix.h:216
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
MatrixIndexT NumRows() const
Dimensions.
Definition: cu-matrix.h:215
#define KALDI_LOG
Definition: kaldi-error.h:153
void AddData(const CuMatrixBase< BaseFloat > &m)
Add data to randomization buffer.
bool IsFull()
Returns true, when capacity is full.

◆ UnitTestRandomizerMask()

void UnitTestRandomizerMask ( )

Definition at line 71 of file nnet-randomizer-test.cc.

References RandomizerMask::Generate(), RandomizerMask::Init(), and KALDI_ASSERT.

Referenced by main().

71  {
74  r.Init(c);
75  const std::vector<int32>& m = r.Generate(5);
76  KALDI_ASSERT(m.size() == 5);
77  int32 sum_of_elems = std::accumulate(m.begin(), m.end(),0);
78  KALDI_ASSERT(sum_of_elems == 4 + 3 + 2 + 1 + 0);
79 }
Generates randomly ordered vector of indices,.
kaldi::int32 int32
Configuration variables that affect how frame-level shuffling is done.
void Init(const NnetDataRandomizerOptions &conf)
Init, call srand,.
const std::vector< int32 > & Generate(int32 mask_size)
Generate randomly ordered vector of integers 0..[mask_size -1],.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ UnitTestStdVectorRandomizer()

void UnitTestStdVectorRandomizer ( )

Definition at line 178 of file nnet-randomizer-test.cc.

References StdVectorRandomizer< T >::AddData(), kaldi::AssertEqual(), StdVectorRandomizer< T >::Done(), rnnlm::i, StdVectorRandomizer< T >::Init(), StdVectorRandomizer< T >::IsFull(), KALDI_ASSERT, KALDI_LOG, StdVectorRandomizer< T >::Next(), StdVectorRandomizer< T >::NumFrames(), StdVectorRandomizer< T >::Randomize(), NnetDataRandomizerOptions::randomizer_size, and StdVectorRandomizer< T >::Value().

Referenced by main().

178  {
179  // prepare vector with some data,
180  std::vector<int32> v(1111);
181  for (int32 i = 0; i < v.size(); i++) {
182  v.at(i) = i;
183  }
184  std::random_shuffle(v.begin(), v.end());
185 
186  // config
188  c.randomizer_size = 1000;
189  c.minibatch_size = 100;
190  // randomizer
192  r.Init(c);
193  r.AddData(v);
194  KALDI_ASSERT(r.IsFull());
195  // create vector with consecutive indices
196  std::vector<int32> mask(1111);
197  for (int32 i = 0; i < 1111; i++) {
198  mask[i]=i;
199  }
200  r.Randomize(mask); // no shuffling
201  // make sure we get same data we put to randomizer
202  int32 i = 0;
203  for ( ; !r.Done(); r.Next(), i++) {
204  KALDI_LOG << i;
205  std::vector<int32> v2 = r.Value();
206  AssertEqual(v2.begin(),
207  v2.end(),
208  v.begin() + (i * c.minibatch_size),
209  v.begin() + ((i+1) * c.minibatch_size));
210  }
211  KALDI_ASSERT(i == 11); // 11 minibatches
212 
213  KALDI_LOG << "Filling for 2nd time";
214  // try to fill buffer one more time, and empty it
215  KALDI_ASSERT(!r.IsFull());
216  r.AddData(v);
217  KALDI_ASSERT(r.IsFull());
218  KALDI_ASSERT(r.NumFrames() == 11 + 1111);
219  { // check last 11 rows were copied to the front in the buffer
220  std::vector<int32> v2 = r.Value();
221  AssertEqual(v2.begin(), v2.begin()+11, v.begin()+1100, v.begin()+1100+11);
222  }
223  KALDI_ASSERT(!r.Done());
224  for ( ; !r.Done(); r.Next(), i++) {
225  KALDI_LOG << i;
226  std::vector<int32> v2 = r.Value();
227  }
228  KALDI_ASSERT(i == 22); // 22 minibatches
229 }
bool Done()
Returns true, if no more data for another mini-batch (after current one)
kaldi::int32 int32
void Init(const NnetDataRandomizerOptions &conf)
Set the randomizer parameters (size)
int32 randomizer_size
Maximum number of samples we have in memory,.
const std::vector< T > & Value()
Returns matrix-window with next mini-batch.
void Next()
Sets cursor to next mini-batch.
Configuration variables that affect how frame-level shuffling is done.
static void AssertEqual(const VectorBase< Real > &A, const VectorBase< Real > &B, float tol=0.001)
int32 NumFrames()
Number of frames stored inside the Randomizer.
void Randomize(const std::vector< int32 > &mask)
Randomize matrix row-order using mask.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
bool IsFull()
Returns true, when capacity is full.
void AddData(const std::vector< T > &v)
Add data to randomization buffer.
Randomizes elements of a vector according to a mask.
#define KALDI_LOG
Definition: kaldi-error.h:153

◆ UnitTestVectorRandomizer()

void UnitTestVectorRandomizer ( )

Definition at line 132 of file nnet-randomizer-test.cc.

References VectorRandomizer::AddData(), kaldi::AssertEqual(), VectorRandomizer::Done(), rnnlm::i, VectorRandomizer::Init(), kaldi::InitRand(), VectorRandomizer::IsFull(), KALDI_ASSERT, KALDI_LOG, NnetDataRandomizerOptions::minibatch_size, VectorRandomizer::Next(), VectorRandomizer::NumFrames(), VectorRandomizer::Randomize(), NnetDataRandomizerOptions::randomizer_size, VectorBase< Real >::Range(), and VectorRandomizer::Value().

Referenced by main().

132  {
133  Vector<BaseFloat> v(1111);
134  InitRand(&v);
135  // config
137  c.randomizer_size = 1000;
138  c.minibatch_size = 100;
139  // randomizer
141  r.Init(c);
142  r.AddData(v);
143  KALDI_ASSERT(r.IsFull());
144  // create vector with consecutive indices
145  std::vector<int32> mask(1111);
146  for (int32 i = 0; i < 1111; i++) {
147  mask[i] = i;
148  }
149  r.Randomize(mask); // no shuffling
150  // make sure we get same data we put to randomizer
151  int32 i = 0;
152  for ( ; !r.Done(); r.Next(), i++) {
153  KALDI_LOG << i;
154  const VectorBase<BaseFloat> &v2 = r.Value();
156  }
157  KALDI_ASSERT(i == 11); // 11 minibatches
158 
159  KALDI_LOG << "Filling for 2nd time";
160  // try to fill buffer one more time, and empty it
161  KALDI_ASSERT(!r.IsFull());
162  r.AddData(v);
163  KALDI_ASSERT(r.IsFull());
164  KALDI_ASSERT(r.NumFrames() == 11 + 1111);
165  { // check last 11 rows were copied to the front in the buffer
166  const VectorBase<BaseFloat> &v2 = r.Value();
167  AssertEqual(v2.Range(0, 11), v.Range(1100, 11));
168  }
169  KALDI_ASSERT(!r.Done());
170  for ( ; !r.Done(); r.Next(), i++) {
171  KALDI_LOG << i;
172  const VectorBase<BaseFloat>& v2 = r.Value();
173  static_cast<const void>(v2); // variable no longer unused,
174  }
175  KALDI_ASSERT(i == 22); // 22 minibatches
176 }
bool IsFull()
Returns true, when capacity is full.
void Next()
Sets cursor to next mini-batch.
Randomizes elements of a vector according to a mask.
kaldi::int32 int32
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,.
int32 NumFrames()
Number of frames stored inside the Randomizer.
void Randomize(const std::vector< int32 > &mask)
Randomize matrix row-order using mask.
void AddData(const Vector< BaseFloat > &v)
Add data to randomization buffer.
Configuration variables that affect how frame-level shuffling is done.
static void InitRand(VectorBase< Real > *v)
static void AssertEqual(const VectorBase< Real > &A, const VectorBase< Real > &B, float tol=0.001)
A class representing a vector.
Definition: kaldi-vector.h:406
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
void Init(const NnetDataRandomizerOptions &conf)
Set the randomizer parameters (size)
Provides a vector abstraction class.
Definition: kaldi-vector.h:41
#define KALDI_LOG
Definition: kaldi-error.h:153
const Vector< BaseFloat > & Value()
Returns matrix-window with next mini-batch.
SubVector< Real > Range(const MatrixIndexT o, const MatrixIndexT l)
Returns a sub-vector of a vector (a range of elements).
Definition: kaldi-vector.h:94