Blame view
src/nnet/nnet-randomizer-test.cc
6.88 KB
8dcb6dfcb first commit |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
// nnet/nnet-randomizer-test.cc // Copyright 2013 Brno University of Technology (author: Karel Vesely) // See ../../COPYING for clarification regarding multiple authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, // MERCHANTABLITY OR NON-INFRINGEMENT. // See the Apache 2 License for the specific language governing permissions and // limitations under the License. #include "nnet/nnet-randomizer.h" #include <numeric> #include <vector> #include <algorithm> using namespace kaldi; using namespace kaldi::nnet1; ////////////////////////////////////////////////// template<class Real> static void InitRand(VectorBase<Real> *v) { for (MatrixIndexT i = 0;i < v->Dim();i++) (*v)(i) = RandGauss(); } template<class Real> static void InitRand(MatrixBase<Real> *M) { do { for (MatrixIndexT i = 0;i < M->NumRows();i++) for (MatrixIndexT j = 0;j < M->NumCols();j++) (*M)(i, j) = RandGauss(); } while (M->NumRows() != 0 && M->Cond() > 100); } template<class Real> static void AssertEqual(const VectorBase<Real> &A, const VectorBase<Real> &B, float tol = 0.001) { KALDI_ASSERT(A.Dim() == B.Dim()); for (MatrixIndexT i = 0; i < A.Dim(); i++) { KALDI_ASSERT(std::abs(A(i)-B(i)) < tol); } } template<class RandomAccessIterator> static void AssertEqual(RandomAccessIterator begin1, RandomAccessIterator end1, RandomAccessIterator begin2, RandomAccessIterator end2) { KALDI_ASSERT((end1 - begin1) == (end2 - begin2)); KALDI_ASSERT(end1 > begin1); for ( ; begin1 < end1; ++begin1, ++begin2) { KALDI_ASSERT(*begin1 == *begin2); } } ////////////////////////////////////////////////// void UnitTestRandomizerMask() { NnetDataRandomizerOptions c; RandomizerMask r; r.Init(c); const std::vector<int32>& m = r.Generate(5); KALDI_ASSERT(m.size() == 5); int32 sum_of_elems = std::accumulate(m.begin(), m.end(),0); KALDI_ASSERT(sum_of_elems == 4 + 3 + 2 + 1 + 0); } void UnitTestMatrixRandomizer() { Matrix<BaseFloat> m(1111, 10); InitRand(&m); CuMatrix<BaseFloat> m2(m); // config NnetDataRandomizerOptions c; c.randomizer_size = 1000; c.minibatch_size = 100; // randomizer MatrixRandomizer r; r.Init(c); r.AddData(m2); KALDI_ASSERT(r.IsFull()); // create vector with consecutive indices std::vector<int32> mask(1111); for (int32 i = 0; i < 1111; i++) { mask[i] = i; } r.Randomize(mask); // no shuffling // make sure we get same data we put to randomizer int32 i=0; for ( ; !r.Done(); r.Next(), i++) { KALDI_LOG << i; const CuMatrixBase<BaseFloat> &m3 = r.Value(); Matrix<BaseFloat> m4(m3.NumRows(), m3.NumCols()); m3.CopyToMat(&m4); AssertEqual(m4, m.RowRange(i * c.minibatch_size, c.minibatch_size)); } KALDI_ASSERT(i == 11); // 11 minibatches KALDI_LOG << "Filling for 2nd time"; // try to fill buffer one more time, and empty it KALDI_ASSERT(!r.IsFull()); r.AddData(m2); KALDI_ASSERT(r.IsFull()); KALDI_ASSERT(r.NumFrames() == 11 + 1111); { // check last 11 rows were copied to the front in the buffer const CuMatrixBase<BaseFloat> &m3 = r.Value(); Matrix<BaseFloat> m4(m3.NumRows(), m3.NumCols()); m3.CopyToMat(&m4); AssertEqual(m4.RowRange(0, 11), m.RowRange(1100, 11)); } KALDI_ASSERT(!r.Done()); for ( ; !r.Done(); r.Next(), i++) { KALDI_LOG << i; const CuMatrixBase<BaseFloat>& m3 = r.Value(); static_cast<const void>(m3); // variable no longer unused, } KALDI_ASSERT(i == 22); // 22 minibatches } void UnitTestVectorRandomizer() { Vector<BaseFloat> v(1111); InitRand(&v); // config NnetDataRandomizerOptions c; c.randomizer_size = 1000; c.minibatch_size = 100; // randomizer VectorRandomizer r; r.Init(c); r.AddData(v); KALDI_ASSERT(r.IsFull()); // create vector with consecutive indices std::vector<int32> mask(1111); for (int32 i = 0; i < 1111; i++) { mask[i] = i; } r.Randomize(mask); // no shuffling // make sure we get same data we put to randomizer int32 i = 0; for ( ; !r.Done(); r.Next(), i++) { KALDI_LOG << i; const VectorBase<BaseFloat> &v2 = r.Value(); AssertEqual(v2, v.Range(i * c.minibatch_size, c.minibatch_size)); } KALDI_ASSERT(i == 11); // 11 minibatches KALDI_LOG << "Filling for 2nd time"; // try to fill buffer one more time, and empty it KALDI_ASSERT(!r.IsFull()); r.AddData(v); KALDI_ASSERT(r.IsFull()); KALDI_ASSERT(r.NumFrames() == 11 + 1111); { // check last 11 rows were copied to the front in the buffer const VectorBase<BaseFloat> &v2 = r.Value(); AssertEqual(v2.Range(0, 11), v.Range(1100, 11)); } KALDI_ASSERT(!r.Done()); for ( ; !r.Done(); r.Next(), i++) { KALDI_LOG << i; const VectorBase<BaseFloat>& v2 = r.Value(); static_cast<const void>(v2); // variable no longer unused, } KALDI_ASSERT(i == 22); // 22 minibatches } void UnitTestStdVectorRandomizer() { // prepare vector with some data, std::vector<int32> v(1111); for (int32 i = 0; i < v.size(); i++) { v.at(i) = i; } std::random_shuffle(v.begin(), v.end()); // config NnetDataRandomizerOptions c; c.randomizer_size = 1000; c.minibatch_size = 100; // randomizer Int32VectorRandomizer r; r.Init(c); r.AddData(v); KALDI_ASSERT(r.IsFull()); // create vector with consecutive indices std::vector<int32> mask(1111); for (int32 i = 0; i < 1111; i++) { mask[i]=i; } r.Randomize(mask); // no shuffling // make sure we get same data we put to randomizer int32 i = 0; for ( ; !r.Done(); r.Next(), i++) { KALDI_LOG << i; std::vector<int32> v2 = r.Value(); AssertEqual(v2.begin(), v2.end(), v.begin() + (i * c.minibatch_size), v.begin() + ((i+1) * c.minibatch_size)); } KALDI_ASSERT(i == 11); // 11 minibatches KALDI_LOG << "Filling for 2nd time"; // try to fill buffer one more time, and empty it KALDI_ASSERT(!r.IsFull()); r.AddData(v); KALDI_ASSERT(r.IsFull()); KALDI_ASSERT(r.NumFrames() == 11 + 1111); { // check last 11 rows were copied to the front in the buffer std::vector<int32> v2 = r.Value(); AssertEqual(v2.begin(), v2.begin()+11, v.begin()+1100, v.begin()+1100+11); } KALDI_ASSERT(!r.Done()); for ( ; !r.Done(); r.Next(), i++) { KALDI_LOG << i; std::vector<int32> v2 = r.Value(); } KALDI_ASSERT(i == 22); // 22 minibatches } int main() { UnitTestRandomizerMask(); UnitTestMatrixRandomizer(); UnitTestVectorRandomizer(); UnitTestStdVectorRandomizer(); std::cout << "Tests succeeded. "; } |