当前位置: 首页>>代码示例>>Java>>正文


Java Instance.getLabeling方法代码示例

本文整理汇总了Java中cc.mallet.types.Instance.getLabeling方法的典型用法代码示例。如果您正苦于以下问题:Java Instance.getLabeling方法的具体用法?Java Instance.getLabeling怎么用?Java Instance.getLabeling使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在cc.mallet.types.Instance的用法示例。


在下文中一共展示了Instance.getLabeling方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: incorporateOneInstance

import cc.mallet.types.Instance; //导入方法依赖的package包/类
private void incorporateOneInstance (Instance instance, double instanceWeight) 
{
  Labeling labeling = instance.getLabeling ();
  if (labeling == null) return; // Handle unlabeled instances by skipping them
  FeatureVector fv = (FeatureVector) instance.getData ();
  double oneNorm = fv.oneNorm();
  if (oneNorm <= 0) return; // Skip instances that have no features present
  if (docLengthNormalization > 0)
  	// Make the document have counts that sum to docLengthNormalization
  	// I.e., if 20, it would be as if the document had 20 words.
  	instanceWeight *= docLengthNormalization / oneNorm;
  assert (instanceWeight > 0 && !Double.isInfinite(instanceWeight));
  for (int lpos = 0; lpos < labeling.numLocations(); lpos++) {
    int li = labeling.indexAtLocation (lpos);
    double labelWeight = labeling.valueAtLocation (lpos);
    if (labelWeight == 0) continue;
    //System.out.println ("NaiveBayesTrainer me.increment "+ labelWeight * instanceWeight);
    me[li].increment (fv, labelWeight * instanceWeight);
    // This relies on labelWeight summing to 1 over all labels
    pe.increment (li, labelWeight * instanceWeight);
  }
}
 
开发者ID:kostagiolasn,项目名称:NucleosomePatternClassifier,代码行数:23,代码来源:NaiveBayesTrainer.java

示例2: train

import cc.mallet.types.Instance; //导入方法依赖的package包/类
public NaiveBayes train (InstanceList trainingSet)
{

  // Get a classifier trained on the labeled examples only
  NaiveBayes c = (NaiveBayes) nbTrainer.newClassifierTrainer().train (trainingSet);
  double prevLogLikelihood = 0, logLikelihood = 0;
  boolean converged = false;

  int iteration = 0;
  while (!converged) {
    // Make a new trainingSet that has some labels set
    InstanceList trainingSet2 = new InstanceList (trainingSet.getPipe());
    for (int ii = 0; ii < trainingSet.size(); ii++) {
      Instance inst = trainingSet.get(ii);
      if (inst.getLabeling() != null)
        trainingSet2.add(inst, 1.0);
      else {
        Instance inst2 = inst.shallowCopy();
        inst2.unLock();
        inst2.setLabeling(c.classify(inst).getLabeling());
        inst2.lock();
        trainingSet2.add(inst2, unlabeledDataWeight);
      }
    }
    c = (NaiveBayes) nbTrainer.newClassifierTrainer().train (trainingSet2);
    logLikelihood = c.dataLogLikelihood (trainingSet2);
    System.err.println ("Loglikelihood = "+logLikelihood);
    // Wait for a change in log-likelihood of less than 0.01% and at least 10 iterations
    if (Math.abs((logLikelihood - prevLogLikelihood)/logLikelihood) < 0.0001)
      converged = true;
    prevLogLikelihood = logLikelihood;
    iteration++;
  }
  return c;    
}
 
开发者ID:kostagiolasn,项目名称:NucleosomePatternClassifier,代码行数:36,代码来源:NaiveBayesEMTrainer.java

示例3: getValue

