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


Java Instance.getTarget方法代码示例

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


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

示例1: main

import cc.mallet.types.Instance; //导入方法依赖的package包/类
public static void main (String[] args)
	{
		try {
			Pipe p = new SerialPipes (new Pipe[] {
				new Input2CharSequence (),
				new SGML2TokenSequence()
//				new SGML2TokenSequence (new CharSequenceLexer (Pattern.compile (".")), "O")
				});

			for (int i = 0; i < args.length; i++) {
				Instance carrier = p.instanceFrom(new Instance (new File(args[i]), null, null, null));
				TokenSequence data = (TokenSequence) carrier.getData();
				TokenSequence target = (TokenSequence) carrier.getTarget();
				logger.finer ("===");
				logger.info (args[i]);
				for (int j = 0; j < data.size(); j++)
					logger.info (target.get(j).getText()+" "+data.get(j).getText());
			}
		} catch (Exception e) {
			System.out.println (e);
			e.printStackTrace();
		}
	}
 
开发者ID:kostagiolasn,项目名称:NucleosomePatternClassifier,代码行数:24,代码来源:SGML2TokenSequence.java

示例2: pipe

import cc.mallet.types.Instance; //导入方法依赖的package包/类
@Override
public Instance pipe(Instance carrier) {

    TokenSequence targets = (TokenSequence) carrier.getTarget();

    for (int i = 0; i < targets.size(); i++) {

        Token target = targets.get(i);
        // System.out.println(t.getText());
        String targetLabel = target.getText();
        // System.out.println(lineWithoutFirst);
        // targetTokenSeq.add(lineSplit[0]);
        if (this.replacementMap.containsKey(targetLabel)) {
            target.setText(this.replacementMap.get(targetLabel));
        }
    }
    carrier.setTarget(targets);

    return carrier;
}
 
开发者ID:exciteproject,项目名称:refext,代码行数:21,代码来源:TargetReplacementPipe.java

示例3: labelConnectionsIn

import cc.mallet.types.Instance; //导入方法依赖的package包/类
private boolean[][] labelConnectionsIn (InstanceList trainingSet, String start)
{
	int numLabels = outputAlphabet.size();
	boolean[][] connections = new boolean[numLabels][numLabels];
	for (int i = 0; i < trainingSet.size(); i++) {
		Instance instance = trainingSet.get(i);
		FeatureSequence output = (FeatureSequence) instance.getTarget();
		for (int j = 1; j < output.size(); j++) {
			int sourceIndex = outputAlphabet.lookupIndex (output.get(j-1));
			int destIndex = outputAlphabet.lookupIndex (output.get(j));
			assert (sourceIndex >= 0 && destIndex >= 0);
			connections[sourceIndex][destIndex] = true;
		}
	}

	// Handle start state
	if (start != null) {
		int startIndex = outputAlphabet.lookupIndex (start);
		for (int j = 0; j < outputAlphabet.size(); j++) {
			connections[startIndex][j] = true;
		}
	}

	return connections;
}
 
开发者ID:kostagiolasn,项目名称:NucleosomePatternClassifier,代码行数:26,代码来源:CRF.java

示例4: predict

import cc.mallet.types.Instance; //导入方法依赖的package包/类
/** This method is deprecated. */
// But it is here as a reminder to do something about induceFeaturesFor(). */
@Deprecated
public Sequence[] predict (InstanceList testing) {
	testing.setFeatureSelection(this.globalFeatureSelection);
	for (int i = 0; i < featureInducers.size(); i++) {
		FeatureInducer klfi = (FeatureInducer)featureInducers.get(i);
		klfi.induceFeaturesFor (testing, false, false);
	}
	Sequence[] ret = new Sequence[testing.size()];
	for (int i = 0; i < testing.size(); i++) {
		Instance instance = testing.get(i);
		Sequence input = (Sequence) instance.getData();
		Sequence trueOutput = (Sequence) instance.getTarget();
		assert (input.size() == trueOutput.size());
		Sequence predOutput = new MaxLatticeDefault(this, input).bestOutputSequence();
		assert (predOutput.size() == trueOutput.size());
		ret[i] = predOutput;
	}
	return ret;
}
 
开发者ID:kostagiolasn,项目名称:NucleosomePatternClassifier,代码行数:22,代码来源:CRF.java

示例5: trainIncrementalLikelihood

import cc.mallet.types.Instance; //导入方法依赖的package包/类
/**
 * Adjust the parameters by learning rate according to the gradient of this
 * single Instance, and return the true label sequence likelihood.
 */
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,
			expectations.new Incrementor()).getTotalWeight();
	// Calculate parameter gradient given these instances: (constraints -
	// expectations)
	constraints.plusEquals(expectations, -1);
	// Change the parameters a little by this difference, obeying
	// weightsFrozen
	crf.parameters.plusEquals(constraints, rate, true);

	return singleLoglik;
}
 
