29 using namespace kaldi;
62 double loglike0 = 0.0;
63 double loglike1 = 0.0;
64 double loglike2 = 0.0;
71 std::cout <<
"Per-frame log-likelihood before update = " 72 << (loglike0/feats.
NumRows()) <<
'\n';
73 std::cout <<
"Per-frame log-likelihood (accumulating at once) = " 74 << (loglike1/feats.
NumRows()) <<
'\n';
75 std::cout <<
"Per-frame log-likelihood (accumulating component-wise) = " 76 << (loglike2/feats.
NumRows()) <<
'\n';
81 KALDI_WARN <<
"Unable to pass test_update_flags() test because of " 82 "component removal during Update() call (this is normal)";
85 KALDI_ASSERT(loglike1 >= loglike0 - (std::abs(loglike1)+std::abs(loglike0))*1.0e-06);
86 KALDI_ASSERT(loglike2 >= loglike0 - (std::abs(loglike2)+std::abs(loglike0))*1.0e-06);
97 if (tmp.
Cond() < 100)
break;
98 std::cout <<
"Condition number of random matrix large " 99 <<
static_cast<float>(tmp.
Cond()) <<
", trying again (this is normal)" 105 if (matrix_sqrt != NULL) matrix_sqrt->Cholesky(*matrix);
107 if ((matrix_sqrt == NULL) && (logdet == NULL)) {
119 const std::vector<SpMatrix<BaseFloat> > inv_covars = gmm.
inv_covars();
122 for (
size_t i = 0;
i < inv_covars.size();
i++) {
126 std::cout << i <<
"'th component eigs are: ";
134 BaseFloat logdet = -(inv_covars[
i].LogPosDefDet());
139 log_like -= 0.5 *
VecSpVec(offset, inv_covars[
i], offset);
140 log_like_sum =
LogAdd(log_like_sum, log_like);
172 MleFullGmmUpdate(config, est_gmm_somep, flags, &gmm_some_update, NULL, NULL);
175 KALDI_WARN <<
"Unable to pass test_update_flags() test because of " 176 "component removal during Update() call (this is normal)";
190 std::vector<SpMatrix<BaseFloat> > vars(gmm.
NumGauss());
192 vars[
i].Resize(gmm.
Dim());
195 vars[
i].InvertDouble();
203 double loglike0 = 0.0;
204 double loglike1 = 0.0;
205 double loglike2 = 0.0;
207 loglike0 +=
static_cast<double>(
209 loglike1 +=
static_cast<double>(
211 loglike2 +=
static_cast<double>(
214 KALDI_LOG <<
"loglike1 = " << loglike1 <<
" loglike2 = " << loglike2;
221 std::cout <<
"Testing I/O, binary = " << binary <<
'\n';
223 est_gmm.
Write(
Output(
"tmp_stats", binary).Stream(), binary);
229 Input ki(
"tmp_stats", &binary_in);
232 Input ki2(
"tmp_stats", &binary_in);
270 int32 maxiterations = 20;
275 int32 maxcomponents = 50;
281 std::vector<SpMatrix<BaseFloat> > vars_f(nMix);
282 std::vector<TpMatrix<BaseFloat> > vars_f_sqrt(nMix);
283 for (
int32 mix = 0; mix < nMix; mix++) {
284 vars_f[mix].Resize(dim);
285 vars_f_sqrt[mix].Resize(dim);
288 for (
int32 m = 0; m < nMix; m++) {
296 int32 counter = 0, multiple = 200;
299 for (
int32 m = 0; m < nMix; m++) {
300 for (
int32 i = 0;
i < multiple;
i++) {
304 feats.
Row(counter).CopyFromVec(means_f.
Row(m));
305 feats.
Row(counter).AddTpVec(1.0, vars_f_sqrt[m],
kNoTrans, rnd_vec, 1.0);
321 std::cout <<
"Avg log-like per frame [full-cov, 1-mix] should be: " 323 std::cout <<
"Total log-like [full-cov, 1-mix] should be: " 324 << (feats.
NumRows()*avg_log) <<
'\n';
329 std::cout <<
"Cov eigs are " << s;
341 std::vector<SpMatrix<BaseFloat> > invcovars(1);
342 invcovars[0].Resize(dim);
351 invcovars[0].CopyFromSp(covar);
352 invcovars[0].InvertDouble();
363 KALDI_LOG <<
"Testing natural<>normal conversion";
372 double prec_m = 1e-3;
373 double prec_v = 1e-3;
377 for (
int32 d2 =
d; d2 < dim; ++d2) {
394 float lastloglike = 0.0;
395 int32 lastloglike_nM = 0;
397 while (iteration < maxiterations) {
400 gmm->
Dim(), flags_all);
402 double loglike = 0.0;
403 double loglike_test = 0.0;
404 for (
int32 i = 0;
i < counter;
i++) {
405 loglike +=
static_cast<double>(
413 std::cout <<
"Loglikelihood before iteration " 414 << iteration <<
" : " << std::scientific << loglike
415 <<
" number of components: " << gmm->
NumGauss() <<
'\n';
420 if (iteration % 5 == 0) {
422 if ((iteration > 0) && (gmm->
NumGauss() >= lastloglike_nM)) {
425 lastloglike = loglike;
432 <<
" per frame, over " << (count) <<
" frames.";
437 if ( (iteration < maxiterations - 3) && (iteration % 4 == 1)
438 && (gmm->
NumGauss() * 2 <= maxcomponents)) {
442 if (iteration == 5) {
443 std::cout <<
"Testing flags-driven updates kGmmAll" <<
'\n';
445 std::cout <<
"Testing flags-driven updates kGmmWeights" <<
'\n';
447 std::cout <<
"Testing flags-driven kGmmMeans" <<
'\n';
449 std::cout <<
"Testing flags-driven kGmmVariances" <<
'\n';
451 std::cout <<
"Testing flags-driven kGmmWeights | kGmmMeans" <<
'\n';
453 std::cout <<
"Testing component-wise accumulation" <<
'\n';
463 gmm->
Dim(), flags_all);
466 for (
int32 i = 0;
i < counter;
i++) {
469 test_io(*gmm, est_gmm,
false, feats);
470 test_io(*gmm, est_gmm,
true, feats);
480 for (
int i = 0;
i < 2;
i++)
482 std::cout <<
"Test OK.\n";
void Write(std::ostream &out_stream, bool binary) const
void AddMat2(const Real alpha, const MatrixBase< Real > &M, MatrixTransposeType transM, const Real beta)
rank-N update: if (transM == kNoTrans) (*this) = beta*(*this) + alpha * M * M^T, or (if transM == kTr...
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
void SetWeights(const Vector< Real > &w)
Mutators for both float or double.
void test_io(const FullGmm &gmm, const AccumFullGmm &est_gmm, bool binary, const Matrix< BaseFloat > &feats)
Packed symetric matrix class.
void rand_posdef_spmatrix(size_t dim, SpMatrix< BaseFloat > *matrix, TpMatrix< BaseFloat > *matrix_sqrt=NULL, BaseFloat *logdet=NULL)
const std::vector< SpMatrix< BaseFloat > > & inv_covars() const
int32 NumGauss() const
Returns the number of mixture components.
void AddRowSumMat(Real alpha, const MatrixBase< Real > &M, Real beta=1.0)
Does *this = alpha * (sum of rows of M) + beta * *this.
BaseFloat ComponentPosteriors(const VectorBase< BaseFloat > &data, VectorBase< BaseFloat > *posterior) const
Computes the posterior probabilities of all Gaussian components given a data point.
int32 Dim() const
Returns the dimensionality of the Gaussian mean vectors.
Definition for Gaussian Mixture Model with full covariances in normal mode: where the parameters are ...
Real Cond() const
Returns condition number by computing Svd.
Configuration variables like variance floor, minimum occupancy, etc.
int32 ComputeGconsts()
Sets the gconsts.
void Split(int32 target_components, float perturb_factor, std::vector< int32 > *history=NULL)
Merge the components and remember the order in which the components were merged (flat list of pairs) ...
Definition for Gaussian Mixture Model with full covariances.
void SymPosSemiDefEig(VectorBase< Real > *s, MatrixBase< Real > *P, Real check_thresh=0.001)
Uses Svd to compute the eigenvalue decomposition of a symmetric positive semi-definite matrix: (*this...
void SetInvCovarsAndMeans(const std::vector< SpMatrix< Real > > &invcovars, const Matrix< Real > &means)
Use SetInvCovarsAndMeans if updating both means and (inverse) covariances.
float RandGauss(struct RandomState *state=NULL)
uint16 GmmFlagsType
Bitwise OR of the above flags.
void SetZero(GmmFlagsType flags)
GmmFlagsType Flags() const
Accessors.
MatrixIndexT NumRows() const
void GetCovars(std::vector< SpMatrix< Real > > *v) const
Accessor for covariances.
std::vector< SpMatrix< double > > vars_
covariances
void Resize(int32 num_components, int32 dim, GmmFlagsType flags)
Allocates memory for accumulators.
void AddToDiag(const Real r)
void CopyFromFullGmm(const FullGmm &fullgmm)
Copies from given FullGmm.
void Cholesky(const SpMatrix< Real > &orig)
void Resize(int32 nMix, int32 dim)
Resizes arrays to this dim. Does not initialize data.
void test_flags_driven_update(const FullGmm &gmm, const Matrix< BaseFloat > &feats, GmmFlagsType flags)
Real LogPosDefDet() const
Computes log determinant but only for +ve-def matrices (it uses Cholesky).
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
void Scale(Real alpha)
Multiply each element with a scalar value.
void UnitTestEstimateFullGmm()
void MleFullGmmUpdate(const MleFullGmmOptions &config, const AccumFullGmm &fullgmm_acc, GmmFlagsType flags, FullGmm *gmm, BaseFloat *obj_change_out, BaseFloat *count_out)
for computing the maximum-likelihood estimates of the parameters of a Gaussian mixture model...
void SetRandn()
Sets to random values of a normal distribution.
void AddMatMat(const Real alpha, const MatrixBase< Real > &A, MatrixTransposeType transA, const MatrixBase< Real > &B, MatrixTransposeType transB, const Real beta)
Class for computing the maximum-likelihood estimates of the parameters of a Gaussian mixture model...
Real VecSpVec(const VectorBase< Real > &v1, const SpMatrix< Real > &M, const VectorBase< Real > &v2)
Computes v1^T * M * v2.
Packed symetric matrix class.
BaseFloat GetLogLikeTest(const FullGmm &gmm, const VectorBase< BaseFloat > &feats, bool print_eigs)
void CopyToFullGmm(FullGmm *fullgmm, GmmFlagsType flags=kGmmAll)
Copies to FullGmm.
int32 NumGauss() const
Returns the number of mixture components in the GMM.
void Scale(Real alpha)
Multiplies all elements by this constant.
const Vector< BaseFloat > & weights() const
void Read(std::istream &in_stream, bool binary, bool add)
double LogAdd(double x, double y)
void GetMeans(Matrix< Real > *m) const
Accessor for means.
void TestComponentAcc(const FullGmm &gmm, const Matrix< BaseFloat > &feats)
void SetInvCovars(const std::vector< SpMatrix< Real > > &v)
Set the (inverse) covariances and recompute means_invcovars_.
A class representing a vector.
#define KALDI_ASSERT(cond)
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Real LogDet(Real *det_sign=NULL) const
Returns logdet of matrix.
void AddVecVec(const Real alpha, const VectorBase< OtherReal > &a, const VectorBase< OtherReal > &b)
*this += alpha * a * b^T
static void AssertEqual(float a, float b, float relative_tolerance=0.001)
assert abs(a - b) <= relative_tolerance * (abs(a)+abs(b))
BaseFloat AccumulateFromFull(const FullGmm &gmm, const VectorBase< BaseFloat > &data, BaseFloat frame_posterior)
Accumulate for all components given a full-covariance GMM.
const Matrix< BaseFloat > & means_invcovars() const
void SymPosSemiDefEig(VectorBase< Real > *s, MatrixBase< Real > *P, Real tolerance=0.001) const
This is the version of SVD that we implement for symmetric positive definite matrices.
Provides a vector abstraction class.
Matrix< double > means_
Means.
void Invert(Real *logdet=NULL, Real *det_sign=NULL, bool inverse_needed=true)
matrix inverse.
void AddVec(const Real alpha, const VectorBase< OtherReal > &v)
Add vector : *this = *this + alpha * rv (with casting between floats and doubles) ...
BaseFloat LogLikelihood(const VectorBase< BaseFloat > &data) const
Returns the log-likelihood of a data point (vector) given the GMM.
void SetMeans(const Matrix< Real > &m)
Use SetMeans to update only the Gaussian means (and not variances)
static bool ApproxEqual(float a, float b, float relative_tolerance=0.001)
return abs(a - b) <= relative_tolerance * (abs(a)+abs(b)).
void Scale(BaseFloat f, GmmFlagsType flags)
void AccumulateForComponent(const VectorBase< BaseFloat > &data, int32 comp_index, BaseFloat weight)
Accumulate for a single component, given the posterior.