IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 40729 for trunk


Ignore:
Timestamp:
May 10, 2019, 9:54:48 AM (7 years ago)
Author:
eugene
Message:

do not let logL drive to -inf (limit probability to >= 1-e6)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/math/psMixtureModels.c

    r36529 r40729  
    4242
    4343
    44 // #define TESTING
     44# define TESTING 0
     45# define TESTING_VERBOSE 0
    4546/////////////////////////////////////////////////////////////////////////////////
    4647// Internal functions                                                          //
     
    363364    }
    364365
     366# if (TESTING)
     367    fprintf (stderr, "kmeans : %d %f %f %f\n",*iterations,diff,means->data.F32[0][0],means->data.F32[1][0]);
     368# endif
     369   
    365370    *iterations += 1;
    366371  } while ((*iterations < MAX_ITERATIONS)&&
    367372           (diff > THRESHOLD));
     373
     374# if (TESTING)
     375  fprintf (stderr, "psMMkmeans diff: %f\n", diff);
     376# endif
    368377
    369378  *V = diff;
     
    550559        for (j = 0; j < dim; j++) { // VV
    551560          M->data.F32[j] = means->data.F32[i][j];
    552           //      printf("%f %f %f\n",
    553           //             X->data.F32[j],M->data.F32[j],
    554           //             S->data.F32[j][j]);
    555         }
    556 
    557        
    558         P->data.F32[k][i] =
    559           pi->data.F32[i] * psMMNDGaussian(X,M,S);
     561#if (TESTING_VERBOSE)
     562                  printf("%f %f %f\n", X->data.F32[j],M->data.F32[j], S->data.F32[j][j]);
     563#endif
     564        }
     565
     566        // if log-likelihood becomes too small, we risk numerical overflow
     567        float Prob = psMMNDGaussian(X,M,S);
     568        if (Prob < 1e-6) { Prob = 1e-6; }
     569
     570        P->data.F32[k][i] = pi->data.F32[i] * Prob;
    560571
    561572        logL += log(P->data.F32[k][i]);
    562         //      printf("%d %d %f %f %f %f\n",
    563         //             k,i,
    564         //             pi->data.F32[i],
    565         //             psMMNDGaussian(X,M,S),
    566         //             P->data.F32[k][i],logL);
     573#if (TESTING_VERBOSE)
     574        printf("%d %d %f %f %f %f\n",
     575                       k,i,
     576                       pi->data.F32[i],
     577                       psMMNDGaussian(X,M,S),
     578                       P->data.F32[k][i],logL);
     579#endif
    567580        norm_K += P->data.F32[k][i];
    568581      }
     
    633646      }
    634647    }
    635     //    printf("%d %f %f %f %f\n",*iterations,logL,oldlogL,means->data.F32[0][0],means->data.F32[1][0]);
     648
     649# if (TESTING)
     650    fprintf (stderr, "GMM: %d %f %f %f %f\n",*iterations,logL,oldlogL,means->data.F32[0][0],means->data.F32[1][0]);
     651# endif
     652
    636653    *iterations = *iterations + 1;
    637654  } while ((logL - oldlogL > 0)&&
    638655           (*iterations < MAX_ITERATIONS));
     656
     657# if (TESTING)
     658  fprintf (stderr, "psMMGMM logL: %f\n", logL);
     659# endif
    639660
    640661  psFree(counts);
     
    667688  int iterations;
    668689  double V,V0;
    669 #ifdef TESTING
    670   int i,j;
    671 #endif
     690
    672691  if (!modes) {
    673692    modes = psVectorAlloc(N,PS_TYPE_F32);
     
    680699      // Error calculating k-means
    681700    }
    682 #ifdef TESTING
    683     for (i = 0 ; i < m -1; i++) {
    684       for (j = 0; j < dim; j++) {
     701
     702#if (TESTING)
     703    for (int i = 0 ; i < m -1; i++) {
     704      for (int j = 0; j < dim; j++) {
    685705        fprintf(stderr,"KMEANS POST: (%d/%d) (%d/%d) %g\n",
    686706                i,m - 1,j,dim,means->data.F32[i][j]);
     
    694714      // Error calculating GMM
    695715    }
    696 #ifdef TESTING
    697     for (i = 0 ; i < m - 1; i++) {
    698       for (j = 0; j < dim; j++) {
     716
     717#if (TESTING)
     718    for (int i = 0 ; i < m - 1; i++) {
     719      for (int j = 0; j < dim; j++) {
    699720        fprintf(stderr,"GMM POST: (%d/%d) (%d/%d) %g\n",
    700721                i,m - 1,j,dim,means->data.F32[i][j]);
     
    712733    // Error calculating k-means
    713734  }
    714 #ifdef TESTING
    715   for (i = 0 ; i < m; i++) {
    716     for (j = 0; j < dim; j++) {
     735
     736#if (TESTING)
     737  for (int i = 0 ; i < m; i++) {
     738    for (int j = 0; j < dim; j++) {
    717739      fprintf(stderr,"KMEANS POST: (%d/%d) (%d/%d) %g\n",
    718740              i,m,j,dim,means->data.F32[i][j]);
     
    726748    // Error calculating GMM
    727749  }
    728 #ifdef TESTING
    729   for (i = 0 ; i < m; i++) {
    730     for (j = 0; j < dim; j++) {
     750
     751#if (TESTING)
     752  for (int i = 0 ; i < m; i++) {
     753    for (int j = 0; j < dim; j++) {
    731754      fprintf(stderr,"GMM POST: (%d/%d) (%d/%d) %g\n",
    732755              i,m,j,dim,means->data.F32[i][j]);
Note: See TracChangeset for help on using the changeset viewer.