开发者ID:kostagiolasn,项目名称:NucleosomePatternClassifier,代码行数:26,代码来源:CRFTrainerByStochasticGradient.java

示例6: evaluateInstanceList

import cc.mallet.types.Instance; //导入方法依赖的package包/类
public void evaluateInstanceList (TransducerTrainer tt, InstanceList data, String description)
{
  int correct = 0;
  for (int i = 0; i < data.size(); i++) {
    Instance instance = data.get(i);
    Sequence input = (Sequence) instance.getData();
    Sequence trueOutput = (Sequence) instance.getTarget();
    assert (input.size() == trueOutput.size());
    Sequence predOutput = tt.getTransducer().transduce (input);
    assert (predOutput.size() == trueOutput.size());
    if (sequencesMatch (trueOutput, predOutput))
      correct++;
    }
  double acc = ((double)correct) / data.size();
accuracy.put(description, acc);

  logger.info (description+" Num instances = "+data.size()+"  Num correct = "+correct+" Per-instance accuracy = "+acc);
}
 
开发者ID:kostagiolasn,项目名称:NucleosomePatternClassifier,代码行数:19,代码来源:InstanceAccuracyEvaluator.java

示例7: evaluateInstanceList

import cc.mallet.types.Instance; //导入方法依赖的package包/类
public void evaluateInstanceList (TransducerTrainer trainer, InstanceList instances, String description) 
 {
	int numCorrectTokens;
	int totalTokens;

	Transducer transducer = trainer.getTransducer();
	totalTokens = numCorrectTokens = 0;
	for (int i = 0; i < instances.size(); i++) {
		Instance instance = instances.get(i);
		Sequence input = (Sequence) instance.getData();
		Sequence trueOutput = (Sequence) instance.getTarget();
		assert (input.size() == trueOutput.size());
		//System.err.println ("TokenAccuracyEvaluator "+i+" length="+input.size());
		Sequence predOutput = transducer.transduce (input);
		assert (predOutput.size() == trueOutput.size());

		for (int j = 0; j < trueOutput.size(); j++) {
			totalTokens++;
			if (trueOutput.get(j).equals(predOutput.get(j)))
				numCorrectTokens++;
		}
		//System.err.println ("TokenAccuracyEvaluator "+i+" numCorrectTokens="+numCorrectTokens+" totalTokens="+totalTokens+" accuracy="+((double)numCorrectTokens)/totalTokens);
	}
	double acc = ((double)numCorrectTokens)/totalTokens;
	//System.err.println ("TokenAccuracyEvaluator accuracy="+acc);
	accuracy.put(description, acc);
	logger.info (description +" accuracy="+acc);
}
 
开发者ID:kostagiolasn,项目名称:NucleosomePatternClassifier,代码行数:29,代码来源:TokenAccuracyEvaluator.java

示例8: pipe

import cc.mallet.types.Instance; //导入方法依赖的package包/类
public Instance pipe (Instance carrier) {
	if (carrier.getTarget() != null) {
		if (! (carrier.getTarget() instanceof String)) {
			throw new IllegalArgumentException ("Target must be a String for conversion to Integer");
		}
		carrier.setTarget( new Integer((String) carrier.getTarget()) );
	}
	return carrier;
}
 
开发者ID:kostagiolasn,项目名称:NucleosomePatternClassifier,代码行数:10,代码来源:Target2Integer.java

示例9: pipe

import cc.mallet.types.Instance; //导入方法依赖的package包/类
public Instance pipe(Instance carrier) {
	StringBuffer sb = new StringBuffer();
	String source = (String) carrier.getSource();
	Sequence as = (Sequence) carrier.getTarget();
	// int startLabelIndex = as.getAlphabet().lookupIndex("start");
	for (int i = 0; i < source.length(); i++) {
		System.out.println("target[" + i + "]=" + as.get(i).toString());
		if (as.get(i).toString().equals("start") && i != 0)
			sb.append(' ');
		sb.append(source.charAt(i));
	}
	carrier.setSource(sb.toString());
	System.out.println("carrier.getSource() = " + carrier.getSource());
	return carrier;
}
 
开发者ID:kostagiolasn,项目名称:NucleosomePatternClassifier,代码行数:16,代码来源:TestCRF.java

示例10: pipe

import cc.mallet.types.Instance; //导入方法依赖的package包/类
public Instance pipe (Instance carrier) {
	if (carrier.getTarget() != null) {
		if (! (carrier.getTarget() instanceof String)) {
			throw new IllegalArgumentException ("Target must be a string for conversion to Double");
		}
		carrier.setTarget( new Double((String) carrier.getTarget()) );
	}
	return carrier;
}
 
