26 using namespace kaldi;
59 double loglike0 = 0.0;
60 double loglike1 = 0.0;
61 double loglike2 = 0.0;
68 std::cout <<
"Per-frame log-likelihood before update = " 69 << (loglike0/feats.
NumRows()) <<
'\n';
70 std::cout <<
"Per-frame log-likelihood (accumulating at once) = " 71 << (loglike1/feats.
NumRows()) <<
'\n';
72 std::cout <<
"Per-frame log-likelihood (accumulating component-wise) = " 73 << (loglike2/feats.
NumRows()) <<
'\n';
78 KALDI_WARN <<
"Unable to pass test_update_flags() test because of " 79 "component removal during Update() call (this is normal)";
82 KALDI_ASSERT(loglike1 >= loglike0 - (std::abs(loglike1)+std::abs(loglike0))*1.0e-06);
83 KALDI_ASSERT(loglike2 >= loglike0 - (std::abs(loglike2)+std::abs(loglike0))*1.0e-06);
115 MleDiagGmmUpdate(config, est_gmm_somep, flags, &gmm_some_update, NULL, NULL);
118 KALDI_WARN <<
"Unable to pass test_update_flags() test because of " 119 "component removal during Update() call (this is normal)";
135 vars.InvertElements();
142 double loglike0 = 0.0;
143 double loglike1 = 0.0;
144 double loglike2 = 0.0;
146 loglike0 +=
static_cast<double>(
148 loglike1 +=
static_cast<double>(
150 loglike2 +=
static_cast<double>(
153 if ((flags & kGmmVariances) && !(flags &
kGmmMeans))
163 std::cout <<
"Testing I/O, binary = " << binary <<
'\n';
165 est_gmm.
Write(
Output(
"tmp_stats", binary).Stream(), binary);
171 Input ki(
"tmp_stats", &binary_in);
174 Input ki2(
"tmp_stats", &binary_in);
205 size_t maxiterations = 20;
210 int32 maxcomponents = 10;
215 for (
size_t m = 0; m < nMix; m++) {
216 for (
size_t d= 0;
d < dim;
d++) {
224 size_t counter = 0, multiple = 200;
226 for (
size_t m = 0; m < nMix; m++) {
227 for (
size_t i = 0;
i < multiple;
i++) {
228 for (
size_t d = 0;
d < dim;
d++) {
230 std::sqrt(vars_f(m,
d));
239 for (
size_t i = 0;
i < counter;
i++) {
241 mean_acc.
AddVec(1.0, featvec);
243 var_acc.
AddVec(1.0, featvec);
245 mean_acc.
Scale(1.0F/counter);
246 var_acc.
Scale(1.0F/counter);
247 var_acc.
AddVec2(-1.0, mean_acc);
260 for (
size_t d= 0;
d < dim;
d++) {
276 KALDI_LOG <<
"Testing natural<>normal conversion";
305 size_t iteration = 0;
306 float lastloglike = 0.0;
307 int32 lastloglike_nM = 0;
309 while (iteration < maxiterations) {
313 double loglike = 0.0;
314 for (
size_t i = 0;
i < counter;
i++) {
319 std::cout <<
"Loglikelihood before iteration " << iteration <<
" : " 320 << std::scientific << loglike <<
" number of components: " 324 if (iteration % 5 == 0) {
326 if ((iteration > 0) && (gmm->
NumGauss() >= lastloglike_nM)) {
329 lastloglike = loglike;
334 est_gmm.
Write(
Output(
"tmp_stats",
true).Stream(),
true);
338 Input ki(
"tmp_stats", &binary_in);
344 <<
" per frame, over " << (count) <<
" frames.";
346 if ((iteration % 3 == 1) && (gmm->
NumGauss() * 2 <= maxcomponents)) {
350 if (iteration == 5) {
351 std::cout <<
"Testing flags-driven updates" <<
'\n';
357 std::cout <<
"Testing component-wise accumulation" <<
'\n';
367 gmm->
Dim(), flags_all);
370 for (
size_t i = 0;
i < counter;
i++) {
373 test_io(*gmm, est_gmm,
false, feats);
374 test_io(*gmm, est_gmm,
true, feats);
380 gmm->
Dim(), flags_all);
384 for (
size_t i = 0;
i < counter;
i++)
385 weights(
i) = 0.5 + 0.1 * (
Rand() % 10);
389 for (
size_t i = 0;
i < counter;
i++) {
390 loglike += weights(
i) *
394 int32 num_threads = 2;
409 for (
int i = 0;
i < 2;
i++)
411 std::cout <<
"Test OK.\n";
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 CopyFromDiagGmm(const DiagGmm &diaggmm)
Copies from given DiagGmm.
void SetInvVarsAndMeans(const MatrixBase< Real > &invvars, const MatrixBase< Real > &means)
Use SetInvVarsAndMeans if updating both means and (inverse) variances.
BaseFloat AccumulateFromDiagMultiThreaded(const DiagGmm &gmm, const MatrixBase< BaseFloat > &data, const VectorBase< BaseFloat > &frame_weights, int32 num_threads)
This does the same job as AccumulateFromDiag, but using multiple threads.
void Split(int32 target_components, float perturb_factor, std::vector< int32 > *history=NULL)
Split the components and remember the order in which the components were split.
Definition for Gaussian Mixture Model with diagonal covariances in normal mode: where the parameters ...
const Matrix< BaseFloat > & means_invvars() const
void test_flags_driven_update(const DiagGmm &gmm, const Matrix< BaseFloat > &feats, GmmFlagsType flags)
void UnitTestEstimateDiagGmm()
void MleDiagGmmUpdate(const MleDiagGmmOptions &config, const AccumDiagGmm &diag_gmm_acc, GmmFlagsType flags, DiagGmm *gmm, BaseFloat *obj_change_out, BaseFloat *count_out, int32 *floored_elements_out, int32 *floored_gaussians_out, int32 *removed_gaussians_out)
for computing the maximum-likelihood estimates of the parameters of a Gaussian mixture model...
void TestComponentAcc(const DiagGmm &gmm, const Matrix< BaseFloat > &feats)
void Resize(int32 nMix, int32 dim)
Resizes arrays to this dim. Does not initialize data.
int32 ComputeGconsts()
Sets the gconsts.
float RandGauss(struct RandomState *state=NULL)
uint16 GmmFlagsType
Bitwise OR of the above flags.
void CopyFromMat(const MatrixBase< OtherReal > &M, MatrixTransposeType trans=kNoTrans)
Copy given matrix. (no resize is done).
void SetMeans(const MatrixBase< Real > &m)
Use SetMeans to update only the Gaussian means (and not variances)
double min_variance
Minimum allowed variance in any dimension (if no variance floor) It is in double since the variance i...
void Scale(BaseFloat f, GmmFlagsType flags)
void CopyRowFromMat(const MatrixBase< Real > &M, MatrixIndexT row)
Extracts a row of the matrix M.
void GetVars(Matrix< Real > *v) const
Accessor for covariances.
void AddVec2(const Real alpha, const VectorBase< Real > &v)
Add vector : *this = *this + alpha * rv^2 [element-wise squaring].
BaseFloat ComponentPosteriors(const VectorBase< BaseFloat > &data, Vector< BaseFloat > *posteriors) const
Computes the posterior probabilities of all Gaussian components given a data point.
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
BaseFloat AccumulateFromDiag(const DiagGmm &gmm, const VectorBase< BaseFloat > &data, BaseFloat frame_posterior)
Accumulate for all components given a diagonal-covariance GMM.
GmmFlagsType Flags() const
BaseFloat LogLikelihood(const VectorBase< BaseFloat > &data) const
Returns the log-likelihood of a data point (vector) given the GMM.
void GetMeans(Matrix< Real > *m) const
Accessor for means.
void Write(std::ostream &out_stream, bool binary) const
void AccumulateForComponent(const VectorBase< BaseFloat > &data, int32 comp_index, BaseFloat weight)
Accumulate for a single component, given the posterior.
const Vector< BaseFloat > & weights() const
Matrix< double > vars_
diagonal variance
int32 NumGauss() const
Returns the number of mixture components in the GMM.
Configuration variables like variance floor, minimum occupancy, etc.
void Scale(Real alpha)
Multiplies all elements by this constant.
void Read(std::istream &in_stream, bool binary, bool add)
void SetZero(GmmFlagsType flags)
int Rand(struct RandomState *state)
void SetInvVars(const MatrixBase< Real > &v)
Set the (inverse) variances and recompute means_invvars_.
int32 Dim() const
Returns the dimensionality of the feature vectors.
Matrix< double > means_
Means.
void InvertElements()
Inverts all the elements of the matrix.
A class representing a vector.
#define KALDI_ASSERT(cond)
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
void ApplyPow(Real power)
Take all elements of vector to a power.
void test_io(const DiagGmm &gmm, const AccumDiagGmm &est_gmm, bool binary, const Matrix< BaseFloat > &feats)
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.
void SetWeights(const VectorBase< Real > &w)
Mutators for both float or double.
void Resize(int32 num_gauss, int32 dim, GmmFlagsType flags)
Allocates memory for accumulators.
void AddVec(const Real alpha, const VectorBase< OtherReal > &v)
Add vector : *this = *this + alpha * rv (with casting between floats and doubles) ...
static bool ApproxEqual(float a, float b, float relative_tolerance=0.001)
return abs(a - b) <= relative_tolerance * (abs(a)+abs(b)).
void AssertEqual(const AccumDiagGmm &other)
int32 NumGauss() const
Returns the number of mixture components.
const Matrix< BaseFloat > & inv_vars() const
void CopyToDiagGmm(DiagGmm *diaggmm, GmmFlagsType flags=kGmmAll) const
Copies to DiagGmm the requested parameters.