/** * 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); }
/** * 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); }
/** * 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); }
@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); }
@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); }
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; }
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; }
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; }
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); }
/** * 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(); }
/** * 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(); }
/** * 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(); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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; }