Package cc.mallet.fst.Transducer

Examples of cc.mallet.fst.Transducer.State


          if (nodes[ip][i] == null) logger.fine ("nodes[ip][i] is NULL");
          else if (nodes[ip][i].alpha == Transducer.IMPOSSIBLE_WEIGHT) logger.fine ("nodes[ip][i].alpha is -Inf");
          logger.fine ("-INFINITE weight or NULL...skipping");
          continue;
        }
        State s = t.getState(i);

        TransitionIterator iter = s.transitionIterator (input, ip, output, ip);
        if (logger.isLoggable (Level.FINE))
          logger.fine (" Starting Forward transition iteration from state "
              + s.getName() + " on input " + input.get(ip).toString()
              + " and output "
              + (output==null ? "(null)" : output.get(ip).toString()));
        while (iter.hasNext()) {
          State destination = iter.nextState();
          boolean legalTransition = true;
          // check constraints to see if node at <ip,i> can transition to destination
          if (ip+1 < constraints.length && constraints[ip+1] > 0 && ((constraints[ip+1]-1) != destination.getIndex())) {
            logger.fine ("Destination state does not match positive constraint. Assigning -infinite weight. position="+(ip+1)+", constraint="+(constraints[ip+1]-1)+", source ="+i+", destination="+destination.getIndex());
            legalTransition = false;
          }
          else if (((ip+1) < constraints.length) && constraints[ip+1] < 0 && (-(constraints[ip+1]+1) == destination.getIndex())) {
            logger.fine ("Destination state does not match negative constraint. Assigning -infinite weight. position="+(ip+1)+", constraint="+(constraints[ip+1]+1)+", destination="+destination.getIndex());
            legalTransition = false;
          }

          if (logger.isLoggable (Level.FINE))
            logger.fine ("Forward Lattice[inputPos="+ip
                +"][source="+s.getName()
                +"][dest="+destination.getName()+"]");
          LatticeNode destinationNode = getLatticeNode (ip+1, destination.getIndex());
          destinationNode.output = iter.getOutput();
          double transitionWeight = iter.getWeight();
          if (legalTransition) {
            //if (logger.isLoggable (Level.FINE))
            logger.fine ("transitionWeight="+transitionWeight
                +" nodes["+ip+"]["+i+"].alpha="+nodes[ip][i].alpha
                +" destinationNode.alpha="+destinationNode.alpha);
            destinationNode.alpha = Transducer.sumLogProb (destinationNode.alpha,
                nodes[ip][i].alpha + transitionWeight);
            //System.out.println ("destinationNode.alpha <- "+destinationNode.alpha);
            logger.fine ("Set alpha of latticeNode at ip = "+ (ip+1) + " stateIndex = " + destination.getIndex() + ", destinationNode.alpha = " + destinationNode.alpha);
          }
          else {
            // this is an illegal transition according to our
            // constraints, so set its prob to 0 . NO, alpha's are
            // unnormalized weights...set to -Inf //
            // destinationNode.alpha = 0.0;
//            destinationNode.alpha = IMPOSSIBLE_WEIGHT;
            logger.fine ("Illegal transition from state " + i + " to state " + destination.getIndex() + ". Setting alpha to -Inf");
          }
        }
      }

    // Calculate total weight of Lattice.  This is the normalizer
    totalWeight = Transducer.IMPOSSIBLE_WEIGHT;
    for (int i = 0; i < numStates; i++)
      if (nodes[latticeLength-1][i] != null) {
        // Note: actually we could sum at any ip index,
        // the choice of latticeLength-1 is arbitrary
        //System.out.println ("Ending alpha, state["+i+"] = "+nodes[latticeLength-1][i].alpha);
        //System.out.println ("Ending beta,  state["+i+"] = "+getState(i).finalWeight);
        if (constraints[latticeLength-1] > 0 && i != constraints[latticeLength-1]-1)
          continue;
        if (constraints[latticeLength-1] < 0 && -i == constraints[latticeLength-1]+1)
          continue;
        logger.fine ("Summing final lattice weight. state="+i+", alpha="+nodes[latticeLength-1][i].alpha + ", final weight = "+t.getState(i).getFinalWeight());
        totalWeight = Transducer.sumLogProb (totalWeight,
            (nodes[latticeLength-1][i].alpha + t.getState(i).getFinalWeight()));
      }
    // Weight is now an "unnormalized weight" of the entire Lattice
    //assert (weight >= 0) : "weight = "+weight;

    // If the sequence has -infinite weight, just return.
    // Usefully this avoids calling any incrementX methods.
    // It also relies on the fact that the gammas[][] and .alpha and .beta values
    // are already initialized to values that reflect -infinite weight
    // xxx Although perhaps not all (alphas,betas) exactly correctly reflecting?
    if (totalWeight == Transducer.IMPOSSIBLE_WEIGHT)
      return;

    // Backward pass
    for (int i = 0; i < numStates; i++)
      if (nodes[latticeLength-1][i] != null) {
        State s = t.getState(i);
        nodes[latticeLength-1][i].beta = s.getFinalWeight();
        gammas[latticeLength-1][i] =
          nodes[latticeLength-1][i].alpha + nodes[latticeLength-1][i].beta - totalWeight;
        if (incrementor != null) {
          double p = Math.exp(gammas[latticeLength-1][i]);
          assert (p >= 0.0 && p <= 1.0 && !Double.isNaN(p))  : "p="+p+" gamma="+gammas[latticeLength-1][i];
          incrementor.incrementFinalState(s, p);
        }
      }
    for (int ip = latticeLength-2; ip >= 0; ip--) {
      for (int i = 0; i < numStates; i++) {
        if (nodes[ip][i] == null || nodes[ip][i].alpha == Transducer.IMPOSSIBLE_WEIGHT)
          // Note that skipping here based on alpha means that beta values won't
          // be correct, but since alpha is -infinite anyway, it shouldn't matter.
          continue;
        State s = t.getState(i);
        TransitionIterator iter = s.transitionIterator (input, ip, output, ip);
        while (iter.hasNext()) {
          State destination = iter.nextState();
          if (logger.isLoggable (Level.FINE))
            logger.fine ("Backward Lattice[inputPos="+ip
                +"][source="+s.getName()
                +"][dest="+destination.getName()+"]");
          int j = destination.getIndex();
          LatticeNode destinationNode = nodes[ip+1][j];
          if (destinationNode != null) {
            double transitionWeight = iter.getWeight();
            assert (!Double.isNaN(transitionWeight));
            //              assert (transitionWeight >= 0);  Not necessarily
View Full Code Here


    for (int ip = 0; ip < latticeLength - 1; ip++) {
      for (int i = 0; i < numStates; i++) {
        if (isInvalidNode(ip, i))
          continue;
        State s = t.getState(i);
        TransitionIterator iter = s.transitionIterator(input, ip,
            output, ip);
        while (iter.hasNext()) {
          State destination = iter.next();
          LatticeNode destinationNode = getLatticeNode(ip + 1,
              destination.getIndex());
          if (Double.isNaN(destinationNode.alpha))
            destinationNode.alpha = 0;
          destinationNode.output = iter.getOutput();
          double transitionWeight = iter.getWeight();
          destinationNode.alpha += nodes[ip][i].alpha
              * Math.exp(transitionWeight);
        }
      }
      // re-scale alphas to so that \sum_i \alpha[ip][i] = 1
      rescaleAlphas(ip + 1);
    }

    // Calculate total weight of Lattice. This is the normalizer
    double Z = Double.NaN;
    for (int i = 0; i < numStates; i++)
      if (nodes[latticeLength - 1][i] != null) {
        if (Double.isNaN(Z))
          Z = 0;
        Z += nodes[latticeLength - 1][i].alpha
            * Math.exp(t.getState(i).getFinalWeight());
      }
    zLogScaling = alphaLogScaling[latticeLength - 1];

    if (Double.isNaN(Z)) {
      totalWeight = Transducer.IMPOSSIBLE_WEIGHT;
      return;
    } else
      totalWeight = Math.log(Z) + zLogScaling;

    // Backward pass
    for (int i = 0; i < numStates; i++)
      if (nodes[latticeLength - 1][i] != null) {
        State s = t.getState(i);
        nodes[latticeLength - 1][i].beta = Math.exp(s.getFinalWeight());
        double gamma = nodes[latticeLength - 1][i].alpha
            * nodes[latticeLength - 1][i].beta / Z;
        gammas[latticeLength - 1][i] = Math.log(gamma);
        if (incrementor != null) {
          double p = gamma;
          assert (p >= 0.0 && p <= 1.0 + 1e-6) : "p=" + p
              + ", gamma=" + gammas[latticeLength - 1][i];
          incrementor.incrementFinalState(s, p);
        }
      }
    rescaleBetas(latticeLength - 1);

    for (int ip = latticeLength - 2; ip >= 0; ip--) {
      for (int i = 0; i < numStates; i++) {
        if (isInvalidNode(ip, i))
          continue;
        State s = t.getState(i);
        TransitionIterator iter = s.transitionIterator(input, ip,
            output, ip);
        double logScaling = alphaLogScaling[ip]
            + betaLogScaling[ip + 1] - zLogScaling;
        double pscaling = Math.exp(logScaling);
        while (iter.hasNext()) {
          State destination = iter.next();
          int j = destination.getIndex();
          LatticeNode destinationNode = nodes[ip + 1][j];
          if (destinationNode != null) {
            double transitionWeight = iter.getWeight();
            if (Double.isNaN(nodes[ip][i].beta))
              nodes[ip][i].beta = 0;
View Full Code Here

      }
    }

    for (int ip = 0; ip < latticeLength - 1; ip++)
      for (int i = 0; i < numStates; i++) {
        State s = t.getState(i);
        TransitionIterator iter = s.transitionIterator(input, ip);
        while (iter.hasNext()) {
          State destination = iter.next();
          double weight = iter.getWeight();
          double p = xis[ip][i][destination.getIndex()];
          totalWeight += p * weight;
          if (cachedDots != null) {
            cachedDots[ip][i][destination.getIndex()] = weight;
          }
          if (incrementor != null) {
            // this is used to gather "constraints",
            // so only probabilities under q are used
            incrementor.incrementTransition(iter, p);
View Full Code Here

      for (int i = 0; i < numStates; i++) {
        if (nodes[ip][i] == null || nodes[ip][i].alpha == Transducer.IMPOSSIBLE_WEIGHT)
          // xxx if we end up doing this a lot,
          // we could save a list of the non-null ones
          continue;
        State s = t.getState(i);
        CachedDotTransitionIterator iter =
          new CachedDotTransitionIterator((CRF.State)s,input,ip,
              null,cachedDots[ip][i]);
        if (logger.isLoggable (Level.FINE))
          logger.fine (" Starting Foward transition iteration from state "
              + s.getName() + " on input " + input.get(ip).toString()
              + " and output "
              + (output==null ? "(null)" : output.get(ip).toString()));
        while (iter.hasNext()) {
          State destination = iter.nextState();
          if (logger.isLoggable (Level.FINE))
            logger.fine ("Forward Lattice[inputPos="+ip+"][source="+s.getName()+"][dest="+destination.getName()+"]");
          LatticeNode destinationNode = getLatticeNode (ip+1, destination.getIndex());
          destinationNode.output = iter.getOutput();
          double transitionWeight = iter.getWeight();
          if (logger.isLoggable (Level.FINE))
            logger.fine ("BEFORE update: destinationNode.alpha="+destinationNode.alpha);
          destinationNode.alpha = Transducer.sumLogProb (destinationNode.alpha,  nodes[ip][i].alpha + transitionWeight);
          if (logger.isLoggable (Level.FINE))
            logger.fine ("transitionWeight="+transitionWeight+" nodes["+ip+"]["+i+"].alpha="+nodes[ip][i].alpha
                +" destinationNode.alpha="+destinationNode.alpha);
          //System.out.println ("destinationNode.alpha <- "+destinationNode.alpha);
        }
      }
   
    if (logger.isLoggable (Level.FINE)) {
      logger.fine("Forward Lattice:");
      for (int ip = 0; ip < latticeLength; ip++) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < numStates; i++)
          sb.append (" "+(nodes[ip][i] == null ? "<null>" : nodes[ip][i].alpha));
        logger.fine(sb.toString());
      }
    }

   
    // Calculate total weight of Lattice.  This is the normalizer
    totalWeight = Transducer.IMPOSSIBLE_WEIGHT;
    for (int i = 0; i < numStates; i++)
      if (nodes[latticeLength-1][i] != null) {
        //System.out.println ("Ending alpha, state["+i+"] = "+nodes[latticeLength-1][i].alpha);
        //System.out.println ("Ending beta,  state["+i+"] = "+t.getState(i).getFinalWeight());
        totalWeight = Transducer.sumLogProb (totalWeight,  (nodes[latticeLength-1][i].alpha + t.getState(i).getFinalWeight()));
      }
    logger.fine ("totalWeight="+totalWeight);
    // totalWeight is now an "unnormalized weight" of the entire Lattice

    // If the sequence has -infinite weight, just return.
    // Usefully this avoids calling any incrementX methods.
    // It also relies on the fact that the gammas[][] and .alpha (but not .beta) values
    // are already initialized to values that reflect -infinite weight
    // TODO Is it important to fill in the betas before we return?
    if (totalWeight == Transducer.IMPOSSIBLE_WEIGHT)
      return;

    // Backward pass
    for (int i = 0; i < numStates; i++)
      if (nodes[latticeLength-1][i] != null) {
        State s = t.getState(i);
        nodes[latticeLength-1][i].beta = s.getFinalWeight();
        gammas[latticeLength-1][i] = nodes[latticeLength-1][i].alpha + nodes[latticeLength-1][i].beta - totalWeight;
        if (incrementor != null) {
          double p = Math.exp(gammas[latticeLength-1][i]);
          // gsc: reducing from 1e-10 to 1e-6
          // gsc: removing the isNaN check, range check will catch the NaN error as well
          // assert (p >= 0.0 && p <= 1.0+1e-10 && !Double.isNaN(p)) : "p="+p+" gamma="+gammas[latticeLength-1][i];
          assert (p >= 0.0 && p <= 1.0+1e-6) : "p="+p+", gamma="+gammas[latticeLength-1][i];
          incrementor.incrementFinalState (s, p);
        }
      }

    for (int ip = latticeLength-2; ip >= 0; ip--) {
      for (int i = 0; i < numStates; i++) {
        if (nodes[ip][i] == null || nodes[ip][i].alpha == Transducer.IMPOSSIBLE_WEIGHT)
          // Note that skipping here based on alpha means that beta values won't
          // be correct, but since alpha is infinite anyway, it shouldn't matter.
          continue;
        State s = t.getState(i);
        CachedDotTransitionIterator iter =
          new CachedDotTransitionIterator((CRF.State)s,input,ip,
              null,cachedDots[ip][i]);
        while (iter.hasNext()) {
          State destination = iter.nextState();
          if (logger.isLoggable (Level.FINE))
            logger.fine ("Backward Lattice[inputPos="+ip+"][source="+s.getName()+"][dest="+destination.getName()+"]");
          int j = destination.getIndex();
          LatticeNode destinationNode = nodes[ip+1][j];
          if (destinationNode != null) {
            double transitionWeight = iter.getWeight();
            assert (!Double.isNaN(transitionWeight));
            double oldBeta = nodes[ip][i].beta;
View Full Code Here

        // xxx if we end up doing this a lot,
        // we could save a list of the non-null ones
        //  continue;


        State s = t.getState(i);

        TransitionIterator iter = s.transitionIterator (input, ip, output, ip);
        if (logger.isLoggable (Level.FINE))
          logger.fine (" Starting Foward transition iteration from state "
              + s.getName() + " on input " + input.get(ip).toString()
              + " and output "
              + (output==null ? "(null)" : output.get(ip).toString()));
        while (iter.hasNext()) {
          State destination = iter.nextState();
          if (logger.isLoggable (Level.FINE))
            logger.fine ("Forward Lattice[inputPos="+ip
                +"][source="+s.getName()
                +"][dest="+destination.getName()+"]");
          LatticeNode destinationNode = getLatticeNode (ip+1, destination.getIndex());
          destinationNode.output = iter.getOutput();
          double transitionWeight = iter.getWeight();
          if (logger.isLoggable (Level.FINE))
            logger.fine ("transitionWeight="+transitionWeight
                +" nodes["+ip+"]["+i+"].alpha="+nodes[ip][i].alpha
                +" destinationNode.alpha="+destinationNode.alpha);
          destinationNode.alpha = Transducer.sumLogProb (destinationNode.alpha,
              nodes[ip][i].alpha + transitionWeight);
          //System.out.println ("destinationNode.alpha <- "+destinationNode.alpha);
        }
      }
    }

    //System.out.println("Mean Nodes Explored: " + MatrixOps.mean(nstatesExpl));
    curAvgNstatesExpl = MatrixOps.mean(nstatesExpl);

    // Calculate total cost of Lattice.  This is the normalizer
    weight = Transducer.IMPOSSIBLE_WEIGHT;
    for (int i = 0; i < numStates; i++)
      if (nodes[latticeLength-1][i] != null) {
        // Note: actually we could sum at any ip index,
        // the choice of latticeLength-1 is arbitrary
        //System.out.println ("Ending alpha, state["+i+"] = "+nodes[latticeLength-1][i].alpha);
        //System.out.println ("Ending beta,  state["+i+"] = "+t.getState(i).finalWeight);
        weight = Transducer.sumLogProb (weight,
            (nodes[latticeLength-1][i].alpha + t.getState(i).getFinalWeight()));
      }
    // Weight is now an "unnormalized weight" of the entire Lattice
    //assert (weight >= 0) : "weight = "+weight;

    // If the sequence has -infinite weight, just return.
    // Usefully this avoids calling any incrementX methods.
    // It also relies on the fact that the gammas[][] and .alpha and .beta values
    // are already initialized to values that reflect -infinite weight
    // xxx Although perhaps not all (alphas,betas) exactly correctly reflecting?
    if (weight == Transducer.IMPOSSIBLE_WEIGHT)
      return;

    // Backward pass
    for (int i = 0; i < numStates; i++)
      if (nodes[latticeLength-1][i] != null) {
        State s = t.getState(i);
        nodes[latticeLength-1][i].beta = s.getFinalWeight();
        gammas[latticeLength-1][i] =
          nodes[latticeLength-1][i].alpha + nodes[latticeLength-1][i].beta - weight;
        if (incrementor != null) {
          double p = Math.exp(gammas[latticeLength-1][i]);
          assert (p > Transducer.IMPOSSIBLE_WEIGHT && !Double.isNaN(p))
          : "p="+p+" gamma="+gammas[latticeLength-1][i];
          incrementor.incrementFinalState(s, p);
        }
      }

    for (int ip = latticeLength-2; ip >= 0; ip--) {
      for (int i = 0; i < numStates; i++) {
        if (nodes[ip][i] == null || nodes[ip][i].alpha == Transducer.IMPOSSIBLE_WEIGHT)
          // Note that skipping here based on alpha means that beta values won't
          // be correct, but since alpha is infinite anyway, it shouldn't matter.
          continue;
        State s = t.getState(i);
        TransitionIterator iter = s.transitionIterator (input, ip, output, ip);
        while (iter.hasNext()) {
          State destination = iter.nextState();
          if (logger.isLoggable (Level.FINE))
            logger.fine ("Backward Lattice[inputPos="+ip
                +"][source="+s.getName()
                +"][dest="+destination.getName()+"]");
          int j = destination.getIndex();
          LatticeNode destinationNode = nodes[ip+1][j];
          if (destinationNode != null) {
            double transitionWeight = iter.getWeight();
            assert (!Double.isNaN(transitionWeight));
            //              assert (transitionWeight >= 0);  Not necessarily
View Full Code Here

          if (nodes[ip][i] == null) logger.fine ("nodes[ip][i] is NULL");
          else if (nodes[ip][i].alpha == Transducer.IMPOSSIBLE_WEIGHT) logger.fine ("nodes[ip][i].alpha is Inf");
          logger.fine ("-INFINITE weight or NULL...skipping");
          continue;
        }
        State s = t.getState(i);

        TransitionIterator iter = s.transitionIterator (input, ip, output, ip);
        if (logger.isLoggable (Level.FINE))
          logger.fine (" Starting Forward transition iteration from state "
              + s.getName() + " on input " + input.get(ip).toString()
              + " and output "
              + (output==null ? "(null)" : output.get(ip).toString()));
        while (iter.hasNext()) {
          State destination = iter.nextState();
          boolean legalTransition = true;
          // check constraints to see if node at <ip,i> can transition to destination
          if (ip+1 < constraints.length && constraints[ip+1] > 0 && ((constraints[ip+1]-1) != destination.getIndex())) {
            logger.fine ("Destination state does not match positive constraint. Assigning -infinite weight. position="+(ip+1)+", constraint="+(constraints[ip+1]-1)+", source ="+i+", destination="+destination.getIndex());
            legalTransition = false;
          }
          else if (((ip+1) < constraints.length) && constraints[ip+1] < 0 && (-(constraints[ip+1]+1) == destination.getIndex())) {
            logger.fine ("Destination state does not match negative constraint. Assigning -infinite weight. position="+(ip+1)+", constraint="+(constraints[ip+1]+1)+", destination="+destination.getIndex());
            legalTransition = false;
          }

          if (logger.isLoggable (Level.FINE))
            logger.fine ("Forward Lattice[inputPos="+ip
                +"][source="+s.getName()
                +"][dest="+destination.getName()+"]");
          LatticeNode destinationNode = getLatticeNode (ip+1, destination.getIndex());
          destinationNode.output = iter.getOutput();
          double transitionWeight = iter.getWeight();
          if (legalTransition) {
            //if (logger.isLoggable (Level.FINE))
            logger.fine ("transitionWeight="+transitionWeight
                +" nodes["+ip+"]["+i+"].alpha="+nodes[ip][i].alpha
                +" destinationNode.alpha="+destinationNode.alpha);
            destinationNode.alpha = Transducer.sumLogProb (destinationNode.alpha,
                nodes[ip][i].alpha + transitionWeight);
            //System.out.println ("destinationNode.alpha <- "+destinationNode.alpha);
            logger.fine ("Set alpha of latticeNode at ip = "+ (ip+1) + " stateIndex = " + destination.getIndex() + ", destinationNode.alpha = " + destinationNode.alpha);
          }
          else {
            // this is an illegal transition according to our
            // constraints, so set its prob to 0 . NO, alpha's are
            // unnormalized weights...set to Inf //
            // destinationNode.alpha = 0.0;
//            destinationNode.alpha = Transducer.IMPOSSIBLE_WEIGHT;
            logger.fine ("Illegal transition from state " + i + " to state " + destination.getIndex() + ". Setting alpha to Inf");
          }
        }
      }

    // Calculate total weight of Lattice.  This is the normalizer
    weight = Transducer.IMPOSSIBLE_WEIGHT;
    for (int i = 0; i < numStates; i++)
      if (nodes[latticeLength-1][i] != null) {
        // Note: actually we could sum at any ip index,
        // the choice of latticeLength-1 is arbitrary
        //System.out.println ("Ending alpha, state["+i+"] = "+nodes[latticeLength-1][i].alpha);
        //System.out.println ("Ending beta,  state["+i+"] = "+t.getState(i).finalWeight);
        if (constraints[latticeLength-1] > 0 && i != constraints[latticeLength-1]-1)
          continue;
        if (constraints[latticeLength-1] < 0 && -i == constraints[latticeLength-1]+1)
          continue;
        logger.fine ("Summing final lattice weight. state="+i+", alpha="+nodes[latticeLength-1][i].alpha + ", final weight = "+t.getState(i).getFinalWeight());
        weight = Transducer.sumLogProb (weight,
            (nodes[latticeLength-1][i].alpha + t.getState(i).getFinalWeight()));
      }
    // Weight is now an "unnormalized weight" of the entire Lattice
    //assert (weight >= 0) : "weight = "+weight;

    // If the sequence has -infinite weight, just return.
    // Usefully this avoids calling any incrementX methods.
    // It also relies on the fact that the gammas[][] and .alpha and .beta values
    // are already initialized to values that reflect -infinite weight
    // xxx Although perhaps not all (alphas,betas) exactly correctly reflecting?
    if (weight == Transducer.IMPOSSIBLE_WEIGHT)
      return;

    // Backward pass
    for (int i = 0; i < numStates; i++)
      if (nodes[latticeLength-1][i] != null) {
        State s = t.getState(i);
        nodes[latticeLength-1][i].beta = s.getFinalWeight();
        gammas[latticeLength-1][i] =
          nodes[latticeLength-1][i].alpha + nodes[latticeLength-1][i].beta - weight;
        if (incrementor != null) {
          double p = Math.exp(gammas[latticeLength-1][i]);
          assert (p >= 0 && p <= 1.0 && !Double.isNaN(p)) : "p="+p+" gamma="+gammas[latticeLength-1][i];
          incrementor.incrementFinalState(s, p);
        }
      }
    for (int ip = latticeLength-2; ip >= 0; ip--) {
      for (int i = 0; i < numStates; i++) {
        if (nodes[ip][i] == null || nodes[ip][i].alpha == Transducer.IMPOSSIBLE_WEIGHT)
          // Note that skipping here based on alpha means that beta values won't
          // be correct, but since alpha is infinite anyway, it shouldn't matter.
          continue;
        State s = t.getState(i);
        TransitionIterator iter = s.transitionIterator (input, ip, output, ip);
        while (iter.hasNext()) {
          State destination = iter.nextState();
          if (logger.isLoggable (Level.FINE))
            logger.fine ("Backward Lattice[inputPos="+ip
                +"][source="+s.getName()
                +"][dest="+destination.getName()+"]");
          int j = destination.getIndex();
          LatticeNode destinationNode = nodes[ip+1][j];
          if (destinationNode != null) {
            double transitionWeight = iter.getWeight();
            assert (!Double.isNaN(transitionWeight));
            //              assert (transitionWeight >= 0);  Not necessarily
View Full Code Here

      for (int i = 0; i < numStates; i++) {
        if (nodes[ip][i] == null || nodes[ip][i].alpha == Transducer.IMPOSSIBLE_WEIGHT)
          // xxx if we end up doing this a lot,
          // we could save a list of the non-null ones
          continue;
        State s = t.getState(i);
        TransitionIterator iter = s.transitionIterator (input, ip, output, ip);
        if (logger.isLoggable (Level.FINE))
          logger.fine (" Starting Foward transition iteration from state "
              + s.getName() + " on input " + input.get(ip).toString()
              + " and output "
              + (output==null ? "(null)" : output.get(ip).toString()));
        while (iter.hasNext()) {
          State destination = iter.nextState();
          if (logger.isLoggable (Level.FINE))
            logger.fine ("Forward Lattice[inputPos="+ip+"][source="+s.getName()+"][dest="+destination.getName()+"]");
          LatticeNode destinationNode = getLatticeNode (ip+1, destination.getIndex());
          destinationNode.output = iter.getOutput();
          double transitionWeight = iter.getWeight();
          if (logger.isLoggable (Level.FINE))
            logger.fine ("BEFORE update: destinationNode.alpha="+destinationNode.alpha);
          destinationNode.alpha = Transducer.sumLogProb (destinationNode.alpha,  nodes[ip][i].alpha + transitionWeight);
          if (logger.isLoggable (Level.FINE))
            logger.fine ("transitionWeight="+transitionWeight+" nodes["+ip+"]["+i+"].alpha="+nodes[ip][i].alpha
                +" destinationNode.alpha="+destinationNode.alpha);
          //System.out.println ("destinationNode.alpha <- "+destinationNode.alpha);
        }
      }
   
    if (logger.isLoggable (Level.FINE)) {
      logger.fine("Forward Lattice:");
      for (int ip = 0; ip < latticeLength; ip++) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < numStates; i++)
          sb.append (" "+(nodes[ip][i] == null ? "<null>" : nodes[ip][i].alpha));
        logger.fine(sb.toString());
      }
    }

   
    // Calculate total weight of Lattice.  This is the normalizer
    totalWeight = Transducer.IMPOSSIBLE_WEIGHT;
    for (int i = 0; i < numStates; i++)
      if (nodes[latticeLength-1][i] != null) {
        //System.out.println ("Ending alpha, state["+i+"] = "+nodes[latticeLength-1][i].alpha);
        //System.out.println ("Ending beta,  state["+i+"] = "+t.getState(i).getFinalWeight());
        totalWeight = Transducer.sumLogProb (totalWeight,  (nodes[latticeLength-1][i].alpha + t.getState(i).getFinalWeight()));
      }
    logger.fine ("totalWeight="+totalWeight);
    // totalWeight is now an "unnormalized weight" of the entire Lattice

    // If the sequence has -infinite weight, just return.
    // Usefully this avoids calling any incrementX methods.
    // It also relies on the fact that the gammas[][] and .alpha (but not .beta) values
    // are already initialized to values that reflect -infinite weight
    // TODO Is it important to fill in the betas before we return?
    if (totalWeight == Transducer.IMPOSSIBLE_WEIGHT)
      return;

    // Backward pass
    for (int i = 0; i < numStates; i++)
      if (nodes[latticeLength-1][i] != null) {
        State s = t.getState(i);
        nodes[latticeLength-1][i].beta = s.getFinalWeight();
        gammas[latticeLength-1][i] = nodes[latticeLength-1][i].alpha + nodes[latticeLength-1][i].beta - totalWeight;
        if (incrementor != null) {
          double p = Math.exp(gammas[latticeLength-1][i]);
          // gsc: reducing from 1e-10 to 1e-6
          // gsc: removing the isNaN check, range check will catch the NaN error as well
          // assert (p >= 0.0 && p <= 1.0+1e-10 && !Double.isNaN(p)) : "p="+p+" gamma="+gammas[latticeLength-1][i];
          assert (p >= 0.0 && p <= 1.0+1e-6) : "p="+p+", gamma="+gammas[latticeLength-1][i];
          incrementor.incrementFinalState (s, p);
        }
      }

    for (int ip = latticeLength-2; ip >= 0; ip--) {
      for (int i = 0; i < numStates; i++) {
        if (nodes[ip][i] == null || nodes[ip][i].alpha == Transducer.IMPOSSIBLE_WEIGHT)
          // Note that skipping here based on alpha means that beta values won't
          // be correct, but since alpha is infinite anyway, it shouldn't matter.
          continue;
        State s = t.getState(i);
        TransitionIterator iter = s.transitionIterator (input, ip, output, ip);
        while (iter.hasNext()) {
          State destination = iter.nextState();
          if (logger.isLoggable (Level.FINE))
            logger.fine ("Backward Lattice[inputPos="+ip+"][source="+s.getName()+"][dest="+destination.getName()+"]");
          int j = destination.getIndex();
          LatticeNode destinationNode = nodes[ip+1][j];
          if (destinationNode != null) {
            double transitionWeight = iter.getWeight();
            assert (!Double.isNaN(transitionWeight));
            double oldBeta = nodes[ip][i].beta;
View Full Code Here

        if (nodes[ip][i] == null
            || nodes[ip][i].alpha == Transducer.IMPOSSIBLE_WEIGHT) {
          continue;
        }
       
        State s = transducer.getState(i);
        CachedDotTransitionIterator iter =
          new CachedDotTransitionIterator((CRF.State)s,input,ip,
              null,cachedDots[ip][i]);

        auxModel.preProcess(index,ip,input);
        while (iter.hasNext()) {
          State destination = iter.next();
          LatticeNode destinationNode = getLatticeNode(ip + 1, destination.getIndex());
          destinationNode.output = iter.getOutput();
          double transitionWeight = iter.getWeight();
          transitionWeight += auxModel.getWeight(index,ip,input,iter);
          destinationNode.alpha = Transducer.sumLogProb(
              destinationNode.alpha, nodes[ip][i].alpha + transitionWeight);
        }
      }

    totalWeight = Transducer.IMPOSSIBLE_WEIGHT;
    for (int i = 0; i < numStates; i++) {
      if (nodes[latticeLength-1][i] != null) {
        totalWeight = Transducer.sumLogProb(totalWeight,
          (nodes[latticeLength-1][i].alpha + transducer.getState(i).getFinalWeight()));
      }
    }

    if (totalWeight == Transducer.IMPOSSIBLE_WEIGHT) {
      return;
    }

    // Backward pass
    for (int i = 0; i < numStates; i++)
      if (nodes[latticeLength - 1][i] != null) {
        State s = transducer.getState(i);
        nodes[latticeLength - 1][i].beta = s.getFinalWeight();
        gammas[latticeLength - 1][i] = nodes[latticeLength - 1][i].alpha
            + nodes[latticeLength - 1][i].beta - totalWeight;
        if (incrementor != null) {
          double p = Math.exp(gammas[latticeLength - 1][i]);
          assert (p >= 0.0 && p <= 1.0 + 1e-6) : "p=" + p
              + ", gamma=" + gammas[latticeLength - 1][i];
          incrementor.incrementFinalState(s, p);
        }
      }

    for (int ip = latticeLength - 2; ip >= 0; ip--) {
      for (int i = 0; i < numStates; i++) {
        if (nodes[ip][i] == null
            || nodes[ip][i].alpha == Transducer.IMPOSSIBLE_WEIGHT)
          continue;
        State s = transducer.getState(i);
        CachedDotTransitionIterator iter =
          new CachedDotTransitionIterator((CRF.State)s,input,ip,
              null,cachedDots[ip][i]);
        auxModel.preProcess(index,ip,input);
        while (iter.hasNext()) {
          State destination = iter.next();
          int j = destination.getIndex();
          LatticeNode destinationNode = nodes[ip + 1][j];
          if (destinationNode != null) {
            double transitionWeight = iter.getWeight();
            transitionWeight += auxModel.getWeight(index,ip,input,iter);
View Full Code Here

        cache.init(position);
      }
      for (int i = 0; i < t.numStates(); i++) {
        if (lattice[position][i] == null || lattice[position][i].delta == Transducer.IMPOSSIBLE_WEIGHT)
          continue;
        State s = t.getState(i);
        TransitionIterator iter =
          s.transitionIterator (input, position, providedOutput, position);
        while (iter.hasNext()) {
          State d = iter.next();
          cache.weight[i][d.getIndex()] = iter.getWeight();
        }
      }       
      caches[position] = cache;
    }
    if (cache != first) {           // Move to front
View Full Code Here

    for (int ip = 0; ip < latticeLength-1; ip++)
      for (int i = 0; i < numStates; i++) {
        if (lattice[ip][i] == null || lattice[ip][i].delta == Transducer.IMPOSSIBLE_WEIGHT)
          continue;
        State s = t.getState(i);
        TransitionIterator iter = s.transitionIterator (input, ip, providedOutput, ip);
        if (logger.isLoggable (Level.FINE))
          logger.fine (" Starting Viterbi transition iteration from state "
              + s.getName() + " on input " + input.get(ip));
        while (iter.hasNext()) {
          State destination = iter.next();
          if (logger.isLoggable (Level.FINE))
            logger.fine ("Viterbi[inputPos="+ip
                +"][source="+s.getName()
                +"][dest="+destination.getName()+"]");
          ViterbiNode destinationNode = getViterbiNode (ip+1, destination.getIndex());
          destinationNode.output = iter.getOutput();
          double weight = lattice[ip][i].delta + iter.getWeight();
          if (ip == latticeLength-2) {
            weight += destination.getFinalWeight();
          }
          if (weight > destinationNode.delta) {
            if (logger.isLoggable (Level.FINE))
              logger.fine ("Viterbi[inputPos="+ip
                  +"][source][dest="+destination.getName()
                  +"] weight increased to "+weight+" by source="+
                  s.getName());
            destinationNode.delta = weight;
            destinationNode.maxWeightPredecessor = lattice[ip][i];
          }
View Full Code Here

TOP

Related Classes of cc.mallet.fst.Transducer.State

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.