public TrainerScore(Data feature, float probability, HMMState state, float logAlpha, float logBeta, float[] logComponentProb) { this.feature = feature; this.hmmState = state; this.logScalingFactor = 0f; if (state != null && state.isEmitting()) { int i = state.getState(); SenoneHMM senoneHMM = (SenoneHMM)state.getHMM(); SenoneSequence senoneSequence = senoneHMM.getSenoneSequence(); this.senone = senoneSequence.getSenones()[i]; } this.logOutputProbability = probability; this.logAlpha = logAlpha; this.logBeta = logBeta; this.logGamma = logAlpha + logBeta; if (logComponentProb != null) { this.logComponentProb = new float[logComponentProb.Length]; this.logComponentGamma = new float[logComponentProb.Length]; for (int i = 0; i < logComponentProb.Length; i++) { this.logComponentProb[i] = logComponentProb[i]; this.logComponentGamma[i] = logComponentProb[i] + this.logGamma; } } else { this.logComponentProb = null; this.logComponentGamma = null; } }
private Graph buildModelGraph(SenoneHMM senoneHMM) { Graph graph = new Graph(); Node node = null; float[][] transitionMatrix = senoneHMM.getTransitionMatrix(); Node node2 = new Node(NodeType.__DUMMY); graph.addNode(node2); graph.setInitialNode(node2); for (int i = 0; i < senoneHMM.getOrder() + 1; i++) { node = new Node(NodeType.__STATE, senoneHMM.getUnit().getName()); node.setObject(senoneHMM.getState(i)); graph.addNode(node); if (i == 0) { graph.linkNodes(node2, node); } for (int j = 0; j <= i; j++) { if (transitionMatrix[j][i] != -3.40282347E+38f) { graph.linkNodes(graph.getNode(j + 1), node); } } node2 = node; } graph.setFinalNode(node); return(graph); }
private void accumulateStateTransition(int num, SenoneHMM senoneHMM, float num2) { float[][] transitionMatrix = senoneHMM.getTransitionMatrix(); float[] array = transitionMatrix[num]; int id = ((Integer)this.indexMap.get(transitionMatrix)).intValue(); Buffer[] array2 = (Buffer[])this.matrixBufferPool.get(id); for (int i = 0; i < array.Length; i++) { if (array[i] != -3.40282347E+38f) { array2[num].logAccumulate(num2, i, this.logMath); } } }
private void accumulateStateTransition(int num, TrainerScore[] array, TrainerScore[] array2) { HMMState state = array[num].getState(); if (state == null) { return; } int state2 = state.getState(); SenoneHMM senoneHMM = (SenoneHMM)state.getHMM(); float[][] transitionMatrix = senoneHMM.getTransitionMatrix(); int id = ((Integer)this.indexMap.get(transitionMatrix)).intValue(); Buffer[] array3 = (Buffer[])this.matrixBufferPool.get(id); float[] array4 = transitionMatrix[state2]; for (int i = 0; i < array4.Length; i++) { if (array4[i] != -3.40282347E+38f) { int num2 = i - state2; int num3 = num + num2; if (!HMMPoolManager.assertionsDisabled && array2[num3].getState() != null && array2[num3].getState().getHMM() != senoneHMM) { throw new AssertionError(); } float alpha = array[num].getAlpha(); float beta = array2[num3].getBeta(); float num4 = array4[i]; float score = array2[num3].getScore(); float num5 = alpha + beta + num4 + score; num5 -= this.currentLogLikelihood; array3[state2].logAccumulate(num5, i, this.logMath); } } }
private void forwardPass(TrainerScore[] array) { for (int i = 0; i < this.graph.size(); i++) { this.outputProbs[i] = this.calculateScores(i); array[i] = new TrainerScore(this.curFeature, this.outputProbs[i], (HMMState)this.graph.getNode(i).getObject(), this.componentScores); array[i].setAlpha(this.probCurrentFrame[i]); } float[] array2 = this.probCurrentFrame; this.probCurrentFrame = new float[this.graph.size()]; int j; for (j = 0; j < this.graph.size(); j++) { Node node = this.graph.getNode(j); if (node.isType("STATE")) { SenoneHMMState senoneHMMState = (SenoneHMMState)node.getObject(); SenoneHMM senoneHMM = (SenoneHMM)senoneHMMState.getHMM(); if (senoneHMMState.isEmitting()) { this.probCurrentFrame[j] = float.MinValue; node.startIncomingEdgeIterator(); while (node.hasMoreIncomingEdges()) { Node source = node.nextIncomingEdge().getSource(); int num = this.graph.indexOf(source); HMMState hmmstate = (HMMState)source.getObject(); float num2; if (hmmstate != null) { if (!BaumWelchLearner.assertionsDisabled && hmmstate.isEmitting() && hmmstate.getHMM() != senoneHMM) { throw new AssertionError(); } if (!hmmstate.isEmitting()) { num2 = 0f; } else { num2 = senoneHMM.getTransitionProbability(hmmstate.getState(), senoneHMMState.getState()); } } else { num2 = 0f; } this.probCurrentFrame[j] = this.logMath.addAsLinear(this.probCurrentFrame[j], array2[num] + num2); } float[] array3 = this.probCurrentFrame; int num3 = j; float[] array4 = array3; array4[num3] += this.outputProbs[j]; array[j].setAlpha(this.probCurrentFrame[j]); } } } j = 0; while (j < this.graph.size()) { Node node = this.graph.getNode(j); HMMState hmmstate2 = null; SenoneHMM senoneHMM = null; if (node.isType("STATE")) { hmmstate2 = (HMMState)node.getObject(); senoneHMM = (SenoneHMM)hmmstate2.getHMM(); if (!hmmstate2.isEmitting()) { goto IL_26F; } } else { if (!this.graph.isInitialNode(node)) { goto IL_26F; } array[j].setAlpha(float.MinValue); this.probCurrentFrame[j] = float.MinValue; } IL_35D: j++; continue; IL_26F: this.probCurrentFrame[j] = float.MinValue; node.startIncomingEdgeIterator(); while (node.hasMoreIncomingEdges()) { Node source2 = node.nextIncomingEdge().getSource(); int num4 = this.graph.indexOf(source2); float num5; if (source2.isType("STATE")) { HMMState hmmstate3 = (HMMState)source2.getObject(); if (!BaumWelchLearner.assertionsDisabled && hmmstate3.isEmitting() && hmmstate3.getHMM() != senoneHMM) { throw new AssertionError(); } if (!hmmstate3.isEmitting()) { num5 = 0f; } else { num5 = senoneHMM.getTransitionProbability(hmmstate3.getState(), hmmstate2.getState()); } } else { num5 = 0f; } this.probCurrentFrame[j] = this.logMath.addAsLinear(this.probCurrentFrame[j], this.probCurrentFrame[num4] + num5); } array[j].setAlpha(this.probCurrentFrame[j]); goto IL_35D; } }
private void backwardPass(TrainerScore[] array) { for (int i = 0; i < this.graph.size(); i++) { this.outputProbs[i] = array[i].getScore(); array[i].setBeta(this.probCurrentFrame[i]); } float[] array2 = this.probCurrentFrame; this.probCurrentFrame = new float[this.graph.size()]; int j; for (j = 0; j < this.graph.size(); j++) { Node node = this.graph.getNode(j); if (node.isType("STATE")) { HMMState hmmstate = (HMMState)node.getObject(); SenoneHMM senoneHMM = (SenoneHMM)hmmstate.getHMM(); if (hmmstate.isEmitting()) { this.probCurrentFrame[j] = float.MinValue; node.startOutgoingEdgeIterator(); while (node.hasMoreOutgoingEdges()) { Node destination = node.nextOutgoingEdge().getDestination(); int num = this.graph.indexOf(destination); HMMState hmmstate2 = (HMMState)destination.getObject(); float num2; if (hmmstate2 != null) { if (!BaumWelchLearner.assertionsDisabled && hmmstate2.isEmitting() && hmmstate2.getHMM() != senoneHMM) { throw new AssertionError(); } if (hmmstate2.getHMM() != senoneHMM) { num2 = 0f; } else { num2 = senoneHMM.getTransitionProbability(hmmstate.getState(), hmmstate2.getState()); } } else { num2 = 0f; } this.probCurrentFrame[j] = this.logMath.addAsLinear(this.probCurrentFrame[j], array2[num] + num2 + this.outputProbs[num]); } array[j].setBeta(this.probCurrentFrame[j]); } } } j = this.graph.size() - 1; while (j >= 0) { Node node = this.graph.getNode(j); HMMState hmmstate = null; if (node.isType("STATE")) { hmmstate = (HMMState)node.getObject(); if (!hmmstate.isEmitting()) { goto IL_21A; } } else { if (!this.graph.isFinalNode(node)) { goto IL_21A; } array[j].setBeta(float.MinValue); this.probCurrentFrame[j] = float.MinValue; } IL_2DE: j--; continue; IL_21A: this.probCurrentFrame[j] = float.MinValue; node.startOutgoingEdgeIterator(); while (node.hasMoreOutgoingEdges()) { Node destination2 = node.nextOutgoingEdge().getDestination(); int num3 = this.graph.indexOf(destination2); float num4; if (destination2.isType("STATE")) { HMMState hmmstate3 = (HMMState)destination2.getObject(); if (!BaumWelchLearner.assertionsDisabled && !hmmstate3.isEmitting() && hmmstate3 != hmmstate) { throw new AssertionError(); } num4 = 0f; } else { num4 = 0f; } this.probCurrentFrame[j] = this.logMath.addAsLinear(this.probCurrentFrame[j], this.probCurrentFrame[num3] + num4); } array[j].setBeta(this.probCurrentFrame[j]); goto IL_2DE; } }
private void loadPhoneList(PropertySheet propertySheet, bool flag, InputStream inputStream, string text) { int num = 0; int num2 = 1; int num3 = 1; ExtendedStreamTokenizer extendedStreamTokenizer = new ExtendedStreamTokenizer(inputStream, 35, false); this.meansPool = new Pool("means"); this.variancePool = new Pool("variances"); this.matrixPool = new Pool("transitionmatrices"); this.senonePool = new Pool("senones"); float @float = propertySheet.getFloat("MixtureComponentScoreFloor"); float float2 = propertySheet.getFloat("mixtureWeightFloor"); float num4 = 0f; float float3 = propertySheet.getFloat("varianceFloor"); this.logger.info("Loading phone list file from: "); this.logger.info(text); string expecting = "0.1"; extendedStreamTokenizer.expectString("version"); extendedStreamTokenizer.expectString(expecting); extendedStreamTokenizer.expectString("same_sized_models"); int num5 = String.instancehelper_equals(extendedStreamTokenizer.getString(), "yes") ? 1 : 0; if (num5 != 0) { extendedStreamTokenizer.expectString("n_state"); num = extendedStreamTokenizer.getInt("numBase"); } extendedStreamTokenizer.expectString("tmat_skip"); int num6 = String.instancehelper_equals(extendedStreamTokenizer.getString(), "yes") ? 1 : 0; int num7 = 0; int num8 = 0; for (;;) { string @string = extendedStreamTokenizer.getString(); if (extendedStreamTokenizer.isEOF()) { break; } int num9 = num; if (num5 == 0) { num9 = extendedStreamTokenizer.getInt("ModelSize"); } this.phoneList.put(@string, Integer.valueOf(num9)); this.logger.fine(new StringBuilder().append("Phone: ").append(@string).append(" size: ").append(num9).toString()); int[] array = new int[num9]; string rep = "-"; int i = 0; while (i < num9) { array[i] = num7; i++; num7++; } Unit unit = this.unitManager.getUnit(@string, String.instancehelper_equals(@string, "SIL")); this.contextIndependentUnits.put(unit.getName(), unit); if (this.logger.isLoggable(Level.FINE)) { this.logger.fine(new StringBuilder().append("Loaded ").append(unit).append(" with ").append(num9).append(" states").toString()); } this.addModelToDensityPool(this.meansPool, array, num2, num3); this.addModelToDensityPool(this.variancePool, array, num2, num3); this.addModelToTransitionMatrixPool(this.matrixPool, num8, array.Length, num4, num6 != 0); this.addModelToSenonePool(this.senonePool, array, @float, float3); float[][] transitionMatrix = (float[][])this.matrixPool.get(num8); SenoneSequence senoneSequence = this.getSenoneSequence(array); SenoneHMM hmm = new SenoneHMM(unit, senoneSequence, transitionMatrix, HMMPosition.lookup(rep)); this.hmmManager.put(hmm); num8++; } this.mixtureWeights = this.initMixtureWeights(num7, num2, num3, float2); extendedStreamTokenizer.close(); }
private void saveHMMPool(bool flag, OutputStream outputStream, string text) { this.logger.info("Saving HMM file to: "); this.logger.info(text); if (outputStream == null) { string text2 = new StringBuilder().append("Error trying to write file ").append(this.location).append(text).toString(); throw new IOException(text2); } PrintWriter printWriter = new PrintWriter(outputStream, true); int num = 0; int num2 = 0; int num3 = 0; int num4 = 0; Iterator iterator = this.hmmManager.iterator(); while (iterator.hasNext()) { HMM hmm = (HMM)iterator.next(); num4 += hmm.getOrder() + 1; if (((SenoneHMM)hmm).isContextDependent()) { num2++; } else { num++; num3 += hmm.getOrder(); } } printWriter.println("0.3"); printWriter.println(new StringBuilder().append(num).append(" n_base").toString()); printWriter.println(new StringBuilder().append(num2).append(" n_tri").toString()); printWriter.println(new StringBuilder().append(num4).append(" n_state_map").toString()); int statesNum = this.mixtureWeights.getStatesNum(); printWriter.println(new StringBuilder().append(statesNum).append(" n_tied_state").toString()); printWriter.println(new StringBuilder().append(num3).append(" n_tied_ci_state").toString()); int num5 = num; if (!Sphinx3Saver.assertionsDisabled && num5 != this.matrixPool.size()) { throw new AssertionError(); } printWriter.println(new StringBuilder().append(num5).append(" n_tied_tmat").toString()); printWriter.println("#"); printWriter.println("# Columns definitions"); printWriter.println("#base lft rt p attrib tmat ... state id's ..."); Iterator iterator2 = this.hmmManager.iterator(); while (iterator2.hasNext()) { HMM hmm2 = (HMM)iterator2.next(); SenoneHMM senoneHMM = (SenoneHMM)hmm2; if (!senoneHMM.isContextDependent()) { Unit unit = senoneHMM.getUnit(); string name = unit.getName(); printWriter.print(new StringBuilder().append(name).append('\t').toString()); string text3 = "-"; printWriter.print(new StringBuilder().append(text3).append(" ").toString()); string text4 = "-"; printWriter.print(new StringBuilder().append(text4).append(' ').toString()); string text5 = senoneHMM.getPosition().toString(); printWriter.print(new StringBuilder().append(text5).append('\t').toString()); string text6 = (!unit.isFiller()) ? "n/a" : "filler"; printWriter.print(new StringBuilder().append(text6).append('\t').toString()); int num6 = this.matrixPool.indexOf(senoneHMM.getTransitionMatrix()); if (!Sphinx3Saver.assertionsDisabled && num6 >= num5) { throw new AssertionError(); } printWriter.print(new StringBuilder().append(num6).append("\t").toString()); SenoneSequence senoneSequence = senoneHMM.getSenoneSequence(); Senone[] senones = senoneSequence.getSenones(); Senone[] array = senones; int num7 = array.Length; for (int i = 0; i < num7; i++) { Senone @object = array[i]; int num8 = this.senonePool.indexOf(@object); if (!Sphinx3Saver.assertionsDisabled && (num8 < 0 || num8 >= num3)) { throw new AssertionError(); } printWriter.print(new StringBuilder().append(num8).append("\t").toString()); } printWriter.println("N"); if (this.logger.isLoggable(Level.FINE)) { this.logger.fine(new StringBuilder().append("Saved ").append(unit).toString()); } } } iterator2 = this.hmmManager.iterator(); while (iterator2.hasNext()) { HMM hmm2 = (HMM)iterator2.next(); SenoneHMM senoneHMM = (SenoneHMM)hmm2; if (senoneHMM.isContextDependent()) { Unit unit = senoneHMM.getUnit(); LeftRightContext leftRightContext = (LeftRightContext)unit.getContext(); Unit[] leftContext = leftRightContext.getLeftContext(); Unit[] rightContext = leftRightContext.getRightContext(); if (!Sphinx3Saver.assertionsDisabled && (leftContext.Length != 1 || rightContext.Length != 1)) { throw new AssertionError(); } string text5 = unit.getName(); printWriter.print(new StringBuilder().append(text5).append('\t').toString()); string text6 = leftContext[0].getName(); printWriter.print(new StringBuilder().append(text6).append(" ").toString()); string name2 = rightContext[0].getName(); printWriter.print(new StringBuilder().append(name2).append(' ').toString()); string text7 = senoneHMM.getPosition().toString(); printWriter.print(new StringBuilder().append(text7).append('\t').toString()); string text8 = (!unit.isFiller()) ? "n/a" : "filler"; if (!Sphinx3Saver.assertionsDisabled && !String.instancehelper_equals(text8, "n/a")) { throw new AssertionError(); } printWriter.print(new StringBuilder().append(text8).append('\t').toString()); int num9 = this.matrixPool.indexOf(senoneHMM.getTransitionMatrix()); if (!Sphinx3Saver.assertionsDisabled && num9 >= num5) { throw new AssertionError(); } printWriter.print(new StringBuilder().append(num9).append("\t").toString()); SenoneSequence senoneSequence2 = senoneHMM.getSenoneSequence(); Senone[] senones2 = senoneSequence2.getSenones(); Senone[] array2 = senones2; int num8 = array2.Length; for (int j = 0; j < num8; j++) { Senone object2 = array2[j]; int num10 = this.senonePool.indexOf(object2); if (!Sphinx3Saver.assertionsDisabled && (num10 < 0 || num10 >= statesNum)) { throw new AssertionError(); } printWriter.print(new StringBuilder().append(num10).append("\t").toString()); } printWriter.println("N"); if (this.logger.isLoggable(Level.FINE)) { this.logger.fine(new StringBuilder().append("Saved ").append(unit).toString()); } } } outputStream.close(); }