开发者ID:kostagiolasn,项目名称:NucleosomePatternClassifier,代码行数:10,代码来源:Target2Double.java

示例11: convert

import cc.mallet.types.Instance; //导入方法依赖的package包/类
/**
 * 
 * @param inst input instance, with FeatureVectorSequence as data.
 * @param alphabetsPipe a Noop pipe containing the data and target alphabets for 
 * the resulting InstanceList and AugmentableFeatureVectors
 * @return list of instances, each with one AugmentableFeatureVector as data
 */
public static InstanceList convert(Instance inst, Noop alphabetsPipe)
{
	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() + "[email protected]_POS_" + (j + 1);
		Object source = inst.getSource();
		Instance toAdd = alphabetsPipe.pipe(new Instance(data, target, name, source));

		ret.add(toAdd);
	}

	return ret;
}
 
开发者ID:kostagiolasn,项目名称:NucleosomePatternClassifier,代码行数:35,代码来源:AddClassifierTokenPredictions.java

示例12: pipe

import cc.mallet.types.Instance; //导入方法依赖的package包/类
public Instance pipe (Instance carrier)
{
	 if (prefix != null)
		 System.out.print (prefix);
   String targetString = "<null>";
   if (carrier.getTarget() != null)
    targetString = carrier.getTarget().toString();
	System.out.println ("name: " + carrier.getName() + 
						"\ntarget: " + targetString + 
						"\ninput: " + carrier.getData()  // Swapping order, since data often has a newline at the end -DM
						);
	return carrier;
}
 
开发者ID:kostagiolasn,项目名称:NucleosomePatternClassifier,代码行数:14,代码来源:PrintInputAndTarget.java

示例13: testXis

import cc.mallet.types.Instance; //导入方法依赖的package包/类
public void testXis() {
	Pipe p = makeSpacePredictionPipe();

	InstanceList instances = new InstanceList(p);
	instances.addThruPipe(new ArrayIterator(data));

	CRF crf1 = new CRF(p, null);
	crf1.addFullyConnectedStatesForLabels();
	CRFTrainerByLabelLikelihood crft1 = new CRFTrainerByLabelLikelihood(
			crf1);
	crft1.train(instances, 10); // Let's get some parameters

	Instance inst = instances.get(0);
	Sequence input = (Sequence) inst.getData();
	SumLatticeDefault lattice = new SumLatticeDefault(crf1, input,
			(Sequence) inst.getTarget(), null, true);
	for (int ip = 0; ip < lattice.length() - 1; ip++) {
		for (int i = 0; i < crf1.numStates(); i++) {
			Transducer.State state = crf1.getState(i);
			Transducer.TransitionIterator it = state.transitionIterator(
					input, ip);
			double gamma = lattice.getGammaProbability(ip, state);
			double xiSum = 0;
			while (it.hasNext()) {
				Transducer.State dest = it.nextState();
				double xi = lattice.getXiProbability(ip, state, dest);
				xiSum += xi;
			}
			assertEquals(gamma, xiSum, 1e-5);
		}
	}
}
 
开发者ID:kostagiolasn,项目名称:NucleosomePatternClassifier,代码行数:33,代码来源:TestCRF.java

示例14: gatherExpectationsOrConstraints

import cc.mallet.types.Instance; //导入方法依赖的package包/类
protected double gatherExpectationsOrConstraints (boolean gatherConstraints)
		{
			// Instance values must either always or never be included in
			// the total values; we can't just sometimes skip a value
			// because it is infinite, this throws off the total values.
			boolean initializingInfiniteValues = false;
			CRF.Factors factors = gatherConstraints ? constraints : expectations;
			CRF.Factors.Incrementor factorIncrementor = factors.new Incrementor ();

			if (infiniteValues == null) {
				infiniteValues = new BitSet ();
				initializingInfiniteValues = true;
			}

			double labelLogProb = 0;
			for (int i = 0; i < memm.numStates(); i++) {
				MEMM.State s = (State) memm.getState (i);

				if (s.trainingSet == null) {
					System.out.println ("Empty training set for state "+s.name);
					continue;
				}

				for (int j = 0; j < s.trainingSet.size(); j++) {
					Instance instance = s.trainingSet.get (j);
					double instWeight = s.trainingSet.getInstanceWeight (j);
					FeatureVector fv = (FeatureVector) instance.getData ();
					String labelString = (String) instance.getTarget ();
					TransitionIterator iter = new TransitionIterator (s, fv, gatherConstraints?labelString:null, memm);
					while (iter.hasNext ()) {
					  // gsc
					  iter.nextState(); // advance the iterator
//						State destination = (MEMM.State) iter.nextState();  // Just to advance the iterator
						double weight = iter.getWeight();
						factorIncrementor.incrementTransition(iter, Math.exp(weight) * instWeight);
						//iter.incrementCount (Math.exp(weight) * instWeight);
						if (!gatherConstraints && iter.getOutput() == labelString) {
							if (!Double.isInfinite (weight))
								labelLogProb += instWeight * weight; // xxx   ?????
							else {
								logger.warning ("State "+i+" transition "+j+" has infinite cost; skipping.");
								if (initializingInfiniteValues)
									throw new IllegalStateException ("Infinite-cost transitions not yet supported"); //infiniteValues.set (j);
								else if (!infiniteValues.get(j))
									throw new IllegalStateException ("Instance i used to have non-infinite value, "
											+"but now it has infinite value.");
							}
						}
					}
				}
			}

			// Force initial & final weight parameters to 0 by making sure that 
			// whether factor refers to expectation or constraint, they have the same value.
			for (int i = 0; i < memm.numStates(); i++) {
				factors.initialWeights[i] = 0.0;
				factors.finalWeights[i] = 0.0;
			}

			return labelLogProb;
		}
 
