All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
kaldi-math.cc
Go to the documentation of this file.
1 // base/kaldi-math.cc
2 
3 // Copyright 2009-2011 Microsoft Corporation; Yanmin Qian;
4 // Saarland University; Jan Silovsky
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 "base/kaldi-math.h"
22 #ifndef _MSC_VER
23 #include <stdlib.h>
24 #endif
25 #include <string>
26 #include <mutex>
27 
28 namespace kaldi {
29 // These routines are tested in matrix/matrix-test.cc
30 
32  KALDI_ASSERT(n > 0);
33  n--;
34  n |= n >> 1;
35  n |= n >> 2;
36  n |= n >> 4;
37  n |= n >> 8;
38  n |= n >> 16;
39  return n+1;
40 }
41 
42 static std::mutex _RandMutex;
43 
44 int Rand(struct RandomState* state) {
45 #ifdef _MSC_VER
46  // On Windows, just call Rand()
47  return rand();
48 #else
49  if (state) {
50  return rand_r(&(state->seed));
51  } else {
52  std::lock_guard<std::mutex> lock(_RandMutex);
53  return rand();
54  }
55 #endif
56 }
57 
59  // we initialize it as Rand() + 27437 instead of just Rand(), because on some
60  // systems, e.g. at the very least Mac OSX Yosemite and later, it seems to be
61  // the case that rand_r when initialized with rand() will give you the exact
62  // same sequence of numbers that rand() will give if you keep calling rand()
63  // after that initial call. This can cause problems with repeated sequences.
64  // For example if you initialize two RandomState structs one after the other
65  // without calling rand() in between, they would give you the same sequence
66  // offset by one (if we didn't have the "+ 27437" in the code). 27437 is just
67  // a randomly chosen prime number.
68  seed = Rand() + 27437;
69 }
70 
71 bool WithProb(BaseFloat prob, struct RandomState* state) {
72  KALDI_ASSERT(prob >= 0 && prob <= 1.1); // prob should be <= 1.0,
73  // but we allow slightly larger values that could arise from roundoff in
74  // previous calculations.
75  KALDI_COMPILE_TIME_ASSERT(RAND_MAX > 128 * 128);
76  if (prob == 0) return false;
77  else if (prob == 1.0) return true;
78  else if (prob * RAND_MAX < 128.0) {
79  // prob is very small but nonzero, and the "main algorithm"
80  // wouldn't work that well. So: with probability 1/128, we
81  // return WithProb (prob * 128), else return false.
82  if (Rand(state) < RAND_MAX / 128) { // with probability 128...
83  // Note: we know that prob * 128.0 < 1.0, because
84  // we asserted RAND_MAX > 128 * 128.
85  return WithProb(prob * 128.0);
86  } else {
87  return false;
88  }
89  } else {
90  return (Rand(state) < ((RAND_MAX + static_cast<BaseFloat>(1.0)) * prob));
91  }
92 }
93 
94 int32 RandInt(int32 min_val, int32 max_val, struct RandomState* state) {
95  // This is not exact.
96  KALDI_ASSERT(max_val >= min_val);
97  if (max_val == min_val) return min_val;
98 
99 #ifdef _MSC_VER
100  // RAND_MAX is quite small on Windows -> may need to handle larger numbers.
101  if (RAND_MAX > (max_val-min_val)*8) {
102  // *8 to avoid large inaccuracies in probability, from the modulus...
103  return min_val +
104  ((unsigned int)Rand(state) % (unsigned int)(max_val+1-min_val));
105  } else {
106  if ((unsigned int)(RAND_MAX*RAND_MAX) >
107  (unsigned int)((max_val+1-min_val)*8)) {
108  // *8 to avoid inaccuracies in probability, from the modulus...
109  return min_val + ( (unsigned int)( (Rand(state)+RAND_MAX*Rand(state)))
110  % (unsigned int)(max_val+1-min_val));
111  } else {
112  throw std::runtime_error(std::string()
113  +"rand_int failed because we do not support "
114  +"such large random numbers. "
115  +"(Extend this function).");
116  }
117  }
118 #else
119  return min_val +
120  (static_cast<int32>(Rand(state)) % static_cast<int32>(max_val+1-min_val));
121 #endif
122 }
123 
124 // Returns poisson-distributed random number.
125 // Take care: this takes time proportinal
126 // to lambda. Faster algorithms exist but are more complex.
127 int32 RandPoisson(float lambda, struct RandomState* state) {
128  // Knuth's algorithm.
129  KALDI_ASSERT(lambda >= 0);
130  float L = expf(-lambda), p = 1.0;
131  int32 k = 0;
132  do {
133  k++;
134  float u = RandUniform(state);
135  p *= u;
136  } while (p > L);
137  return k-1;
138 }
139 
140 void RandGauss2(float *a, float *b, RandomState *state) {
141  KALDI_ASSERT(a);
142  KALDI_ASSERT(b);
143  float u1 = RandUniform(state);
144  float u2 = RandUniform(state);
145  u1 = sqrtf(-2.0f * logf(u1));
146  u2 = 2.0f * M_PI * u2;
147  *a = u1 * cosf(u2);
148  *b = u1 * sinf(u2);
149 }
150 
151 void RandGauss2(double *a, double *b, RandomState *state) {
152  KALDI_ASSERT(a);
153  KALDI_ASSERT(b);
154  float a_float, b_float;
155  // Just because we're using doubles doesn't mean we need super-high-quality
156  // random numbers, so we just use the floating-point version internally.
157  RandGauss2(&a_float, &b_float, state);
158  *a = a_float;
159  *b = b_float;
160 }
161 
162 
163 } // end namespace kaldi
Relabels neural network egs with the read pdf-id alignments.
Definition: chain.dox:20
float RandUniform(struct RandomState *state=NULL)
Returns a random number strictly between 0 and 1.
Definition: kaldi-math.h:151
#define M_PI
Definition: kaldi-math.h:44
bool WithProb(BaseFloat prob, struct RandomState *state)
Definition: kaldi-math.cc:71
int32 RoundUpToNearestPowerOfTwo(int32 n)
Definition: kaldi-math.cc:31
int32 RandPoisson(float lambda, struct RandomState *state)
Definition: kaldi-math.cc:127
static std::mutex _RandMutex
Definition: kaldi-math.cc:42
float BaseFloat
Definition: kaldi-types.h:29
void RandGauss2(float *a, float *b, RandomState *state)
Definition: kaldi-math.cc:140
struct rnnlm::@11::@12 n
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:44
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
int32 RandInt(int32 min_val, int32 max_val, struct RandomState *state)
Definition: kaldi-math.cc:94
#define KALDI_COMPILE_TIME_ASSERT(b)
Definition: kaldi-utils.h:127