cc.mallet.types.InstanceList.getInstanceWeight()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(14.8k)|赞(0)|评价(0)|浏览(117)

本文整理了Java中cc.mallet.types.InstanceList.getInstanceWeight()方法的一些代码示例,展示了InstanceList.getInstanceWeight()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。InstanceList.getInstanceWeight()方法的具体详情如下:
包路径:cc.mallet.types.InstanceList
类名称:InstanceList
方法名:getInstanceWeight

InstanceList.getInstanceWeight介绍

暂无

代码示例

代码示例来源:origin: de.julielab/jcore-mallet-2.0.9

/**
 * Returns an <code>InstanceList</code> of the same size, where the instances come from the
 * random sampling (with replacement) of this list using the instance weights.
 * The new instances all have their weights set to one.
 */
// added by Gary - ghuang@cs.umass.edu
@Deprecated
// Move to InstanceListUtils
public InstanceList sampleWithInstanceWeights(java.util.Random r) 
{
  double[] weights = new double[size()];
  for (int i = 0; i < weights.length; i++)
    weights[i] = getInstanceWeight(i);
  return sampleWithWeights(r, weights);
}

代码示例来源:origin: cc.mallet/mallet

/**
 * Returns an <code>InstanceList</code> of the same size, where the instances come from the
 * random sampling (with replacement) of this list using the instance weights.
 * The new instances all have their weights set to one.
 */
// added by Gary - ghuang@cs.umass.edu
@Deprecated
// Move to InstanceListUtils
public InstanceList sampleWithInstanceWeights(java.util.Random r) 
{
  double[] weights = new double[size()];
  for (int i = 0; i < weights.length; i++)
    weights[i] = getInstanceWeight(i);
  return sampleWithWeights(r, weights);
}

代码示例来源:origin: com.github.steveash.mallet/mallet

/**
 * Returns an <code>InstanceList</code> of the same size, where the instances come from the
 * random sampling (with replacement) of this list using the instance weights.
 * The new instances all have their weights set to one.
 */
// added by Gary - ghuang@cs.umass.edu
@Deprecated
// Move to InstanceListUtils
public InstanceList sampleWithInstanceWeights(java.util.Random r) 
{
  double[] weights = new double[size()];
  for (int i = 0; i < weights.length; i++)
    weights[i] = getInstanceWeight(i);
  return sampleWithWeights(r, weights);
}

代码示例来源:origin: com.github.steveash.mallet/mallet

@Test
  public void setWeights() {
  
  InstanceList instances = new InstanceList(new Noop());
  Instance instance1 = new Instance("test", null, null, null);
  Instance instance2 = new Instance("test", null, null, null);
  Instance instance3 = new Instance("test", null, null, null);
  
  instances.add(instance1, 10.0);
  instances.add(instance2);
  assertEquals("#1", instances.getInstanceWeight(0), 10.0, 0.0);
  assertEquals("#2", instances.getInstanceWeight(instance1), 10.0, 0.0);
  assertEquals("#3", instances.getInstanceWeight(1), 1.0, 0.0);
  assertEquals("#4", instances.getInstanceWeight(instance2), 1.0, 0.0);
  // Reset an existing weight
  instances.setInstanceWeight(0, 1.0);
  assertEquals("#5", instances.getInstanceWeight(0), 1.0, 0.0);
  // Reset an existing default (and therefore not represented) weight
  instances.setInstanceWeight(1, 5.0);
  assertEquals("#6", instances.getInstanceWeight(1), 5.0, 0.0);
}

代码示例来源:origin: cc.mallet/mallet

@Test
  public void setWeights() {
  
  InstanceList instances = new InstanceList(new Noop());
  Instance instance1 = new Instance("test", null, null, null);
  Instance instance2 = new Instance("test", null, null, null);
  Instance instance3 = new Instance("test", null, null, null);
  
  instances.add(instance1, 10.0);
  instances.add(instance2);
  assertEquals("#1", instances.getInstanceWeight(0), 10.0, 0.0);
  assertEquals("#2", instances.getInstanceWeight(instance1), 10.0, 0.0);
  assertEquals("#3", instances.getInstanceWeight(1), 1.0, 0.0);
  assertEquals("#4", instances.getInstanceWeight(instance2), 1.0, 0.0);
  // Reset an existing weight
  instances.setInstanceWeight(0, 1.0);
  assertEquals("#5", instances.getInstanceWeight(0), 1.0, 0.0);
  // Reset an existing default (and therefore not represented) weight
  instances.setInstanceWeight(1, 5.0);
  assertEquals("#6", instances.getInstanceWeight(1), 5.0, 0.0);
}

