Package cc.mallet.types

Examples of cc.mallet.types.Instance


    private double computeValueForInstance (int i)
    {
      double retval = 0.0;

      Instance instance = trainData.get(i);

      /* Compute marginals for each clique */
      ACRF.UnrolledGraph unrolled = new ACRF.UnrolledGraph (instance, templates, fixedTmpls);

      if (unrolled.numVariables () == 0) return 0;   // Happens if all nodes are pruned.

      /* Save the expected value of each feature for when we
         compute the gradient. */
      Assignment observations = unrolled.getAssignment ();
      double value = collectExpectationsAndValue (unrolled, observations);

      if (Double.isNaN (value)) {
        System.out.println("NaN on instance "+i+" : "+instance.getName ());
        printDebugInfo (unrolled);
/*          throw new IllegalStateException
            ("Value is NaN in ACRF.getValue() Instance "+i);
*/
        logger.warning ("Value is NaN in ACRF.getValue() Instance "+i+" : "+
View Full Code Here


      }
    }

    private void collectConstraintsForInstance (InstanceList ilist, int inum)
    {
      Instance inst = ilist.get (inum);
      ACRF.UnrolledGraph unrolled = new ACRF.UnrolledGraph (inst, templates, null, false);
      for (Iterator it = unrolled.unrolledVarSetIterator (); it.hasNext();) {
        ACRF.UnrolledVarSet clique = (ACRF.UnrolledVarSet) it.next();
        int tidx = clique.getTemplate().index;
        if (tidx == -1) continue;
View Full Code Here

    }
   
    for (int i = 0; i < instances.size(); i++) {
      if (viterbiOutputStream != null)
        viterbiOutputStream.println ("Viterbi path for "+description+" instance #"+i);
      Instance instance = instances.get(i);
      Sequence input = (Sequence) instance.getData();
      TokenSequence sourceTokenSequence = null;
      if (instance.getSource() instanceof TokenSequence)
        sourceTokenSequence = (TokenSequence) instance.getSource();

      Sequence trueOutput = (Sequence) instance.getTarget();
      assert (input.size() == trueOutput.size());
      Sequence predOutput = transducerTrainer.getTransducer().transduce (input);
      assert (predOutput.size() == trueOutput.size());
     
      for (int j = 0; j < trueOutput.size(); j++) {
View Full Code Here

      FeatureInducer klfi = (FeatureInducer)featureInducers.get(i);
      klfi.induceFeaturesFor (testing, false, false);
    }
    Sequence[] ret = new Sequence[testing.size()];
    for (int i = 0; i < testing.size(); i++) {
      Instance instance = testing.get(i);
      Sequence input = (Sequence) instance.getData();
      Sequence trueOutput = (Sequence) instance.getTarget();
      assert (input.size() == trueOutput.size());
      Sequence predOutput = new MaxLatticeDefault(this, input).bestOutputSequence();
      assert (predOutput.size() == trueOutput.size());
      ret[i] = predOutput;
    }
View Full Code Here

    LabelAlphabet dict = (LabelAlphabet) pipe.getTargetAlphabet ();
    Extraction extraction = new Extraction (null, dict);

    for (int i = 0; i < pred.size(); i++) {
      Instance aPred = pred.get (i);
      Instance aTarget = targets.get (i);
      Tokenization input = (Tokenization) aPred.getData ();
      Sequence predSeq = (Sequence) aPred.getTarget ();
      Sequence targetSeq = (Sequence) aTarget.getTarget ();
      DocumentExtraction docextr = new DocumentExtraction ("TEST"+i, dict, input, predSeq, targetSeq, "O");
      extraction.addDocumentExtraction (docextr);
    }

    return extraction;
View Full Code Here

    public Instance next ()  {
      if (currentIndex >= currentTokenSequence.size()) {
        currentInstance = source.next();
        currentTokenSequence = (TokenSequence) currentInstance.getData();
      }
      Instance ret = new Instance (currentTokenSequence.get(currentIndex),
          ((LabelSequence)currentInstance.getTarget()).getLabelAtPosition(currentIndex),
          null, null);
      currentIndex++;
      return ret;
    }
View Full Code Here

      for(int j=0; j<numFeats; j++)
        this.weights[i][j] = 1.0;
    //System.out.println("Init weights to 1.  Theta= "+theta);
    // loop through all instances
    for (int ii = 0; ii < trainingList.size(); ii++){
      Instance inst = (Instance) trainingList.get(ii);
      Labeling labeling = inst.getLabeling ();
      FeatureVector fv = (FeatureVector) inst.getData ();
      double[] results = new double [numLabels];
      int fvisize = fv.numLocations();
      int correctIndex = labeling.getBestIndex();
     
      for(int rpos=0; rpos < numLabels; rpos++)
View Full Code Here

    int iteration = 0;
    while (!converged) {
      // Make a new trainingSet that has some labels set
      InstanceList trainingSet2 = new InstanceList (trainingSet.getPipe());
      for (int ii = 0; ii < trainingSet.size(); ii++) {
        Instance inst = trainingSet.get(ii);
        if (inst.getLabeling() != null)
          trainingSet2.add(inst, 1.0);
        else {
          Instance inst2 = inst.shallowCopy();
          inst2.unLock();
          inst2.setLabeling(c.classify(inst).getLabeling());
          inst2.lock();
          trainingSet2.add(inst2, unlabeledDataWeight);
        }
      }
      c = (NaiveBayes) nbTrainer.newClassifierTrainer().train (trainingSet2);
      logLikelihood = c.dataLogLikelihood (trainingSet2);
View Full Code Here

    double[][] scores = new double[trainingList.size()][numLabels];
    double[] constraintValue = new double[numLabels];
   
    // pass 1: calculate model distribution
    for (int ii = 0; ii < trainingList.size(); ii++) {
      Instance instance = trainingList.get(ii);
      double instanceWeight = trainingList.getInstanceWeight(instance);
     
      // skip if labeled
      if (instance.getTarget() != null) {
        continue;
      }
     
      FeatureVector fv = (FeatureVector) instance.getData();
      classifier.getClassificationScoresWithTemperature(instance, temperature, scores[ii]);
      for (MaxEntGEConstraint constraint : constraints) {
        constraint.computeExpectations(fv,scores[ii],instanceWeight);
      }
    }
   
    // compute value
    double value = 0;
    for (MaxEntGEConstraint constraint : constraints) {
      value += constraint.getValue();
    }
    value *= objWeight;

    // pass 2: determine per example gradient
    for (int ii = 0; ii < trainingList.size(); ii++) {
      Instance instance = trainingList.get(ii);
     
      // skip if labeled
      if (instance.getTarget() != null) {
        continue;
      }
     
      Arrays.fill(constraintValue,0);
      double instanceExpectation = 0;
      double instanceWeight = trainingList.getInstanceWeight(instance);
      FeatureVector fv = (FeatureVector) instance.getData();

      for (MaxEntGEConstraint constraint : constraints) {
        constraint.preProcess(fv);
        for (int label = 0; label < numLabels; label++) {
          double val = constraint.getCompositeConstraintFeatureValue(fv, label);
View Full Code Here

    values = new int[numClasses][numClasses];
    for(int i=0; i < classifications.size(); i++)
    {
      LabelVector lv =
        ((Classification)classifications.get(i)).getLabelVector();
      Instance inst = ((Classification)classifications.get(i)).getInstance();
      int bestIndex = lv.getBestIndex();
      int correctIndex = inst.getLabeling().getBestIndex();
      assert(correctIndex != -1);
      //System.out.println("Best index="+bestIndex+". Correct="+correctIndex);
      values[correctIndex][bestIndex]++;
    }     
  }
View Full Code Here

TOP

Related Classes of cc.mallet.types.Instance

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.