Package cc.mallet.fst

Examples of cc.mallet.fst.SumLatticeDefault


    //System.err.println("Used Memory "+String.format("%.3f", (Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory())/1000000.) + " before lattice");  
    ArrayList<SumLattice> lattices = new ArrayList<SumLattice>();
    if (numThreads == 1) {
      for (int ii = 0; ii < data.size(); ii++) {
        if (instancesWithConstraints.get(ii)) {
          SumLatticeDefault lattice = new SumLatticeDefault(
              this.crf, (FeatureVectorSequence)data.get(ii).getData(),
              null, null, true);
          lattices.add(lattice);
        }
        else {
          lattices.add(null);
        }
      }
    }
    else {
      // mutli-threaded version
      ArrayList<Callable<Void>> tasks = new ArrayList<Callable<Void>>();
     
      if (data.size() < numThreads) {
        numThreads = data.size();
      }
     
      int increment = data.size() / numThreads;
      int start = 0;
      int end = increment;
      for (int thread = 0; thread < numThreads; thread++) {
        tasks.add(new SumLatticeTask(crf,data,instancesWithConstraints,start,end));
        start += increment;
        if (thread == numThreads - 2) {
          end = data.size();
        }
        else {
          end += increment;
        }
      }
     
      try {
        // run all threads and wait for them to finish
        executor.invokeAll(tasks);
      } catch (InterruptedException ie) {
        ie.printStackTrace();
      }
     
      for (Callable<Void> task : tasks) {
        lattices.addAll(((SumLatticeTask)task).getLattices());
      }
      assert(lattices.size() == data.size()) : lattices.size() + " " + data.size();
    }
    System.err.println("Done computing lattices.");
   
    for (GEConstraint constraint : constraints) {
      constraint.zeroExpectations();
      constraint.computeExpectations(lattices);
    }
    System.err.println("Done computing expectations.");
    //System.gc();
    //System.err.println("Used Memory "+String.format("%.3f", (Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory())/1000000.) + " after lattice"); 
   
    // compute GE value
    this.cachedValue = 0;
    for (GEConstraint constraint : constraints) {
      this.cachedValue += constraint.getValue();
    }
   
    cachedGradient.zero();
   
    // compute GE gradient
    if (numThreads == 1) {
      for (int ii = 0; ii < data.size(); ii++) {
        if (instancesWithConstraints.get(ii)) {
          SumLattice lattice = lattices.get(ii);
          FeatureVectorSequence fvs = (FeatureVectorSequence)data.get(ii).getData();
          new GELattice(fvs, lattice.getGammas(), lattice.getXis(), crf, reverseTrans, reverseTransIndices, cachedGradient,this.constraints, false);
        }
      }
    }
    else {
      // multi-threaded version
View Full Code Here


  public Void call() throws Exception {
    for (int ii = start; ii < end; ii++) {
      if (instancesWithConstraints.get(ii)) {
        Instance instance = data.get(ii);
        SumLatticeDefault lattice = new SumLatticeDefault(
          this.crf, (FeatureVectorSequence)instance.getData(),
          null, null, true);
        lattices.add(lattice);
      }
      else {
View Full Code Here

    expectations.zero();

    // now, update the expectations due to each instance for entropy reg.
    for (int ii = 0; ii < data.size(); ii++) {
      FeatureVectorSequence input = (FeatureVectorSequence) data.get(ii).getData();
      SumLattice lattice = new SumLatticeDefault(crf,input, true);

      // udpate the expectations
      EntropyLattice entropyLattice = new EntropyLattice(
          input, lattice.getGammas(), lattice.getXis(), crf,
          incrementor, scalingFactor);
      cachedValue += entropyLattice.getEntropy();
    }
  }
View Full Code Here

      crf = saveCRF;
    }
//    MEMM.OptimizableCRF mcrf = crf.getMaximizableCRF(ilist);
    Optimizable.ByGradientValue mcrf = memmt.getOptimizableMEMM(ilist);

    double unconstrainedCost = new SumLatticeDefault (crf, fvs).getTotalWeight();
    double constrainedCost = new SumLatticeDefault (crf, fvs, ss).getTotalWeight();
    double minimizableCost = 0, minimizableGradientNorm = 0;
    double[] gradient = new double [mcrf.getNumParameters()];
    //System.out.println ("unconstrainedCost="+unconstrainedCost+" constrainedCost="+constrainedCost);
    for (int i = 0; i < numStates; i++)
      for (int j = 0; j < numStates; j++)
        for (int k = 0; k < inputVocabSize; k++) {
          crf.setParameter(i, j, k, (k + i + j) * (k * i + i * j));
          unconstrainedCost = new SumLatticeDefault (crf, fvs).getTotalWeight();
          constrainedCost = new SumLatticeDefault (crf, fvs, ss).getTotalWeight();
          minimizableCost = mcrf.getValue ();
          mcrf.getValueGradient (gradient);
          minimizableGradientNorm = MatrixOps.oneNorm (gradient);
          System.out.println("parameters " + i + " " + j + " " + k
                             + ": unconstrainedCost=" + unconstrainedCost
View Full Code Here

    assertTrue (count == 1);
  }

  public void testForwardBackward ()
  {
    SumLatticeDefault lattice = new SumLatticeDefault (transducer, seq);
    System.out.println ("weight= "+lattice.getTotalWeight());
    assertTrue (lattice.getTotalWeight() == seqWeight);
  }
View Full Code Here

  }

  public void testEstimate ()
  {
    transducer.setTrainable (true);
    SumLatticeDefault lattice = new SumLatticeDefault (transducer, seq); // used to have third argument: true
    double oldWeight = lattice.getTotalWeight ();
    transducer.estimate ();
    lattice = new SumLatticeDefault (transducer, seq); // used to have third argument: false
    double newWeight = lattice.getTotalWeight ();
    System.out.println ("oldWeight="+oldWeight+" newWeight="+newWeight);
    assertTrue (newWeight < oldWeight);
  }
View Full Code Here

  }

  public void testIncrement ()
  {
    transducer.setTrainable (true);
    SumLatticeDefault lattice = new SumLatticeDefault (transducer, seq); // used to have third argument: true
    double oldWeight = lattice.getTotalWeight ();
    System.out.println ("State 0 transition estimator");
    Multinomial.Estimator est
      = ((FeatureTransducer.State)transducer.getState(0)).getTransitionEstimator();
    est.print();
    assertTrue (est.getCount(0) == 2.0);
View Full Code Here

            new FeatureVector((Alphabet) crf.getInputAlphabet(),
                new double[] { 1 }),
            new FeatureVector((Alphabet) crf.getInputAlphabet(),
                new double[] { 1 }), });

    SumLattice lattice = new SumLatticeDefault(crf, fvs, true);
    // We start in state0
    assertTrue(lattice.getGammaProbability(0, crf.getState(0)) == 1.0);
    assertTrue(lattice.getGammaProbability(0, crf.getState(1)) == 0.0);
    // We go to state1
    assertTrue(lattice.getGammaProbability(1, crf.getState(0)) == 0.0);
    assertTrue(lattice.getGammaProbability(1, crf.getState(1)) == 1.0);
    // And on through a self-transition
    assertTrue(lattice
        .getXiProbability(1, crf.getState(1), crf.getState(1)) == 1.0);
    assertTrue(lattice
        .getXiProbability(1, crf.getState(1), crf.getState(0)) == 0.0);
    assertTrue("Lattice weight = " + lattice.getTotalWeight(), lattice
        .getTotalWeight() == 4.0);
    // Gammas at all times sum to 1.0
    for (int time = 0; time < lattice.length() - 1; time++) {
      double gammasum = lattice
          .getGammaProbability(time, crf.getState(0))
          + lattice.getGammaProbability(time, crf.getState(1));
      assertEquals("Gammas at time step " + time + " sum to " + gammasum,
          1.0, gammasum, 0.0001);
    }
    // Xis at all times sum to 1.0
    for (int time = 0; time < lattice.length() - 1; time++) {
      double xissum = lattice.getXiProbability(time, crf.getState(0), crf
          .getState(0))
          + lattice.getXiProbability(time, crf.getState(0), crf
              .getState(1))
          + lattice.getXiProbability(time, crf.getState(1), crf
              .getState(0))
          + lattice.getXiProbability(time, crf.getState(1), crf
              .getState(1));
      assertEquals("Xis at time step " + time + " sum to " + xissum, 1.0,
          xissum, 0.0001);
    }
  }