代码示例来源:origin: de.julielab/jcore-mallet-2.0.9

public NaiveBayes trainIncremental (InstanceList trainingInstancesToAdd) 
{
  // Initialize and check instance variables as necessary...
  setup(trainingInstancesToAdd, null);
  // Incrementally add the counts of this new training data
  for (Instance instance : trainingInstancesToAdd)
   incorporateOneInstance(instance, trainingInstancesToAdd.getInstanceWeight(instance));
 
 // Estimate multinomials, and return a new naive Bayes classifier.  
 // Note that, unlike MaxEnt, NaiveBayes is immutable, so we create a new one each time.
 classifier = new NaiveBayes (instancePipe, pe.estimate(), estimateFeatureMultinomials());
 return classifier;
}

代码示例来源:origin: cc.mallet/mallet

public NaiveBayes trainIncremental (InstanceList trainingInstancesToAdd) 
{
  // Initialize and check instance variables as necessary...
  setup(trainingInstancesToAdd, null);
  // Incrementally add the counts of this new training data
  for (Instance instance : trainingInstancesToAdd)
   incorporateOneInstance(instance, trainingInstancesToAdd.getInstanceWeight(instance));
 
 // Estimate multinomials, and return a new naive Bayes classifier.  
 // Note that, unlike MaxEnt, NaiveBayes is immutable, so we create a new one each time.
 classifier = new NaiveBayes (instancePipe, pe.estimate(), estimateFeatureMultinomials());
 return classifier;
}

代码示例来源:origin: com.github.steveash.mallet/mallet

public NaiveBayes trainIncremental (InstanceList trainingInstancesToAdd) 
{
  // Initialize and check instance variables as necessary...
  setup(trainingInstancesToAdd, null);
  // Incrementally add the counts of this new training data
  for (Instance instance : trainingInstancesToAdd)
   incorporateOneInstance(instance, trainingInstancesToAdd.getInstanceWeight(instance));
 
 // Estimate multinomials, and return a new naive Bayes classifier.  
 // Note that, unlike MaxEnt, NaiveBayes is immutable, so we create a new one each time.
 classifier = new NaiveBayes (instancePipe, pe.estimate(), estimateFeatureMultinomials());
 return classifier;
}

代码示例来源:origin: cc.mallet/mallet

public void split (FeatureSelection fs)
{
  if (ilist == null)
    throw new IllegalStateException ("Frozen.  Cannot split.");
  InstanceList ilist0 = new InstanceList (ilist.getPipe());
  InstanceList ilist1 = new InstanceList (ilist.getPipe());
  for (int i = 0; i < ilist.size(); i++) {
    Instance instance = ilist.get(i);
    FeatureVector fv = (FeatureVector) instance.getData ();
    // xxx What test should this be?  What to do with negative values?
      // Whatever is decided here should also go in InfoGain.calcInfoGains()
    if (fv.value (featureIndex) != 0) {
      //System.out.println ("list1 add "+instance.getUri()+" weight="+ilist.getInstanceWeight(i));
      ilist1.add (instance, ilist.getInstanceWeight(i));
    } else {
      //System.out.println ("list0 add "+instance.getUri()+" weight="+ilist.getInstanceWeight(i));
      ilist0.add (instance, ilist.getInstanceWeight(i));
    }
  }
  logger.info("child0="+ilist0.size()+" child1="+ilist1.size());
  child0 = new Node (ilist0, this, fs);
  child1 = new Node (ilist1, this, fs);
}

代码示例来源:origin: cc.mallet/mallet

/**
 * Set the constraints by running forward-backward with the <i>output label
 * sequence provided</i>, thus restricting it to only those paths that agree with
 * the label sequence.
 */
protected void gatherConstraints(InstanceList ilist) {
  logger.info("Gathering constraints...");
  assert (constraints.structureMatches(crf.parameters));
  constraints.zero();
  for (Instance instance : ilist) {
    FeatureVectorSequence input = (FeatureVectorSequence) instance.getData();
    FeatureSequence output = (FeatureSequence) instance.getTarget();
    double instanceWeight = ilist.getInstanceWeight(instance);
    Transducer.Incrementor incrementor =
      instanceWeight == 1.0 ? constraints.new Incrementor()
    : constraints.new WeightedIncrementor(instanceWeight);
      new SumLatticeDefault (this.crf, input, output, incrementor); 
  }
  constraints.assertNotNaNOrInfinite();
}

代码示例来源:origin: com.github.steveash.mallet/mallet