import cc.mallet.types.Instance; //导入方法依赖的package包/类
public double getValue ()
	{
		if (cachedValueStale) {
			numGetValueCalls++;
			cachedValue = 0;
			// We'll store the expectation values in "cachedGradient" for now
			cachedGradientStale = true;
			MatrixOps.setAll (cachedGradient, 0.0);
			// Incorporate likelihood of data
			double[] scores = new double[trainingList.getTargetAlphabet().size()];
			double value = 0.0;
			Iterator<Instance> iter = trainingList.iterator();
			int ii=0;
			while (iter.hasNext()) {
				ii++;
				Instance instance = iter.next();
				double instanceWeight = trainingList.getInstanceWeight(instance);
				Labeling labeling = instance.getLabeling ();
				if (labeling == null)
					continue;
				//System.out.println("L Now "+inputAlphabet.size()+" regular features.");

				this.theClassifier.getClassificationScores (instance, scores);
				FeatureVector fv = (FeatureVector) instance.getData ();
				int li = labeling.getBestIndex();
				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;
//					continue;
				}
				cachedValue += value;
				for (int si = 0; si < scores.length; si++) {
					if (scores[si] == 0) continue;
					assert (!Double.isInfinite(scores[si]));
					MatrixOps.rowPlusEquals (cachedGradient, numFeatures,
							si, fv, -instanceWeight * scores[si]);
					cachedGradient[numFeatures*si + defaultFeatureIndex] += (-instanceWeight * scores[si]);
				}
			}
			//logger.info ("-Expectations:"); cachedGradient.print();

			// Incorporate prior on parameters
			double prior = 0;
			if (usingHyperbolicPrior) {
				for (int li = 0; li < numLabels; li++)
					for (int fi = 0; fi < numFeatures; fi++)
						prior += (hyperbolicPriorSlope / hyperbolicPriorSharpness
								* Math.log (Maths.cosh (hyperbolicPriorSharpness * parameters[li *numFeatures + fi])));
			}
			else if (usingGaussianPrior) {
				for (int li = 0; li < numLabels; li++)
					for (int fi = 0; fi < numFeatures; fi++) {
						double param = parameters[li*numFeatures + fi];
						prior += param * param / (2 * gaussianPriorVariance);
					}
			}

			double oValue = cachedValue;
			cachedValue += prior;
			cachedValue *= -1.0; // MAXIMIZE, NOT MINIMIZE
			cachedValueStale = false;
			progressLogger.info ("Value (labelProb="+oValue+" prior="+prior+") loglikelihood = "+cachedValue);
		}
		return cachedValue;
	}
 
开发者ID:kostagiolasn,项目名称:NucleosomePatternClassifier,代码行数:76,代码来源:MaxEntOptimizableByLabelLikelihood.java

示例4: train

import cc.mallet.types.Instance; //导入方法依赖的package包/类
/**
	 * Trains winnow on the instance list, updating 
	 * {@link #weights weights} according to errors
	 * @param ilist Instance list to be trained on
	 * @return Classifier object containing learned weights
	 */
	public Winnow train (InstanceList trainingList)
	{
		FeatureSelection selectedFeatures = trainingList.getFeatureSelection();
		if (selectedFeatures != null)
			// xxx Attend to FeatureSelection!!!
			throw new UnsupportedOperationException ("FeatureSelection not yet implemented.");
		// if "train" is run more than once, 
		// we will be reinitializing the weights
		// TODO: provide method to save weights
		trainingList.getDataAlphabet().stopGrowth();
		trainingList.getTargetAlphabet().stopGrowth();
		Pipe dataPipe = trainingList.getPipe ();
		Alphabet dict = (Alphabet) trainingList.getDataAlphabet ();
		int numLabels = trainingList.getTargetAlphabet().size();
		int numFeats = dict.size(); 
		this.theta =  numFeats * this.nfactor;
		this.weights = new double [numLabels][numFeats];
		// init weights to 1
		for(int i=0; i<numLabels; i++)
			for(int j=0; j<numFeats; j++)
				this.weights[i][j] = 1.0;
		//System.out.println("Init weights to 1.  Theta= "+theta);
		// loop through all instances
		for (int ii = 0; ii < trainingList.size(); ii++){
			Instance inst = (Instance) trainingList.get(ii);
			Labeling labeling = inst.getLabeling ();
			FeatureVector fv = (FeatureVector) inst.getData ();
			double[] results = new double [numLabels]; 
			int fvisize = fv.numLocations();
			int correctIndex = labeling.getBestIndex();
			
			for(int rpos=0; rpos < numLabels; rpos++)
		    results[rpos]=0;
			// sum up xi*wi for each class
			for(int fvi=0; fvi < fvisize; fvi++){
				int fi = fv.indexAtLocation(fvi);
				//System.out.println("feature index "+fi);
				for(int lpos=0; lpos < numLabels; lpos++)
			    results[lpos] += this.weights[lpos][fi];
			}
			//System.out.println("In instance " + ii);
			// make guess for each label using threshold
			// update weights according to alpha and beta 
			// upon incorrect guess
			for(int ri=0; ri < numLabels; ri++){
				if(results[ri] > this.theta){ // guess 1
					if(correctIndex != ri) // correct is 0
				    demote(ri, fv);
				}
				else{ // guess 0
					if(correctIndex == ri) // correct is 1
						promote(ri, fv);   
				}
			}
//			System.out.println("Results guessed:")
//		for(int x=0; x<numLabels; x++)
//		    System.out.println(results[x]);
//			System.out.println("Correct label: "+correctIndex );
//			System.out.println("Weights are");
//			for(int h=0; h<numLabels; h++){
//				for(int g=0; g<numFeats; g++)
//			    System.out.println(weights[h][g]);
//				System.out.println("");
//			}
		}
		classifier = new Winnow (dataPipe, weights, theta, numLabels, numFeats);
		return classifier;
	}
 
开发者ID:kostagiolasn,项目名称:NucleosomePatternClassifier,代码行数:75,代码来源:WinnowTrainer.java


注:本文中的cc.mallet.types.Instance.getLabeling方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。