Package cc.mallet.types

Examples of cc.mallet.types.FeatureVectorSequence$Iterator


    }

    trainingGatheredFor = training;
    for (int i = 0; i < training.size(); i++) {
      Instance instance = training.get(i);
      FeatureVectorSequence input = (FeatureVectorSequence) instance.getData();
      FeatureSequence output = (FeatureSequence) instance.getTarget();
      // Do it for the paths consistent with the labels...
      new SumLatticeDefault (memm, input, output, new Transducer.Incrementor() {
        public void incrementFinalState(Transducer.State s, double count) { }
        public void incrementInitialState(Transducer.State s, double count) { }
View Full Code Here


   * @param instance instance with data field a {@link InstanceList}.
   * @param scores has length = number of Instances in Instance.data,
   * which is of type InstanceList */
  public void getUnnormalizedClassificationScores (Instance instance, double[] scores)
  {
    FeatureVectorSequence fvs = (FeatureVectorSequence)instance.getData();
    assert (scores.length == fvs.size());
    int numFeatures = instance.getDataAlphabet().size()+1;

    for (int instanceNumber=0; instanceNumber < fvs.size(); instanceNumber++) {
      FeatureVector fv = (FeatureVector)fvs.get(instanceNumber);
      // Make sure the feature vector's feature dictionary matches
      // what we are expecting from our data pipe (and thus our notion
      // of feature probabilities.
      assert (fv.getAlphabet ()
              == this.instancePipe.getDataAlphabet ());
View Full Code Here

    }
  }
 
  public void getClassificationScores (Instance instance, double[] scores)
  {
    FeatureVectorSequence fvs = (FeatureVectorSequence)instance.getData();
    int numFeatures = instance.getDataAlphabet().size()+1;
    int numLabels = fvs.size();
    assert (scores.length == fvs.size());

    for (int instanceNumber=0; instanceNumber < fvs.size(); instanceNumber++) {
      FeatureVector fv = (FeatureVector)fvs.get(instanceNumber);
      // Make sure the feature vector's feature dictionary matches
      // what we are expecting from our data pipe (and thus our notion
      // of feature probabilities.
      assert (fv.getAlphabet ()
              == this.instancePipe.getDataAlphabet ());
View Full Code Here

    for (int li = 0; li < scores.length; li++)
      scores[li] /= sum;   
  }
  public Classification classify (Instance instance)
  {
    FeatureVectorSequence fvs = (FeatureVectorSequence) instance.getData();
    int numClasses = fvs.size();
    double[] scores = new double[numClasses];
    getClassificationScores (instance, scores);
    // Create and return a Classification object
    return new Classification (instance, this,
                               createLabelVector (getLabelAlphabet(),
View Full Code Here

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

        Iterator<Instance> iter = trainingList.iterator();
        int ii=0;       
        while (iter.hasNext()) {
          ii++;
          Instance instance = iter.next();
          FeatureVectorSequence fvs = (FeatureVectorSequence) instance.getData();
          // scores stores Pr of subList[i] being positive instance
          double[] scores = new double[fvs.size()];
          double instanceWeight = trainingList.getInstanceWeight(instance);

          // labeling is a String representation of an int, indicating which FeatureVector from
          // the subList is the positive example         
         
          // If is String, proceed as usual. Else, if is String[], do
          // not penalize scores for duplicate entries. This improved accuracy in some expts.
          Object target = instance.getTarget();
          int li = -1;
          if (target instanceof Label) {
            li = Integer.valueOf(((Label)target).toString()).intValue();
            if (li == -1) // hack to avoid invalid instances
              continue;
            assert (li >=0 && li < fvs.size());
            this.theClassifier.getClassificationScores (instance, scores);
          } else if (target instanceof Labels){
            Labels labels = (Labels)target;
            int[] bestPositions = new int[labels.size()];
            for (int pi = 0; pi < labels.size(); pi++)
              bestPositions[pi] = Integer.valueOf(labels.get(pi).toString());
            li = bestPositions[0];
            this.theClassifier.getClassificationScoresForTies (instance, scores, bestPositions);             
          }
          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;
          }
          cachedValue += value;
          double positiveScore = scores[li];

         
          for (int si=0; si < fvs.size(); si++) {
            if (scores[si]==0)
              continue;
            assert (!Double.isInfinite(scores[si]));
            FeatureVector cfv = (FeatureVector)fvs.get(si);
            MatrixOps.rowPlusEquals (cachedGradient, numFeatures,
                                     0, cfv, -instanceWeight * scores[si]);
            cachedGradient[numFeatures*0 + defaultFeatureIndex] += (-instanceWeight * scores[si]);           
          }
        }
 
View Full Code Here

  public BitSet preProcess(InstanceList data) {
    // count
    BitSet bitSet = new BitSet(data.size());
    int ii = 0;
    for (Instance instance : data) {
      FeatureVectorSequence fvs = (FeatureVectorSequence)instance.getData();
      for (int ip = 1; ip < fvs.size(); ip++) {
        for (int fi : constraintsMap.keys()) {
          // binary constraint features
          if (fvs.get(ip).location(fi) >= 0) {
            constraintsList.get(constraintsMap.get(fi)).count += 1;
            bitSet.set(ii);
          }
        }
      }
View Full Code Here

  public void computeExpectations(ArrayList<SumLattice> lattices) {
    double[][][] xis;
    TIntArrayList cache = new TIntArrayList();
    for (int i = 0; i < lattices.size(); i++) {
      if (lattices.get(i) == null) { continue; }
      FeatureVectorSequence fvs = (FeatureVectorSequence)lattices.get(i).getInput();
      SumLattice lattice = lattices.get(i);
      xis = lattice.getXis();
      for (int ip = 1; ip < fvs.size(); ++ip) {
        cache.resetQuick();
        FeatureVector fv = fvs.getFeatureVector(ip);
        int fi;
        for (int loc = 0; loc < fv.numLocations(); loc++) {
          fi = fv.indexAtLocation(loc);
          // binary constraint features
          if (constraintsMap.containsKey(fi)) {
View Full Code Here

    int ii = 0;
    int fi;
    FeatureVector fv;
    BitSet bitSet = new BitSet(data.size());
    for (Instance instance : data) {
      FeatureVectorSequence fvs = (FeatureVectorSequence)instance.getData();
      for (int ip = 0; ip < fvs.size(); ip++) {
        fv = fvs.get(ip);
        for (int loc = 0; loc < fv.numLocations(); loc++) {
          fi = fv.indexAtLocation(loc);
          if (constraints.containsKey(fi)) {
            constraints.get(fi).count += 1;
            bitSet.set(ii);
View Full Code Here

    int ii = 0;
    int fi;
    FeatureVector fv;
    BitSet bitSet = new BitSet(data.size());
    for (Instance instance : data) {
      FeatureVectorSequence fvs = (FeatureVectorSequence)instance.getData();
      for (int ip = 0; ip < fvs.size(); ip++) {
        fv = fvs.get(ip);
        for (int loc = 0; loc < fv.numLocations(); loc++) {
          fi = fv.indexAtLocation(loc);
          if (constraints.containsKey(fi)) {
            constraints.get(fi).count += 1;
            bitSet.set(ii);
View Full Code Here

TOP

Related Classes of cc.mallet.types.FeatureVectorSequence$Iterator

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.