Package cc.mallet.types

Examples of cc.mallet.types.FeatureVector


    //        int numFeatures = getAlphabet().size() + 1;
    int numFeatures = this.defaultFeatureIndex + 1;

    int numLabels = getLabelAlphabet().size();
    assert (scores.length == numLabels);
    FeatureVector fv = (FeatureVector) instance.getData ();
    // 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 ());

    // Include the feature weights according to each label
    for (int li = 0; li < numLabels; li++) {
      scores[li] = parameters[li*numFeatures + defaultFeatureIndex]
View Full Code Here


        int[] featureIndicesArr = new int[featureIndices.size()];
        for (int index = 0; index < featureIndices.size(); index++) {
          featureIndicesArr[index] = featureIndices.get(index);
        }
         fvs[l] = featureInductionOption.value ? new AugmentableFeatureVector(features, featureIndicesArr, null, featureIndicesArr.length) :
          new FeatureVector(features, featureIndicesArr);
      }
      carrier.setData(new FeatureVectorSequence(fvs));
      if (isTargetProcessing())
        carrier.setTarget(target);
      else
View Full Code Here

    for (int i = start; i < end; ++i) {
      FeatureVectorSequence fvs =
          (FeatureVectorSequence) ilist.get(i).getData();
      featurePresent = false;
      for (int ip = 0; ip < fvs.size(); ++ip) {
        FeatureVector fv = fvs.getFeatureVector(ip);
        // set flag and bit if any constraint is present
        for (int index : indices) {
          if (fv.value(index) > 0.0) {
            featurePresent = true;
            break;
          }
        }
        if (featurePresent) {
View Full Code Here

            {
               StringBuffer buf = new StringBuffer();
              for (int a = 0; a < k; a++)
                 buf.append(outputs[a].get(j).toString()).append(" ");
              if (includeInput) {
                FeatureVector fv = (FeatureVector)input.get(j);
                buf.append(fv.toString(true));               
              }
              System.out.println(buf.toString());
            }
            System.out.println();
          }
View Full Code Here

        int[] featureIndicesArr = new int[featureIndices.size()];
        for (int index = 0; index < featureIndices.size(); index++) {
          featureIndicesArr[index] = featureIndices.get(index);
        }
         fvs[l] = featureInductionOption.value ? new AugmentableFeatureVector(features, featureIndicesArr, null, featureIndicesArr.length) :
          new FeatureVector(features, featureIndicesArr);
      }
      carrier.setData(new FeatureVectorSequence(fvs));
      if (isTargetProcessing())
        carrier.setTarget(target);
      else
View Full Code Here

      if (output != null && output.size() > 0) {
        // Do it for the paths consistent with the labels...
        sumLatticeFactory.newSumLattice (this, input, output, new Transducer.Incrementor() {
          public void incrementTransition (Transducer.TransitionIterator ti, double count) {
            State source = (CRF.State)ti.getSourceState();
            FeatureVector input = (FeatureVector)ti.getInput();
            int index = ti.getIndex();
            int nwi = source.weightsIndices[index].length;
            for (int wi = 0; wi < nwi; wi++) {
              int weightsIndex = source.weightsIndices[index][wi];
              for (int i = 0; i < input.numLocations(); i++) {
                int featureIndex = input.indexAtLocation(i);
                if ((globalFeatureSelection == null || globalFeatureSelection.contains(featureIndex))
                    && (featureSelections == null
                        || featureSelections[weightsIndex] == null
                        || featureSelections[weightsIndex].contains(featureIndex)))
                  weightsPresent[weightsIndex].set (featureIndex);
              }
            }
          }
          public void incrementInitialState (Transducer.State s, double count) {  }
          public void incrementFinalState (Transducer.State s, double count) {  }
        });
      }
      // ...and also do it for the paths selected by the current model (so we will get some negative weights)
      if (useSomeUnsupportedTrick && this.getParametersAbsNorm() > 0) {
        if (i == 0)
          logger.info ("CRF: Incremental training detected.  Adding weights for some unsupported features...");
        // (do this once some training is done)
        sumLatticeFactory.newSumLattice (this, input, null, new Transducer.Incrementor() {
          public void incrementTransition (Transducer.TransitionIterator ti, double count) {
            if (count < 0.2) // Only create features for transitions with probability above 0.2
              return// This 0.2 is somewhat arbitrary -akm
            State source = (CRF.State)ti.getSourceState();
            FeatureVector input = (FeatureVector)ti.getInput();
            int index = ti.getIndex();
            int nwi = source.weightsIndices[index].length;
            for (int wi = 0; wi < nwi; wi++) {
              int weightsIndex = source.weightsIndices[index][wi];
              for (int i = 0; i < input.numLocations(); i++) {
                int featureIndex = input.indexAtLocation(i);
                if ((globalFeatureSelection == null || globalFeatureSelection.contains(featureIndex))
                    && (featureSelections == null
                        || featureSelections[weightsIndex] == null
                        || featureSelections[weightsIndex].contains(featureIndex)))
                  weightsPresent[weightsIndex].set (featureIndex);
View Full Code Here

      // skip if labeled
      if (instance.getTarget() != null) {
        continue;
      }
     
      FeatureVector fv = (FeatureVector) instance.getData();
      classifier.getClassificationScoresWithTemperature(instance, temperature, scores[ii]);
     
      for (int loc = 0; loc < fv.numLocations(); loc++) {
        int featureIndex = fv.indexAtLocation(loc);
        if (constraints.containsKey(featureIndex)) {
          int cIndex = mapping.get(featureIndex);           
          double val;
          if (!useValues) {
            val = 1.;
          }
          else {
            val = fv.valueAtLocation(loc);
          }
          featureCounts[cIndex] += val;
          for (int l = 0; l < numLabels; l++) {
            modelExpectations[cIndex][l] += scores[ii][l] * val * instanceWeight;
          }
        }
      }
     
      // special case of label regularization
      if (constraints.containsKey(defaultFeatureIndex)) {
        int cIndex = mapping.get(defaultFeatureIndex);
        featureCounts[cIndex] += 1;
        for (int l = 0; l < numLabels; l++) {
          modelExpectations[cIndex][l] += scores[ii][l] * instanceWeight;
        }       
      }
    }
   
    double value = 0;
    for (int featureIndex : constraints.keySet()) {
      int cIndex = mapping.get(featureIndex);
      if (featureCounts[cIndex] > 0) {
        for (int label = 0; label < numLabels; label++) {
          double cProb = constraints.get(featureIndex)[label];
          // normalize by count
          modelExpectations[cIndex][label] /= featureCounts[cIndex];
          ratio[cIndex][label] =  cProb / modelExpectations[cIndex][label];
          // add to the cross entropy term
          value += scalingFactor * cProb * Math.log(modelExpectations[cIndex][label]);
          // add to the entropy term
          if (cProb > 0) {
            value -= scalingFactor * cProb * Math.log(cProb);
          }
        }
        assert(Maths.almostEquals(MatrixOps.sum(modelExpectations[cIndex]),1));
      }
    }

    // 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;
      }
     
      double instanceWeight = trainingList.getInstanceWeight(instance);
      FeatureVector fv = (FeatureVector) instance.getData();

      for (int loc = 0; loc < fv.numLocations() + 1; loc++) {
        int featureIndex;
        if (loc == fv.numLocations()) {
          featureIndex = defaultFeatureIndex;
        }
        else {
          featureIndex = fv.indexAtLocation(loc);
        }
       
        if (constraints.containsKey(featureIndex)) {
          int cIndex = mapping.get(featureIndex);

          // skip if this feature never occurred
          if (featureCounts[cIndex] == 0) {
            continue;
          }

          double val;
          if ((featureIndex == defaultFeatureIndex)||(!useValues)) {
            val = 1;
          }
          else {
            val = fv.valueAtLocation(loc);
          }
         
          // compute \sum_y p(y|x) \hat{g}_y / \bar{g}_y
          double instanceExpectation = 0;
          for (int label = 0; label < numLabels; label++) {
View Full Code Here

        indices[termIndex-1] = getDataAlphabet().lookupIndex(feature, true);      
        values[termIndex-1] = Double.parseDouble(s[1]);
      }
    }
   
    FeatureVector fv = new FeatureVector(getDataAlphabet(), indices, values);
    carrier.setData(fv);
    return carrier;
  }
View Full Code Here

                          }
                         
                          buf.append(tag).append(" ");
                        }
                        if (includeInput) {
                            FeatureVector fv = (FeatureVector)input.get(j);
                            buf.append(fv.toString(true));               
                        }
                        System.out.println(buf.toString());
                    }
                    //System.out.println();
                }
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);
      } else {
        int li = labeling.getBestIndex();
        labelDist[li] = 1.;
      }
 
      keyIter = labeledFeatures.keySet().iterator();
      int i = 0;
      while (keyIter.hasNext()) {
        int fi = keyIter.next();
        if (fv.location(fi) >= 0) {
          for (int li = 0; li < numLabels; li++) {
            featureCounts[i][li] += labelDist[li] * fv.valueAtLocation(fv.location(fi));
          }
        }
        i++;
      }
    }
View Full Code Here

TOP

Related Classes of cc.mallet.types.FeatureVector

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.