/* CreateMixes: create array[1..S][1..segLen] of mix component index */ IntVec *CreateMixes(MemHeap *x,int segLen) { IntVec *mixes; int s; mixes = (IntVec*)New(x,sizeof(IntVec)*nStreams); --mixes; for (s=1; s<=nStreams; s++) mixes[s] = CreateIntVec(x,segLen); return mixes; }
/* EstimateModel: top level of iterative estimation process */ void EstimateModel(void) { LogFloat totalP,newP,delta; Boolean converged = FALSE; int i,iter,numSegs,segLen; IntVec states; /* array[1..numSegs] of State */ IntVec *mixes; /* array[1..S][1..numSegs] of MixComp */ if (trace&T_TOP) printf("Starting Estimation Process\n"); if (newModel){ UniformSegment(); } totalP=LZERO; for (iter=1; !converged && iter<=maxIter; iter++){ ZeroAccs(&hset, uFlags); /* Clear all accumulators */ numSegs = NumSegs(segStore); /* Align on each training segment and accumulate stats */ for (newP=0.0,i=1;i<=numSegs;i++) { segLen = SegLength(segStore,i); states = CreateIntVec(&gstack,segLen); mixes = (hset.hsKind==DISCRETEHS)?NULL: CreateMixes(&gstack,segLen); newP += ViterbiAlign(i,segLen,states,mixes); if (trace&T_ALN) ShowAlignment(i,segLen,states,mixes); UpdateCounts(i,segLen,states,mixes); FreeIntVec(&gstack,states); /* disposes mixes too */ } /* Update parameters or quit */ newP /= (float)numSegs; delta = newP - totalP; converged = (iter>1) && (fabs(delta) < epsilon); if (!converged) UpdateParameters(); totalP = newP; if (trace & T_TOP){ printf("Iteration %d: Average LogP =%12.5f",iter,totalP); if (iter > 1) printf(" Change =%12.5f\n",delta); else printf("\n"); fflush(stdout); } } if (trace&T_TOP) { if (converged) printf("Estimation converged at iteration %d\n",iter); else printf("Estimation aborted at iteration %d\n",iter); fflush(stdout); } }