Package cc.mallet.types

Examples of cc.mallet.types.Labeling


    for (int j = 0; j < fvs.size(); j++) {
      FeatureVector fv = fvs.getFeatureVector(j);
      int[] indices = fv.getIndices();
      FeatureVector data = new AugmentableFeatureVector (alphabetsPipe.getDataAlphabet(),
          indices, fv.getValues(), indices.length);
      Labeling target = ls.getLabelAtPosition(j);
      String name = instName.toString() + "_@_POS_" + (j + 1);
      Object source = inst.getSource();
      Instance toAdd = alphabetsPipe.pipe(new Instance(data, target, name, source));

      ret.add(toAdd);
View Full Code Here


        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++)
        results[rpos]=0;
      // sum up xi*wi for each class
      for(int fvi=0; fvi < fvisize; fvi++){
View Full Code Here

   */
  public ConfusionMatrix(Trial t)
  {
    this.trial = t;
    this.classifications = t;
    Labeling tempLabeling =
      ((Classification)classifications.get(0)).getLabeling();
    this.numClasses = tempLabeling.getLabelAlphabet().size();
    values = new int[numClasses][numClasses];
    for(int i=0; i < classifications.size(); i++)
    {
      LabelVector lv =
        ((Classification)classifications.get(i)).getLabelVector();
View Full Code Here

    // Initialize the constraints
    logger.fine("Number of instances in training list = " + trainingList.size());
    for (Instance inst : trainingList) {
      double instanceWeight = trainingList.getInstanceWeight(inst);
      Labeling labeling = inst.getLabeling ();
      if (labeling == null)
        continue;
      //logger.fine ("Instance "+ii+" labeling="+labeling);
      FeatureVector fv = (FeatureVector) inst.getData ();
      Alphabet fdict = fv.getAlphabet();
      assert (fv.getAlphabet() == fd);
      int li = labeling.getBestIndex();
      MatrixOps.rowPlusEquals (constraints, numFeatures, li, fv, instanceWeight);
      // For the default feature, whose weight is 1.0
      assert(!Double.isNaN(instanceWeight)) : "instanceWeight is NaN";
      assert(!Double.isNaN(li)) : "bestIndex is NaN";
      boolean hasNaN = false;
View Full Code Here

      int ii=0;
      while (iter.hasNext()) {
        ii++;
        Instance instance = iter.next();
        double instanceWeight = trainingList.getInstanceWeight(instance);
        Labeling labeling = instance.getLabeling ();
        if (labeling == null)
          continue;
        //System.out.println("L Now "+inputAlphabet.size()+" regular features.");

        this.theClassifier.getClassificationScores (instance, scores);
        FeatureVector fv = (FeatureVector) instance.getData ();
        int li = labeling.getBestIndex();
        value = - (instanceWeight * Math.log (scores[li]));
        if(Double.isNaN(value)) {
          logger.fine ("MaxEntTrainer: Instance " + instance.getName() +
              "has NaN value. log(scores)= " + Math.log(scores[li]) +
              " scores = " + scores[li] +
View Full Code Here

    }
  }

  private void incorporateOneInstance (Instance instance, double instanceWeight)
  {
    Labeling labeling = instance.getLabeling ();
    if (labeling == null) return; // Handle unlabeled instances by skipping them
    FeatureVector fv = (FeatureVector) instance.getData ();
    double oneNorm = fv.oneNorm();
    if (oneNorm <= 0) return; // Skip instances that have no features present
    if (docLengthNormalization > 0)
      // Make the document have counts that sum to docLengthNormalization
      // I.e., if 20, it would be as if the document had 20 words.
      instanceWeight *= docLengthNormalization / oneNorm;
    assert (instanceWeight > 0 && !Double.isInfinite(instanceWeight));
    for (int lpos = 0; lpos < labeling.numLocations(); lpos++) {
      int li = labeling.indexAtLocation (lpos);
      double labelWeight = labeling.valueAtLocation (lpos);
      if (labelWeight == 0) continue;
      //System.out.println ("NaiveBayesTrainer me.increment "+ labelWeight * instanceWeight);
      me[li].increment (fv, labelWeight * instanceWeight);
      // This relies on labelWeight summing to 1 over all labels
      pe.increment (li, labelWeight * instanceWeight);
 
View Full Code Here

  /** Return the average rank of the correct class label as returned by Labeling.getRank(correctLabel) on the predicted Labeling. */
  public double getAverageRank ()
  {
    double rsum = 0;
    Labeling tmpL;
    Classification tmpC;
    Instance tmpI;
    Label tmpLbl, tmpLbl2;
    int tmpInt;
    for(int i = 0; i < this.size(); i++) {
      tmpC = this.get(i);
      tmpI = tmpC.getInstance();
      tmpL = tmpC.getLabeling();
      tmpLbl = (Label)tmpI.getTarget();
      tmpInt = tmpL.getRank(tmpLbl);
      tmpLbl2 = tmpL.getLabelAtRank(0);
      rsum = rsum + tmpInt;
    }
    return rsum/this.size();
  }
View Full Code Here

   
    double[][] featureCounts = new double[labeledFeatures.size()][numLabels];
    for (int ii = 0; ii < trainingData.size(); ii++) {
      Instance instance = trainingData.get(ii);
      FeatureVector fv = (FeatureVector)instance.getData();
      Labeling labeling = trainingData.get(ii).getLabeling();
      double[] labelDist = new double[numLabels];
     
      if (labeling == null) {
        labelByVoting(labeledFeatures,instance,labelDist);
      } else {
        int li = labeling.getBestIndex();
        labelDist[li] = 1.;
      }
 
      keyIter = labeledFeatures.keySet().iterator();
      int i = 0;
View Full Code Here

    // Initialize the constraints
    logger.fine("Number of instances in training list = " + trainingList.size());
    for (Instance inst : trainingList) {
      double instanceWeight = trainingList.getInstanceWeight(inst);
      Labeling labeling = inst.getLabeling ();
      if (labeling == null)
        continue;
      //logger.fine ("Instance "+ii+" labeling="+labeling);
      FeatureVector fv = (FeatureVector) inst.getData ();
      Alphabet fdict = fv.getAlphabet();
      assert (fv.getAlphabet() == fd);

      // Here is the difference between this code and the single label
      //  version: rather than only picking out the "best" index,
      //  loop over all label indices.
     
      assert(labeling.numLocations() == trainingSet.getTargetAlphabet().size());
      for (int pos = 0; pos < labeling.numLocations(); pos++){
        MatrixOps.rowPlusEquals (constraints, numFeatures,
                     labeling.indexAtLocation(pos),
                     fv,
                     instanceWeight*labeling.valueAtLocation(pos));
      }

      assert(!Double.isNaN(instanceWeight)) : "instanceWeight is NaN";

      boolean hasNaN = false;
      for (int i = 0; i < fv.numLocations(); i++) {
        if (Double.isNaN(fv.valueAtLocation(i))) {
          logger.info("NaN for feature " + fdict.lookupObject(fv.indexAtLocation(i)).toString());
          hasNaN = true;
        }
      }
      if (hasNaN)
        logger.info("NaN in instance: " + inst.getName());

      // For the default feature, whose weight is 1.0
      for (int pos = 0; pos < labeling.numLocations(); pos++) {
        constraints[labeling.indexAtLocation(pos)*numFeatures + defaultFeatureIndex] +=
          1.0 * instanceWeight * labeling.value(labeling.indexAtLocation(pos));
      }
    }
  }
 
View Full Code Here

      int ii=0;
      while (iter.hasNext()) {
        ii++;
        Instance instance = iter.next();
        double instanceWeight = trainingList.getInstanceWeight(instance);
        Labeling labeling = instance.getLabeling ();
        if (labeling == null)
          continue;
        //System.out.println("L Now "+inputAlphabet.size()+" regular features.");

        this.theClassifier.getClassificationScores (instance, scores);
        FeatureVector fv = (FeatureVector) instance.getData ();

        value = 0.0;
        for(int pos = 0; pos < labeling.numLocations(); pos++) { //loop, added by Limin Yao
          int ll = labeling.indexAtLocation(pos);
          if (scores[ll] == && labeling.valueAtLocation(pos) > 0) {
            logger.warning ("Instance "+instance.getSource() + " has infinite value; skipping value and gradient");
            cachedValue = Double.NEGATIVE_INFINITY;
            cachedValueStale = false;
            return cachedValue;
          }
          else if (labeling.valueAtLocation(pos) != 0) {
            value -= (instanceWeight * labeling.valueAtLocation(pos) * Math.log (scores[ll]));
          }
        }     

        if (Double.isNaN(value)) {
          logger.fine ("MaxEntOptimizableByLabelDistribution: Instance " + instance.getName() +
View Full Code Here

TOP

Related Classes of cc.mallet.types.Labeling

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.