def testLogAddSub(self): for i in range(100): f1 = kaldi_math.rand() % 10000 f2 = kaldi_math.rand() % 20 add1 = kaldi_math.exp( kaldi_math.log_add(kaldi_math.log(f1), kaldi_math.log(f2))) add2 = kaldi_math.exp( kaldi_math.log_add(kaldi_math.log(f2), kaldi_math.log(f1))) add = f1 + f2 thresh = add * 0.00001 self.assertAlmostEqual(add, add1, delta=thresh) self.assertAlmostEqual(add, add2, delta=thresh) try: f2_check = kaldi_math.exp( kaldi_math.log_sub(kaldi_math.log(add), kaldi_math.log(f1))) self.assertAlmostEqual(f2, f2_check, delta=thresh) except: # self.assertEqual(0, f2) pass
def testDefines(self): self.assertAlmostEqual(0.0, kaldi_math.exp(kaldi_math.K_LOG_ZERO_FLOAT)) self.assertAlmostEqual(0.0, kaldi_math.exp(kaldi_math.K_LOG_ZERO_DOUBLE)) # TODO: # How to test these in Python? # den = 0.0 # self.assertTrue(kaldi_math.KALDI_ISNAN(0.0 / den)) # self.assertFalse(kaldi_math.KALDI_ISINF(0.0 / den)) # self.assertFalse(kaldi_math.KALDI_ISFINITE(0.0 / den)) # self.assertFalse(kaldi_math.KALDI_ISNAN(1.0 / den)) # self.assertTrue(kaldi_math.KALDI_ISINF(1.0 / den)) # self.assertFalse(kaldi_math.KALDI_ISFINITE(1.0 / den)) # self.assertTrue(kaldi_math.KALDI_ISFINITE(0.0)) # self.assertFalse(kaldi_math.KALDI_ISINF(0.0)) # self.assertFalse(kaldi_math.KALDI_ISNAN(0.0)) self.assertTrue(1.0 != 1.0 + kaldi_math.DBL_EPSILON) self.assertTrue(1.0 == 1.0 + 0.5 * kaldi_math.DBL_EPSILON) # self.assertNotAlmostEqual(1.0, 1.0 + kaldi_math.FLT_EPSILON, places = 7) # self.assertAlmostEqual(1.0, 1.0 + 0.5 * kaldi_math.FLT_EPSILON, places = 6) self.assertAlmostEqual(0.0, math.fabs(math.sin(kaldi_math.M_PI))) self.assertAlmostEqual(1.0, math.fabs(math.cos(kaldi_math.M_PI))) self.assertAlmostEqual(0.0, math.fabs(math.sin(kaldi_math.M_2PI))) self.assertAlmostEqual(1.0, math.fabs(math.cos(kaldi_math.M_2PI))) self.assertAlmostEqual( 0.0, math.fabs(math.sin(kaldi_math.exp(kaldi_math.M_LOG_2PI))), places=5) self.assertAlmostEqual( 1.0, math.fabs(math.cos(kaldi_math.exp(kaldi_math.M_LOG_2PI))), places=5)
def showPerplexity(fst, data): num_phones = 0 tot_loglike = 0.0 for i in data: num_phones += len(i) linear_fst = StdVectorFst() make_linear_acceptor(i, linear_fst) composed_fst = compose(linear_fst, fst) weight = shortestdistance(composed_fst)[-1] tot_loglike -= weight.value perplexity = kaldi_math.exp(-(tot_loglike / num_phones)) print("Perplexity over {} phones (of training data) is {}".format( num_phones, perplexity))
def init_rand_diag_gmm(gmm): num_comp, dim = gmm.num_gauss(), gmm.dim() weights = Vector([kaldi_math.rand_uniform() for _ in range(num_comp)]) tot_weigth = weights.sum() for i, m in enumerate(weights): weights[i] = m / tot_weigth means = Matrix([[kaldi_math.rand_gauss() for _ in range(dim)] for _ in range(num_comp)]) vars_ = Matrix([[kaldi_math.exp(kaldi_math.rand_gauss()) for _ in range(dim)] for _ in range(num_comp)]) vars_.invert_elements_() gmm.set_weights(weights) gmm.set_inv_vars_and_means(vars_, means) gmm.perturb(0.5 * kaldi_math.rand_uniform()) gmm.compute_gconsts()