Package cc.mallet.fst.Transducer

Examples of cc.mallet.fst.Transducer.TransitionIterator


          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
            double oldBeta = nodes[ip][i].beta;
            assert (!Double.isNaN(nodes[ip][i].beta));
            nodes[ip][i].beta = Transducer.sumLogProb (nodes[ip][i].beta,
                destinationNode.beta + transitionWeight);
            assert (!Double.isNaN(nodes[ip][i].beta))
            : "dest.beta="+destinationNode.beta+" trans="+transitionWeight+" sum="+(destinationNode.beta+transitionWeight)
            + " oldBeta="+oldBeta;
            // xis[ip][i][j] = nodes[ip][i].alpha + transitionWeight + nodes[ip+1][j].beta - weight;
            assert (!Double.isNaN(nodes[ip][i].alpha));
            assert (!Double.isNaN(transitionWeight));
            assert (!Double.isNaN(nodes[ip+1][j].beta));
            assert (!Double.isNaN(totalWeight));
            if (incrementor != null || outputAlphabet != null) {
              double xi = nodes[ip][i].alpha + transitionWeight + nodes[ip+1][j].beta - totalWeight;
              double p = Math.exp(xi);
              assert (p > Transducer.IMPOSSIBLE_WEIGHT && !Double.isNaN(p)) : "xis["+ip+"]["+i+"]["+j+"]="+xi;
              if (incrementor != null)
                incrementor.incrementTransition(iter, p);
              if (outputAlphabet != null) {
                int outputIndex = outputAlphabet.lookupIndex (iter.getOutput(), false);
                assert (outputIndex >= 0);
                // xxx This assumes that "ip" == "op"!
                outputCounts[ip][outputIndex] += p;
                //System.out.println ("CRF Lattice outputCounts["+ip+"]["+outputIndex+"]+="+p);
              }
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;
            double transitionProb = Math.exp(transitionWeight);
            nodes[ip][i].beta += destinationNode.beta
                * transitionProb;
            double xi = nodes[ip][i].alpha * transitionProb
                * nodes[ip + 1][j].beta / Z;
            if (saveXis)
              xis[ip][i][j] = Math.log(xi) + logScaling;
            if (incrementor != null || outputAlphabet != null) {
              double p = xi * pscaling;
              assert (p >= 0.0 && p <= 1.0 + 1e-6) : "p=" + p
                  + ", xis[" + ip + "][" + i + "][" + j
                  + "]=" + xi;
              if (incrementor != null)
                incrementor.incrementTransition(iter, p);
              if (outputAlphabet != null) {
                int outputIndex = outputAlphabet.lookupIndex(
                    iter.getOutput(), false);
                assert (outputIndex >= 0);
                outputCounts[ip][outputIndex] += p;
              }
            }
          }
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;
          }