/**
 * Set the constraints by running forward-backward with the <i>output label
 * sequence provided</i>, thus restricting it to only those paths that agree with
 * the label sequence.
 */
protected void gatherConstraints(InstanceList ilist) {
  logger.info("Gathering constraints...");
  assert (constraints.structureMatches(crf.parameters));
  constraints.zero();
  for (Instance instance : ilist) {
    FeatureVectorSequence input = (FeatureVectorSequence) instance.getData();
    FeatureSequence output = (FeatureSequence) instance.getTarget();
    double instanceWeight = ilist.getInstanceWeight(instance);
    Transducer.Incrementor incrementor =
      instanceWeight == 1.0 ? constraints.new Incrementor()
    : constraints.new WeightedIncrementor(instanceWeight);
      new SumLatticeDefault (this.crf, input, output, incrementor); 
  }
  constraints.assertNotNaNOrInfinite();
}

代码示例来源:origin: de.julielab/jcore-mallet-2.0.9

/**
 * Set the constraints by running forward-backward with the <i>output label
 * sequence provided</i>, thus restricting it to only those paths that agree with
 * the label sequence.
 */
protected void gatherConstraints(InstanceList ilist) {
  logger.info("Gathering constraints...");
  assert (constraints.structureMatches(crf.parameters));
  constraints.zero();
  for (Instance instance : ilist) {
    FeatureVectorSequence input = (FeatureVectorSequence) instance.getData();
    FeatureSequence output = (FeatureSequence) instance.getTarget();
    double instanceWeight = ilist.getInstanceWeight(instance);
    Transducer.Incrementor incrementor =
      instanceWeight == 1.0 ? constraints.new Incrementor()
    : constraints.new WeightedIncrementor(instanceWeight);
      new SumLatticeDefault (this.crf, input, output, incrementor); 
  }
  constraints.assertNotNaNOrInfinite();
}

代码示例来源:origin: de.julielab/jcore-mallet-2.0.9

public LabelVector targetLabelDistribution ()
{
  if (this.size() == 0) return null;
  if (!(get(0).getTarget() instanceof Labeling))
    throw new IllegalStateException ("Target is not a labeling.");
  double[] counts = new double[getTargetAlphabet().size()];
  for (int i = 0; i < this.size(); i++) {
    Instance instance =  get(i);
    Labeling l = (Labeling) instance.getTarget();
    l.addTo (counts, getInstanceWeight(i));
  }
  return new LabelVector ((LabelAlphabet)getTargetAlphabet(), counts);
}

代码示例来源:origin: com.github.steveash.mallet/mallet

public LabelVector targetLabelDistribution ()
{
  if (this.size() == 0) return null;
  if (!(get(0).getTarget() instanceof Labeling))
    throw new IllegalStateException ("Target is not a labeling.");
  double[] counts = new double[getTargetAlphabet().size()];
  for (int i = 0; i < this.size(); i++) {
    Instance instance =  get(i);
    Labeling l = (Labeling) instance.getTarget();
    l.addTo (counts, getInstanceWeight(i));
  }
  return new LabelVector ((LabelAlphabet)getTargetAlphabet(), counts);
}

代码示例来源:origin: cc.mallet/mallet

public LabelVector targetLabelDistribution ()
{
  if (this.size() == 0) return null;
  if (!(get(0).getTarget() instanceof Labeling))
    throw new IllegalStateException ("Target is not a labeling.");
  double[] counts = new double[getTargetAlphabet().size()];
  for (int i = 0; i < this.size(); i++) {
    Instance instance =  get(i);
    Labeling l = (Labeling) instance.getTarget();
    l.addTo (counts, getInstanceWeight(i));
  }
  return new LabelVector ((LabelAlphabet)getTargetAlphabet(), counts);
}

代码示例来源:origin: com.github.steveash.jg2p/jg2p-core

private static RankedFeatureVector countFeatures(InstanceList ilist, boolean countInstances) {
 int numFeatures = ilist.getDataAlphabet().size();
 double[] counts = new double[numFeatures];
 for (int i = 0; i < ilist.size(); i++) {
  Instance inst = ilist.get(i);
  if (ilist.getInstanceWeight(i) == 0) {
   continue;
  }
  Object data = inst.getData();
  if (data instanceof FeatureVectorSequence) {
   FeatureVectorSequence fvs = (FeatureVectorSequence) data;
   for (int j = 0; j < fvs.size(); j++) {
    countVector(counts, fvs.get(j), countInstances);
   }
  } else {
   throw new IllegalArgumentException("Currently only handles FeatureVectorSequence data");
  }
 }
 return new RankedFeatureVector(ilist.getDataAlphabet(), counts);
}

