Package cc.mallet.types

Examples of cc.mallet.types.Instance


      double value = 0.0;
      Iterator<Instance> iter = trainingList.iterator();
      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] +
              " has instance weight = " + instanceWeight);

        }
        if (Double.isInfinite(value)) {
          logger.warning ("Instance "+instance.getSource() + " has infinite value; skipping value and gradient");
          cachedValue -= value;
          cachedValueStale = false;
          return -value;
//          continue;
        }
View Full Code Here


  /** Pipe the object through this classifier's pipe, then classify the resulting instance. */
  public Classification classify (Object obj)
  {
    if (obj instanceof Instance)
      return classify ((Instance)obj);
    return classify (instancePipe.instanceFrom(new Instance (obj, null, null, null)));
  }
View Full Code Here

  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

      // Initialize the constraints, using only the constraints from
      // the "positive" instance
      Iterator<Instance> iter = trainingList.iterator ();
      logger.fine("Number of instances in training list = " + trainingList.size());
      while (iter.hasNext()) {
        Instance instance = iter.next();
        double instanceWeight = trainingList.getInstanceWeight(instance);
        FeatureVectorSequence fvs = (FeatureVectorSequence) instance.getData();
        // label of best instance in subList
        Object target = instance.getTarget();
        Label label = null;
        if (target instanceof Labels)
          label = ((Labels)target).get(0);
        else label = (Label)target;
        int positiveIndex =
          Integer.valueOf(label.getBestLabel().getEntry().toString()).intValue();
        if (positiveIndex == -1) { // invalid instance
          logger.warning("True label is -1. Skipping...");
           continue;
        }
        FeatureVector fv = (FeatureVector)fvs.get(positiveIndex);
        Alphabet fdict = fv.getAlphabet();
        assert (fv.getAlphabet() == fd);

        // xxx ensure dimensionality of constraints correct
        MatrixOps.rowPlusEquals (constraints, numFeatures, 0, 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;
        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: " + instance.getName());

        // default constraints for positive instances xxx
        constraints[0*numFeatures + defaultFeatureIndex] += 1.0 * instanceWeight;
      }
      //TestMaximizable.testValueAndGradientCurrentParameters (this);
View Full Code Here

    Iterator<Integer> keyIter = labeledFeatures.keySet().iterator();
   
    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);
View Full Code Here

    int numLabels = list.getTargetAlphabet().size();
   
    double[][] featureLabelCounts = new double[numFeatures][numLabels];
   
    for (int ii = 0; ii < list.size(); ii++) {
      Instance instance = list.get(ii);
      FeatureVector featureVector = (FeatureVector)instance.getData();
     
      // this handles distributions over labels
      for (int li = 0; li < numLabels; li++) {
        double py = instance.getLabeling().value(li);
        for (int loc = 0; loc < featureVector.numLocations(); loc++) {
          int fi = featureVector.indexAtLocation(loc);
          double val;
          if (useValues) {
            val = featureVector.valueAtLocation(loc);
View Full Code Here

      if (m_ilist == null)
        throw new IllegalStateException ("Frozen.  Cannot split.");
      int numLeftChildren = 0;
      boolean[] toLeftChild = new boolean[m_instIndices.length];
      for (int i = 0; i < m_instIndices.length; i++) {
        Instance instance = m_ilist.get(m_instIndices[i]);
        FeatureVector fv = (FeatureVector) instance.getData();
        if (fv.value (m_gainRatio.getMaxValuedIndex()) <= m_gainRatio.getMaxValuedThreshold()) {
          toLeftChild[i] = true;
          numLeftChildren++;
        }
        else
View Full Code Here

      double value = 0.0;
      Iterator<Instance> iter = trainingList.iterator();
      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() +
                 "has NaN value.");
        }
        if (Double.isInfinite(value)) {
          logger.warning ("Instance "+instance.getSource() + " has infinite value; skipping value and gradient");
          cachedValue -= value;
          cachedValueStale = false;
          return -value;
//          continue;
        }
View Full Code Here

    NeighborIterator iter = new ClusterSampleIterator(clustering,
                                                      random,
                                                      0.5,
                                                      10);
    while (iter.hasNext()) {
      Instance instance = (Instance)iter.next();
      System.err.println(instance.getData() + "\n");
    }
   
    System.err.println("\n\nPairSampleIterator");
    iter = new PairSampleIterator(clustering,
                                  random,
                                  0.5,
                                  10);
    while (iter.hasNext()) {
      Instance instance = (Instance)iter.next();
      System.err.println(instance.getData() + "\n");
    }

    System.err.println("\n\nAllPairsIterator");
    iter = new AllPairsIterator(clustering);                               
    while (iter.hasNext()) {
      Instance instance = (Instance)iter.next();
      System.err.println(instance.getData() + "\n");
    }
}
View Full Code Here

    double value = 0.;
    //double sumLogP = 0;
   
    for (int ii = 0; ii < trainingData.size(); ii++) {
      double[] scores = new double[numLabels];
      Instance instance = trainingData.get(ii);
      FeatureVector input = (FeatureVector) instance.getData ();
      double instanceWeight = trainingData.getInstanceWeight(ii);
     
      classifier.getClassificationScores(instance, scores);
      double logZ = Double.NEGATIVE_INFINITY;
     
      for (int li = 0; li < numLabels; li++) {
        if (baseDist != null && baseDist[ii][li] == 0) {
          scores[li] = Double.NEGATIVE_INFINITY;
        }
        else if (baseDist != null) {
          double logP = Math.log(baseDist[ii][li]);
          scores[li] += logP; 
        }
        logZ = Maths.sumLogProb(logZ, scores[li]);
      }
      assert(!Double.isNaN(logZ));
      value -= instanceWeight * logZ;
     
      if (Double.isNaN(value)) {
        logger.warning("Instance " + instance.getName() + " has NaN value.");
        continue;
      }
      if (Double.isInfinite(value)) {
        logger.warning("Instance " + instance.getName() + " has infinite value; skipping value and gradient");
        continue;
      }
     
      // exp normalize
      MatrixOps.expNormalize(scores);
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.