View Full Code Here

        //  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
            double oldBeta = nodes[ip][i].beta;
            assert (!Double.isNaN(nodes[ip][i].beta));
            nodes[ip][i].beta = Transducer.sumLogProb (nodes[ip][i].beta,
                destinationNode.beta + transitionWeight);
            assert (!Double.isNaN(nodes[ip][i].beta))
            : "dest.beta="+destinationNode.beta+" trans="+transitionWeight+" sum="+(destinationNode.beta+transitionWeight)
            + " oldBeta="+oldBeta;
            double xi = nodes[ip][i].alpha + transitionWeight + nodes[ip+1][j].beta - weight;
            if (saveXis) xis[ip][i][j] = xi;
            assert (!Double.isNaN(nodes[ip][i].alpha));
            assert (!Double.isNaN(transitionWeight));
            assert (!Double.isNaN(nodes[ip+1][j].beta));
            assert (!Double.isNaN(weight));
            if (incrementor != null || outputAlphabet != null) {
              double p = Math.exp(xi);
              assert (p > Transducer.IMPOSSIBLE_WEIGHT && !Double.isNaN(p)) : "xis["+ip+"]["+i+"]["+j+"]="+xi;
              if (incrementor != null)
                incrementor.incrementTransition(iter, p);
              if (outputAlphabet != null) {
                int outputIndex = outputAlphabet.lookupIndex (iter.getOutput(), false);
                assert (outputIndex >= 0);
                // xxx This assumes that "ip" == "op"!
                outputCounts[ip][outputIndex] += p;
                //System.out.println ("CRF Lattice outputCounts["+ip+"]["+outputIndex+"]+="+p);
              }
View Full Code Here

          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
            double oldBeta = nodes[ip][i].beta;
            assert (!Double.isNaN(nodes[ip][i].beta));
            nodes[ip][i].beta = Transducer.sumLogProb (nodes[ip][i].beta,
                destinationNode.beta + transitionWeight);
            assert (!Double.isNaN(nodes[ip][i].beta))
            : "dest.beta="+destinationNode.beta+" trans="+transitionWeight+" sum="+(destinationNode.beta+transitionWeight)
            + " oldBeta="+oldBeta;
            // xis[ip][i][j] = nodes[ip][i].alpha + transitionWeight + nodes[ip+1][j].beta - weight;
            assert (!Double.isNaN(nodes[ip][i].alpha));
            assert (!Double.isNaN(transitionWeight));
            assert (!Double.isNaN(nodes[ip+1][j].beta));
            assert (!Double.isNaN(weight));
            if (incrementor != null || outputAlphabet != null) {
              double xi = nodes[ip][i].alpha + transitionWeight + nodes[ip+1][j].beta - weight;
              double p = Math.exp(xi);
              assert (p >= 0 && p <= 1.0 && !Double.isNaN(p)) : "xis["+ip+"]["+i+"]["+j+"]="+-xi;
              if (incrementor != null)
                incrementor.incrementTransition(iter, p);
              if (outputAlphabet != null) {
                int outputIndex = outputAlphabet.lookupIndex (iter.getOutput(), false);
                assert (outputIndex >= 0);
                // xxx This assumes that "ip" == "op"!
                outputCounts[ip][outputIndex] += p;
                //System.out.println ("CRF Lattice outputCounts["+ip+"]["+outputIndex+"]+="+p);
              }
View Full Code Here

        }
      }

      for (int j = 0; j < input.size(); j++) {
        for (int k = 0; k < crf.numStates(); k++) {
          TransitionIterator iter = crf.getState(k).transitionIterator(input, j);
          while (iter.hasNext()) {
            int l = iter.next().getIndex();
            cachedDots[i][j][k][l] = iter.getWeight();
          }
        }
      }
    }
  }
View Full Code Here

        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;
            assert (!Double.isNaN(nodes[ip][i].beta));
            nodes[ip][i].beta = Transducer.sumLogProb (nodes[ip][i].beta,  destinationNode.beta + transitionWeight);
            assert (!Double.isNaN(nodes[ip][i].beta))
            : "dest.beta="+destinationNode.beta+" trans="+transitionWeight+" sum="+(destinationNode.beta+transitionWeight+ " oldBeta="+oldBeta;
            double xi = nodes[ip][i].alpha + transitionWeight + nodes[ip+1][j].beta - totalWeight;
            if (saveXis) xis[ip][i][j] = xi;
            assert (!Double.isNaN(nodes[ip][i].alpha));
            assert (!Double.isNaN(transitionWeight));
            assert (!Double.isNaN(nodes[ip+1][j].beta));
            assert (!Double.isNaN(totalWeight));
            if (incrementor != null || outputAlphabet != null) {
              double p = Math.exp(xi);
              // 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)) : "xis["+ip+"]["+i+"]["+j+"]="+xi;
              assert (p >= 0.0 && p <= 1.0+1e-6) : "p="+p+", xis["+ip+"]["+i+"]["+j+"]="+xi;
              if (incrementor != null)
                incrementor.incrementTransition(iter, p);
              if (outputAlphabet != null) {
                int outputIndex = outputAlphabet.lookupIndex (iter.getOutput(), false);
                assert (outputIndex >= 0);
                // xxx This assumes that "ip" == "op"!
                outputCounts[ip][outputIndex] += p;
                //System.out.println ("CRF Lattice outputCounts["+ip+"]["+outputIndex+"]+="+p);
              }
View Full Code Here

      }
      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))
View Full Code Here

    // Increment the initial state
    incrementor.incrementInitialState(viterbiNodeAlignment.output().get(0).state, 1.0);
    // Increment the final state
    incrementor.incrementFinalState(viterbiNodeAlignment.output().get(sequenceLength-1).state, 1.0);
    for (int ip = 0; ip < viterbiNodeAlignment.input().size()-1; ip++) {
      TransitionIterator iter =
        viterbiNodeAlignment.output().get(ip).state.transitionIterator (input, ip, providedOutput, ip);
      // xxx This assumes that a transition is completely
      // identified, and made unique by its destination state and
      // output.  This may not be true!
      int numIncrements = 0;
      while (iter.hasNext()) {
        if (iter.next().equals (viterbiNodeAlignment.output().get(ip+1).state)
            && iter.getOutput().equals (viterbiNodeAlignment.output().get(ip).output)) {
          incrementor.incrementTransition(iter, 1.0);
          numIncrements++;
        }
      }
      if (numIncrements > 1)
View Full Code Here

TOP

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

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.