Package cc.mallet.types

Examples of cc.mallet.types.FeatureVectorSequence


      for (int j = parameters.weights[i].numLocations()-1; j >= 0; j--)
        weightsPresent[i].set (parameters.weights[i].indexAtLocation(j));
    // Put in the weights in the training set
    for (int i = 0; i < trainingData.size(); i++) {
      Instance instance = trainingData.get(i);
      FeatureVectorSequence input = (FeatureVectorSequence) instance.getData();
      FeatureSequence output = (FeatureSequence) instance.getTarget();
      // gsc: trainingData can have unlabeled instances as well
      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


   * Add the token classifier's predictions as features to the instance.
   * This method assumes the input instance contains FeatureVectorSequence as data 
   */
  public Instance pipe(Instance carrier)
  {
    FeatureVectorSequence fvs = (FeatureVectorSequence) carrier.getData();
    InstanceList ilist = convert(carrier, (Noop) m_tokenClassifiers.getInstancePipe());
    assert (fvs.size() == ilist.size());

    // For passing instances to the token classifier, each instance's data alphabet needs to
    // match that used by the token classifier at training time.  For the resulting piped
    // instance, each instance's data alphabet needs to contain token classifier's prediction
    // as features
    FeatureVector[] fva = new FeatureVector[fvs.size()];

    for (int i = 0; i < ilist.size(); i++) {
      Instance inst = ilist.get(i);
      Classification c = m_tokenClassifiers.classify(inst, ! m_inProduction);
      LabelVector lv = c.getLabelVector();
      AugmentableFeatureVector afv1 = (AugmentableFeatureVector) inst.getData();
      int[] indices = afv1.getIndices();
      AugmentableFeatureVector afv2 = new AugmentableFeatureVector(m_dataAlphabet,
          indices, afv1.getValues(), indices.length + m_predRanks2add.length);

      for (int j = 0; j < m_predRanks2add.length; j++) {
        Label label = lv.getLabelAtRank(m_predRanks2add[j]);
        int idx = m_dataAlphabet.lookupIndex("TOK_PRED=" + label.toString() + "_@_RANK_" + m_predRanks2add[j]);

        assert(idx >= 0);
        afv2.add(idx, 1);
      }
      fva[i] = afv2;
    }

    carrier.setData(new FeatureVectorSequence(fva));
    return carrier;
  }
View Full Code Here

  {
    InstanceList ret = new InstanceList(alphabetsPipe);
    Object obj = inst.getData();
    assert(obj instanceof FeatureVectorSequence);

    FeatureVectorSequence fvs = (FeatureVectorSequence) obj;
    LabelSequence ls = (LabelSequence) inst.getTarget();
    assert(fvs.size() == ls.size());

    Object instName = (inst.getName() == null ? "NONAME" : inst.getName());
   
    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);
View Full Code Here

    // self-transition
    crf.setParameter(0, 1, 0, 1.0); // state0->state1
    crf.setParameter(1, 1, 0, 1.0); // state1 self-transition
    crf.setParameter(1, 0, 0, Transducer.IMPOSSIBLE_WEIGHT); // state1->state0

    FeatureVectorSequence fvs = new FeatureVectorSequence(
        new FeatureVector[] {
            new FeatureVector((Alphabet) crf.getInputAlphabet(),
                new double[] { 1 }),
            new FeatureVector((Alphabet) crf.getInputAlphabet(),
                new double[] { 1 }),
View Full Code Here

    // self-transition
    crf.setParameter(0, 1, 0, 1.0); // state0->state1
    crf.setParameter(1, 1, 0, 1.0); // state1 self-transition
    crf.setParameter(1, 0, 0, Transducer.IMPOSSIBLE_WEIGHT); // state1->state0

    FeatureVectorSequence fvs = new FeatureVectorSequence(
        new FeatureVector[] {
            new FeatureVector((Alphabet) crf.getInputAlphabet(),
                new double[] { 1 }),
            new FeatureVector((Alphabet) crf.getInputAlphabet(),
                new double[] { 1 }),
View Full Code Here

    }

    CRF crf = new CRF(inputAlphabet, outputAlphabet);
    CRF saveCRF = crf;
    // inputAlphabet = (Feature.Alphabet) crf.getInputAlphabet();
    FeatureVectorSequence fvs = new FeatureVectorSequence(
        new FeatureVector[] {
            new FeatureVector(crf.getInputAlphabet(), new int[] {
                1, 2, 3 }),
            new FeatureVector(crf.getInputAlphabet(), new int[] {
                1, 2, 3 }),
View Full Code Here

    // now check the speeds of SumLatticeDefault vs SumLatticeScaling
    long totalTimeDefault = 0, totalTimeScaling = 0;
    for (int iter = 0; iter < 10000; iter++) {
      for (int ii = 0; ii < lists[1].size(); ii++) {
        FeatureVectorSequence input = (FeatureVectorSequence) lists[1]
            .get(ii).getData();
        totalTimeDefault -= System.currentTimeMillis();
        SumLattice defaultLattice = new SumLatticeDefault(crf, input,
            true);
        totalTimeDefault += System.currentTimeMillis();
View Full Code Here

    this (false, false);
  }
 
  public Instance pipe (Instance carrier)
  {
    carrier.setData(new FeatureVectorSequence ((Alphabet)getDataAlphabet(),
                                               (TokenSequence)carrier.getData(),
                                               binary, augmentable,
                                               growAlphabet));
    return carrier;
  }
View Full Code Here

  private double computeLikelihood(InstanceList trainingSample) {
    double loglik = 0.0;
    for (int i = 0; i < trainingSample.size(); i++) {
      Instance trainingInstance = trainingSample.get(i);
      FeatureVectorSequence fvs = (FeatureVectorSequence) trainingInstance
          .getData();
      Sequence labelSequence = (Sequence) trainingInstance.getTarget();
      loglik += new SumLatticeDefault(crf, fvs, labelSequence, null)
          .getTotalWeight();
      loglik -= new SumLatticeDefault(crf, fvs, null, null)
View Full Code Here

  public double trainIncrementalLikelihood(Instance trainingInstance,
      double rate) {
    double singleLoglik;
    constraints.zero();
    expectations.zero();
    FeatureVectorSequence fvs = (FeatureVectorSequence) trainingInstance
        .getData();
    Sequence labelSequence = (Sequence) trainingInstance.getTarget();
    singleLoglik = new SumLatticeDefault(crf, fvs, labelSequence,
        constraints.new Incrementor()).getTotalWeight();
    singleLoglik -= new SumLatticeDefault(crf, fvs, null,
View Full Code Here

TOP

Related Classes of cc.mallet.types.FeatureVectorSequence

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.