代码示例来源:origin: cc.mallet/mallet

protected void gatherConstraints (InstanceList ilist)
  {
    // Set the constraints by running forward-backward with the *output
    // label sequence provided*, thus restricting it to only those
    // paths that agree with the label sequence.
    // Zero the constraints[]
    // Reset constraints[] to zero before we fill them again
    assert (constraints.structureMatches(crf.parameters));
    constraints.zero();

    for (Instance instance : ilist) {
      FeatureVectorSequence input = (FeatureVectorSequence) instance.getData();
      FeatureSequence output = (FeatureSequence) instance.getTarget();
      double instanceWeight = ilist.getInstanceWeight(instance);
      //System.out.println ("Constraint-gathering on instance "+i+" of "+ilist.size());
      Transducer.Incrementor incrementor = instanceWeight == 1.0 ? constraints.new Incrementor() : constraints.new WeightedIncrementor(instanceWeight);
      new SumLatticeDefault (this.crf, input, output, incrementor); 
    }
//        System.out.println ("testing Value and Gradient");
//        TestOptimizable.testValueAndGradientCurrentParameters (this);
  }

代码示例来源:origin: de.julielab/jcore-mallet-2.0.9

protected void gatherConstraints (InstanceList ilist)
  {
    // Set the constraints by running forward-backward with the *output
    // label sequence provided*, thus restricting it to only those
    // paths that agree with the label sequence.
    // Zero the constraints[]
    // Reset constraints[] to zero before we fill them again
    assert (constraints.structureMatches(crf.parameters));
    constraints.zero();

    for (Instance instance : ilist) {
      FeatureVectorSequence input = (FeatureVectorSequence) instance.getData();
      FeatureSequence output = (FeatureSequence) instance.getTarget();
      double instanceWeight = ilist.getInstanceWeight(instance);
      //System.out.println ("Constraint-gathering on instance "+i+" of "+ilist.size());
      Transducer.Incrementor incrementor = instanceWeight == 1.0 ? constraints.new Incrementor() : constraints.new WeightedIncrementor(instanceWeight);
      new SumLatticeDefault (this.crf, input, output, incrementor); 
    }
//        System.out.println ("testing Value and Gradient");
//        TestOptimizable.testValueAndGradientCurrentParameters (this);
  }

代码示例来源:origin: com.github.steveash.mallet/mallet

protected void gatherConstraints (InstanceList ilist)
  {
    // Set the constraints by running forward-backward with the *output
    // label sequence provided*, thus restricting it to only those
    // paths that agree with the label sequence.
    // Zero the constraints[]
    // Reset constraints[] to zero before we fill them again
    assert (constraints.structureMatches(crf.parameters));
    constraints.zero();

    for (Instance instance : ilist) {
      FeatureVectorSequence input = (FeatureVectorSequence) instance.getData();
      FeatureSequence output = (FeatureSequence) instance.getTarget();
      double instanceWeight = ilist.getInstanceWeight(instance);
      //System.out.println ("Constraint-gathering on instance "+i+" of "+ilist.size());
      Transducer.Incrementor incrementor = instanceWeight == 1.0 ? constraints.new Incrementor() : constraints.new WeightedIncrementor(instanceWeight);
      new SumLatticeDefault (this.crf, input, output, incrementor); 
    }
//        System.out.println ("testing Value and Gradient");
//        TestOptimizable.testValueAndGradientCurrentParameters (this);
  }

代码示例来源:origin: cc.mallet/mallet

private static double[] calcFeatureCounts (InstanceList ilist)
{
  int numInstances = ilist.size();
  int numClasses = ilist.getTargetAlphabet().size();
  int numFeatures = ilist.getDataAlphabet().size();
  double[] counts = new double[numFeatures];
  double count;
  for (int i = 0; i < ilist.size(); i++) {
    Instance inst = ilist.get(i);
    if (!(inst.getData() instanceof FeatureVector))
      throw new IllegalArgumentException ("Currently only handles FeatureVector data");
    FeatureVector fv = (FeatureVector) inst.getData ();
    if (ilist.getInstanceWeight(i) == 0)
      continue;
    for (int j = 0; j < fv.numLocations(); j++) {
      if (countInstances)
        counts[fv.indexAtLocation(j)] += 1;
      else
        counts[fv.indexAtLocation(j)] += fv.valueAtLocation(j);
    }                    
  }
  return counts;
}

相关文章

微信公众号

最新文章

更多