View Full Code Here

      }
      System.err.println("Wrote out CRF");
      crf = saveCRF;
    }
    Optimizable.ByGradientValue mcrf = crft.getOptimizableCRF(ilist);
    double unconstrainedWeight = new SumLatticeDefault(crf, fvs)
        .getTotalWeight();
    double constrainedWeight = new SumLatticeDefault(crf, fvs, ss)
        .getTotalWeight();
    double optimizableValue = 0, gradientNorm = 0;
    double[] gradient = new double[mcrf.getNumParameters()];
    // System.out.println
    // ("unconstrainedCost="+unconstrainedCost+" constrainedCost="+constrainedCost);
    for (int i = 0; i < numStates; i++)
      for (int j = 0; j < numStates; j++)
        for (int k = 0; k < inputVocabSize; k++) {
          crf.setParameter(i, j, k, (k + i + j) * (k * i + i * j));
          unconstrainedWeight = new SumLatticeDefault(crf, fvs)
              .getTotalWeight();
          constrainedWeight = new SumLatticeDefault(crf, fvs, ss)
              .getTotalWeight();
          optimizableValue = mcrf.getValue();
          mcrf.getValueGradient(gradient);
          gradientNorm = MatrixOps.oneNorm(gradient);
          System.out.println("parameters " + i + " " + j + " " + k
View Full Code Here

    for (int iter = 0; iter < 10000; iter++) {
      for (int ii = 0; ii < lists[1].size(); ii++) {
        FeatureVectorSequence input = (FeatureVectorSequence) lists[1]
            .get(ii).getData();
        totalTimeDefault -= System.currentTimeMillis();
        SumLattice defaultLattice = new SumLatticeDefault(crf, input,
            true);
        totalTimeDefault += System.currentTimeMillis();
        totalTimeScaling -= System.currentTimeMillis();
        SumLattice scalingLattice = new SumLatticeScaling(crf, input,
            true);
        totalTimeScaling += System.currentTimeMillis();
        if (iter == 0) {
          // check that total weight is same
          assertEquals(defaultLattice.getTotalWeight(),
              scalingLattice.getTotalWeight(), 0.0001);
          // check that gammas
          double[][] g1 = defaultLattice.getGammas(), g2 = scalingLattice
              .getGammas();
          for (int i = 0; i < g1.length; i++) {
            for (int j = 0; j < g1[i].length; j++) {
              assertEquals(g1[i][j], g2[i][j], 0.0001);
            }
          }
          // check that xis match
          double[][][] x1 = defaultLattice.getXis(), x2 = scalingLattice
              .getXis();
          for (int i = 0; i < x1.length; i++) {
            for (int j = 0; j < x1[i].length; j++) {
              for (int k = 0; k < x1[i][j].length; k++) {
                assertEquals(x1[i][j][k], x2[i][j][k], 0.0001);
View Full Code Here

TOP

Related Classes of cc.mallet.fst.SumLatticeDefault

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.