开发者ID:kostagiolasn,项目名称:NucleosomePatternClassifier,代码行数:62,代码来源:MEMMTrainer.java

示例15: getExpectationValue

import cc.mallet.types.Instance; //导入方法依赖的package包/类
protected double getExpectationValue ()
{
	// Instance values must either always or never be included in
	// the total values; we can't just sometimes skip a value
	// because it is infinite, this throws off the total values.
	boolean initializingInfiniteValues = false;
	double value = 0;
	if (infiniteValues == null) {
		infiniteValues = new BitSet ();
		initializingInfiniteValues = true;
	}

	// Reset expectations to zero before we fill them again
	assert (expectations.structureMatches(crf.parameters));
	expectations.zero();

	// count the number of instances that have infinite weight
	int numInfLabeledWeight = 0;
	int numInfUnlabeledWeight = 0;
	int numInfWeight = 0;
	
	// Calculate the value of each instance, and also fill in expectations
	double unlabeledWeight, labeledWeight, weight;
	for (int ii = 0; ii < trainingSet.size(); ii++) {
		Instance instance = trainingSet.get(ii);
		double instanceWeight = trainingSet.getInstanceWeight(instance);
		FeatureVectorSequence input = (FeatureVectorSequence) instance.getData();
		FeatureSequence output = (FeatureSequence) instance.getTarget();
		labeledWeight = new SumLatticeDefault (this.crf, input, output, (Transducer.Incrementor)null).getTotalWeight();
		String instanceName = instance.getName() == null ? "instance#"+ii : instance.getName().toString();
		//System.out.println ("labeledWeight = "+labeledWeight);
		if (Double.isInfinite (labeledWeight)) {
			++numInfLabeledWeight;
			logger.warning (instanceName + " has -infinite labeled weight.\n"+(instance.getSource() != null ? instance.getSource() : ""));
		}
		
		Transducer.Incrementor incrementor = instanceWeight == 1.0 ? expectations.new Incrementor() : expectations.new WeightedIncrementor (instanceWeight);
		unlabeledWeight = new SumLatticeDefault (this.crf, input, null, incrementor).getTotalWeight();
		//System.out.println ("unlabeledWeight = "+unlabeledWeight);
		if (Double.isInfinite (unlabeledWeight)) {
			++numInfUnlabeledWeight;
			logger.warning (instance.getName().toString() + " has -infinite unlabeled weight.\n"+(instance.getSource() != null ? instance.getSource() : ""));
		}
		
		// Here weight is log(conditional probability correct label sequence)
		weight = labeledWeight - unlabeledWeight;
		//System.out.println ("Instance "+ii+" CRF.MaximizableCRF.getWeight = "+weight);
		if (Double.isInfinite(weight)) {
			++numInfWeight;
			logger.warning (instanceName + " has -infinite weight; skipping.");
			if (initializingInfiniteValues)
				infiniteValues.set (ii);
			else if (!infiniteValues.get(ii))
				throw new IllegalStateException ("Instance i used to have non-infinite value, but now it has infinite value.");
			continue;
		}
     // Weights are log probabilities, and we want to return a log probability
     value += weight * instanceWeight;
	}

	if (numInfLabeledWeight > 0 || numInfUnlabeledWeight > 0 || numInfWeight > 0) {
		logger.warning("Number of instances with:\n" +
				"\t -infinite labeled weight: " + numInfLabeledWeight + "\n" +
				"\t -infinite unlabeled weight: " + numInfUnlabeledWeight + "\n" +
				"\t -infinite weight: " + numInfWeight);
	}
	
	return value;
}
 
开发者ID:kostagiolasn,项目名称:NucleosomePatternClassifier,代码行数:70,代码来源:CRFOptimizableByLabelLikelihood.java


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