29 using namespace kaldi;
38 if (tmp.
Cond() < 100)
break;
39 std::cout <<
"Condition number of random matrix large " 40 <<
static_cast<float>(tmp.
Cond()) <<
", trying again (this is normal)" 46 if (matrix_sqrt != NULL) matrix_sqrt->Cholesky(*matrix);
48 if ((matrix_sqrt == NULL) && (logdet == NULL)) {
55 size_t num_comp = gmm->
NumGauss(), dim = gmm->
Dim();
60 for (
size_t m = 0; m < num_comp; m++) {
62 for (
size_t d= 0;
d < dim;
d++) {
66 tot_weight += weights(m);
70 for (
size_t m = 0; m < num_comp; m++) {
71 weights(m) /= tot_weight;
85 int32 num_frames = 5000;
88 fgmm_normal.
Rand(&feats);
91 for (
int32 t = 0; t < num_frames; t++)
99 num_params = (num_comp * (dim + 1 + (dim*(dim+1)/2))),
100 predicted_change = 0.5 * num_params / num_frames;
101 KALDI_LOG <<
"Objf change per frame was " << change <<
" vs. predicted " 103 KALDI_ASSERT(change < 2.0 * predicted_change && change > 0.0);
114 std::cout <<
"Testing NumGauss: " << nMix <<
", " <<
"Dim: " << dim
123 std::vector<SpMatrix<BaseFloat> > invcovars(nMix);
124 for (
size_t mix = 0; mix < nMix; mix++) {
125 invcovars[mix].Resize(dim);
129 for (
size_t d = 0;
d < dim;
d++) {
133 float tot_weight = 0.0;
134 for (
size_t m = 0; m < nMix; m++) {
136 for (
size_t d = 0;
d < dim;
d++) {
141 invcovars[m].CopyFromSp(covar);
142 invcovars[m].InvertDouble();
143 tot_weight += weights(m);
147 for (
size_t m = 0; m < nMix; m++) {
148 weights(m) /= tot_weight;
153 for (
size_t m = 0; m < nMix; m++) {
157 +
VecSpVec(feat, invcovars[m], feat))
159 loglikes(m) +=
Log(weights(m));
185 std::vector<SpMatrix<BaseFloat> > invcovars_bak(nMix);
186 for (
size_t i = 0;
i < nMix;
i++) {
187 invcovars_bak[
i].Resize(dim);
193 for (
size_t i = 0;
i < nMix;
i++) {
194 invcovars_bak[
i].InvertDouble();
212 std::vector<int32> indices;
214 indices.push_back(
i);
224 gmm3.SetWeights(weights_bak);
226 for (
size_t i = 0;
i < nMix;
i++) {
230 gmm3.SetMeans(means_bak);
231 gmm3.SetInvCovars(invcovars_bak);
232 gmm3.ComputeGconsts();
233 float loglike_gmm3 = gmm3.LogLikelihood(feat);
241 for (
size_t i = 0;
i < nMix;
i++) {
242 invcovars_bak[
i].InvertDouble();
257 Input ki(
"tmpf", &binary_in);
271 Input ki2(
"tmpfb", &binary_in);
288 gmm_diag.
Resize(nMix, dim);
308 std::vector<SpMatrix<BaseFloat> > invcovars1(1);
311 invcovars1[0].Resize(dim);
312 invcovars1[0].CopyFromSp(invcovars[0]);
319 gmm2.
Split(2, 0.001);
335 for (
int i = 0;
i < 2;
i++) {
339 std::cout <<
"Test OK.\n";
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...
int32 Dim() const
Returns the dimensionality of the Gaussian mean vectors.
void SetWeights(const Vector< Real > &w)
Mutators for both float or double.
Packed symetric matrix class.
void Merge(int32 target_components, std::vector< int32 > *history=NULL)
Merge the components and remember the order in which the components were merged (flat list of pairs) ...
void Perturb(float perturb_factor)
Perturbs the component means with a random vector multiplied by the pertrub factor.
void SetInvVarsAndMeans(const MatrixBase< Real > &invvars, const MatrixBase< Real > &means)
Use SetInvVarsAndMeans if updating both means and (inverse) variances.
float RandUniform(struct RandomState *state=NULL)
Returns a random number strictly between 0 and 1.
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.
void LogLikelihoodsPreselect(const VectorBase< BaseFloat > &data, const std::vector< int32 > &indices, Vector< BaseFloat > *loglikes) const
Outputs the per-component log-likelihoods of a subset of mixture components.
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 Resize(int32 nMix, int32 dim)
Resizes arrays to this dim. Does not initialize data.
void SetInvCovarsAndMeans(const std::vector< SpMatrix< Real > > &invcovars, const Matrix< Real > &means)
Use SetInvCovarsAndMeans if updating both means and (inverse) covariances.
int32 ComputeGconsts()
Sets the gconsts.
float RandGauss(struct RandomState *state=NULL)
void Write(std::ostream &os, bool binary) const
void CopyFromMat(const MatrixBase< OtherReal > &M, MatrixTransposeType trans=kNoTrans)
Copy given matrix. (no resize is done).
Real LogSumExp(Real prune=-1.0) const
Returns log(sum(exp())) without exp overflow If prune > 0.0, ignores terms less than the max - prune...
void GetCovars(std::vector< SpMatrix< Real > > *v) const
Accessor for covariances.
void CopyFromVec(const VectorBase< Real > &v)
Copy data from another vector (must match own size).
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.
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 UnitTestFullGmmEst()
BaseFloat LogLikelihood(const VectorBase< BaseFloat > &data) const
Returns the log-likelihood of a data point (vector) given the GMM.
void RandPosdefSpMatrix(MatrixIndexT dim, SpMatrix< Real > *matrix)
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 Rand(MatrixBase< BaseFloat > *feats)
Generates random features from the model.
void SetRandn()
Sets to random values of a normal distribution.
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.
void init_rand_diag_gmm(DiagGmm *gmm)
int32 NumGauss() const
Returns the number of mixture components in the GMM.
int32 NumGauss() const
Returns the number of mixture components in the GMM.
void SetZero()
Sets matrix to zero.
const Vector< BaseFloat > & weights() const
int Rand(struct RandomState *state)
Real Sum() const
Returns sum of the elements.
void Read(std::istream &is, bool binary)
void GetMeans(Matrix< Real > *m) const
Accessor for means.
void CopyFromFullGmm(const FullGmm &fullgmm)
Copies from given FullGmm.
void InitRandFullGmm(int32 dim, int32 num_comp, FullGmm *gmm)
void InvertElements()
Inverts all the elements of the matrix.
void SetInvCovars(const std::vector< SpMatrix< Real > > &v)
Set the (inverse) covariances and recompute means_invcovars_.
A class representing a vector.
void LogLikelihoods(const VectorBase< BaseFloat > &data, Vector< BaseFloat > *loglikes) const
Outputs the per-component contributions to the log-likelihood.
void CopyFromDiagGmm(const DiagGmm &diaggmm)
Copies from given DiagGmm.
#define KALDI_ASSERT(cond)
static void AssertEqual(float a, float b, float relative_tolerance=0.001)
assert abs(a - b) <= relative_tolerance * (abs(a)+abs(b))
Definition for Gaussian Mixture Model with diagonal covariances.
SubMatrix< Real > Range(const MatrixIndexT row_offset, const MatrixIndexT num_rows, const MatrixIndexT col_offset, const MatrixIndexT num_cols) const
Return a sub-part of matrix.
BaseFloat AccumulateFromFull(const FullGmm &gmm, const VectorBase< BaseFloat > &data, BaseFloat frame_posterior)
Accumulate for all components given a full-covariance GMM.
void SetWeights(const VectorBase< Real > &w)
Mutators for both float or double.
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)
Represents a non-allocating general vector which can be defined as a sub-vector of higher-level vecto...
void GetCovarsAndMeans(std::vector< SpMatrix< Real > > *covars, Matrix< Real > *means) const
Accessor for covariances and means.
int32 RandInt(int32 min_val, int32 max_val, struct RandomState *state)
void GetComponentMean(int32 gauss, VectorBase< Real > *out) const
Accessor for component mean.