Skip to content

Commit

Permalink
Fixed yet another NaN bug, and incorporated NaN detection.
Browse files Browse the repository at this point in the history
The NaN bug was caused in random parameter initialization (_gmm_init_params_random) because the mean vector was not initialized to zeros while estimating the overall mean of data.
  • Loading branch information
snitish committed Oct 31, 2015
1 parent 41e8917 commit befb566
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion gmm.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ void gmm_fit(GMM *gmm, const double * const *X, int N)
llh = _gmm_em_step(gmm, X, N);
//if (i_iter%20 == 0)
IPrintf("Iter = %d, LLH = %lf\n", i_iter+1, llh);
if ((llh > 0) == 0 && (llh <= 0) == 0)
{
IPrintf("WARNING: Encountered NaN value at iteration: %d\n", i_iter+1);
gmm_print_params(gmm);
break;
}

// Check for convergence
if (i_iter > 2 && fabs((llh - llh_prev)/llh_prev) < gmm->tol)
Expand Down Expand Up @@ -163,7 +169,7 @@ void _gmm_init_params_random(GMM *gmm, const double * const *X, int N)
gmm->weights[k] = 1.0/gmm->M;

// Initialize component variances to data variance
double *mean = malloc(gmm->D*sizeof(double));
double *mean = calloc(gmm->D, sizeof(double));
for (int t=0; t<N; t++)
_gmm_vec_add(mean, X[t], 1, 1, gmm->D);
_gmm_vec_divide_by_scalar(mean, N, gmm->D);
Expand Down

0 comments on commit befb566

Please sign in to comment.