本文整理汇总了Java中cc.mallet.optimize.Optimizable类的典型用法代码示例。如果您正苦于以下问题:Java Optimizable类的具体用法?Java Optimizable怎么用?Java Optimizable使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Optimizable类属于cc.mallet.optimize包,在下文中一共展示了Optimizable类的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: train
import cc.mallet.optimize.Optimizable; //导入依赖的package包/类
public boolean train (ACRF acrf, InstanceList trainingList, InstanceList validationList, InstanceList testSet,
ACRFEvaluator eval, int numIter, Optimizable.ByGradientValue macrf)
{
if (wrongWrongType == NO_WRONG_WRONG) {
return super.train (acrf, trainingList, validationList, testSet, eval, numIter, macrf);
} else {
Maxable bipwMaxable = (Maxable) macrf;
// add wrong wrongs after 5 iterations
logger.info ("BiconditionalPiecewiseACRFTrainer: Initial training");
super.train (acrf, trainingList, validationList, testSet, eval, wrongWrongIter, macrf);
FileUtils.writeGzippedObject (new File (outputPrefix, "initial-acrf.ser.gz"), acrf);
logger.info ("BiconditionalPiecewiseACRFTrainer: Adding wrong-wrongs");
bipwMaxable.addWrongWrong (trainingList);
logger.info ("BiconditionalPiecewiseACRFTrainer: Adding wrong-wrongs");
boolean converged = super.train (acrf, trainingList, validationList, testSet, eval, numIter, macrf);
reportTrainingLikelihood (acrf, trainingList);
return converged;
}
}
示例2: incrementalTrain
import cc.mallet.optimize.Optimizable; //导入依赖的package包/类
public boolean incrementalTrain (ACRF acrf,
InstanceList training,
InstanceList validation,
InstanceList testing,
ACRFEvaluator eval,
int numIter)
{
long stime = new Date ().getTime ();
for (int i = 0; i < SIZE.length; i++) {
InstanceList subset = training.split (new double[]
{SIZE[i], 1 - SIZE[i]})[0];
logger.info ("Training on subset of size " + subset.size ());
Optimizable.ByGradientValue subset_macrf = createOptimizable (acrf, subset);
train (acrf, training, validation, null, eval,
SUBSET_ITER, subset_macrf);
logger.info ("Subset training " + i + " finished...");
}
long etime = new Date ().getTime ();
logger.info ("All subset training finished. Time = " + (etime - stime) + " ms.");
return train (acrf, training, validation, testing, eval, numIter);
}
示例3: someUnsupportedTrain
import cc.mallet.optimize.Optimizable; //导入依赖的package包/类
public boolean someUnsupportedTrain (ACRF acrf,
InstanceList trainingList,
InstanceList validationList,
InstanceList testSet,
ACRFEvaluator eval,
int numIter)
{
Optimizable.ByGradientValue macrf = createOptimizable (acrf, trainingList);
train (acrf, trainingList, validationList, testSet, eval, 5, macrf);
ACRF.Template[] tmpls = acrf.getTemplates ();
for (int ti = 0; ti < tmpls.length; ti++)
tmpls[ti].addSomeUnsupportedWeights (trainingList);
logger.info ("Some unsupporetd weights initialized. Training...");
return train (acrf, trainingList, validationList, testSet, eval, numIter, macrf);
}
示例4: testGetSetParameters
import cc.mallet.optimize.Optimizable; //导入依赖的package包/类
public void testGetSetParameters() {
int inputVocabSize = 100;
int numStates = 5;
Alphabet inputAlphabet = new Alphabet();
for (int i = 0; i < inputVocabSize; i++)
inputAlphabet.lookupIndex("feature" + i);
Alphabet outputAlphabet = new Alphabet();
CRF crf = new CRF(inputAlphabet, outputAlphabet);
String[] stateNames = new String[numStates];
for (int i = 0; i < numStates; i++)
stateNames[i] = "state" + i;
crf.addFullyConnectedStates(stateNames);
CRFTrainerByLabelLikelihood crft = new CRFTrainerByLabelLikelihood(crf);
Optimizable.ByGradientValue mcrf = crft
.getOptimizableCRF(new InstanceList(null));
TestOptimizable.testGetSetParameters(mcrf);
}
示例5: testPrint
import cc.mallet.optimize.Optimizable; //导入依赖的package包/类
public void testPrint() {
Pipe p = new SerialPipes(new Pipe[] {
new CharSequence2TokenSequence("."), new TokenText(),
new TestCRFTokenSequenceRemoveSpaces(),
new TokenSequence2FeatureVectorSequence(),
new PrintInputAndTarget(), });
InstanceList one = new InstanceList(p);
String[] data = new String[] { "ABCDE", };
one.addThruPipe(new ArrayIterator(data));
CRF crf = new CRF(p, null);
crf.addFullyConnectedStatesForThreeQuarterLabels(one);
CRFTrainerByLabelLikelihood crft = new CRFTrainerByLabelLikelihood(crf);
crf.setWeightsDimensionAsIn(one, false);
Optimizable mcrf = crft.getOptimizableCRF(one);
double[] params = new double[mcrf.getNumParameters()];
for (int i = 0; i < params.length; i++) {
params[i] = i;
}
mcrf.setParameters(params);
crf.print();
}
示例6: testSpaceSerializable
import cc.mallet.optimize.Optimizable; //导入依赖的package包/类
public void testSpaceSerializable () throws IOException, ClassNotFoundException
{
Pipe p = makeSpacePredictionPipe ();
InstanceList training = new InstanceList (p);
training.addThruPipe (new ArrayIterator (data));
MEMM memm = new MEMM (p, null);
memm.addFullyConnectedStatesForLabels ();
memm.addStartState();
memm.setWeightsDimensionAsIn(training);
MEMMTrainer memmt = new MEMMTrainer (memm);
memmt.train (training, 10);
MEMM memm2 = (MEMM) TestSerializable.cloneViaSerialization (memm);
Optimizable.ByGradientValue mcrf1 = memmt.getOptimizableMEMM(training);
double val1 = mcrf1.getValue ();
Optimizable.ByGradientValue mcrf2 = memmt.getOptimizableMEMM(training);
double val2 = mcrf2.getValue ();
assertEquals (val1, val2, 1e-5);
}
示例7: ThreadedOptimizable
import cc.mallet.optimize.Optimizable; //导入依赖的package包/类
/**
* Initializes the optimizable and starts new threads.
*
* @param optimizable Optimizable to be parallelized
* @param numFactors Number of factors in model's parameters, used to
* initialize the gradient
* @param cacheIndicator Determines when value/gradient become stale
*/
public ThreadedOptimizable(Optimizable.ByCombiningBatchGradient optimizable,
InstanceList trainingSet, int numFactors,
CacheStaleIndicator cacheIndicator) {
// set up
this.trainingSet = trainingSet;
this.optimizable = optimizable;
int numBatches = optimizable.getNumBatches();
assert(numBatches > 0) : "Invalid number of batches: " + numBatches;
batchCachedValue = new double[numBatches];
batchCachedGradient = new ArrayList<double[]>(numBatches);
for (int i = 0; i < numBatches; ++i) {
batchCachedGradient.add(new double[numFactors]);
}
this.cacheIndicator = cacheIndicator;
logger.info("Creating " + numBatches + " threads for updating gradient...");
executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(numBatches);
this.createTasks();
}
示例8: testGetSetParameters
import cc.mallet.optimize.Optimizable; //导入依赖的package包/类
/**
* Tests that parameters set by setParameters can be retrieved by
* getParameters.
* @param maxable Instance of a Maximizable that should be tested.
* Its current parameters will be overwritten.
*/
public static boolean testGetSetParameters (Optimizable maxable)
{
System.out.println ("TestMaximizable testGetSetParameters");
// Set all the parameters to unique values using setParameters()
double[] parameters = new double [maxable.getNumParameters()];
maxable.getParameters (parameters);
for (int i = 0; i < parameters.length; i++)
parameters[i] = (double)i;
maxable.setParameters (parameters);
// Test to make sure those parameters are there
MatrixOps.setAll (parameters, 0.0);
maxable.getParameters (parameters);
for (int i = 0; i < parameters.length; i++)
assertTrue (parameters[i] == (double)i);
return true;
}
示例9:
import cc.mallet.optimize.Optimizable; //导入依赖的package包/类
/**
* Tests that getValue and getValueGradient are consistent.
* Tests for consistency at <tt>params = 0</tt> and at
* <tt> params = -0.0001 * grad(f)</tt>
* @see #testValueAndGradientCurrentParameters testValueAndGradientCurrentParameters
* @throws IllegalStateException If the test fails.
*/
public static boolean testValueAndGradient (Optimizable.ByGradientValue maxable)
{
double[] parameters = new double [maxable.getNumParameters()];
MatrixOps.setAll (parameters, 0.0);
maxable.setParameters (parameters);
testValueAndGradientCurrentParameters (maxable);
MatrixOps.setAll (parameters, 0.0);
double[] delta = new double[maxable.getNumParameters()];
maxable.getValueGradient (delta);
logger.info ("Gradient two-Norm = "+MatrixOps.twoNorm(delta));
logger.info (" max parameter change = "+(MatrixOps.infinityNorm(delta) * -0.001));
MatrixOps.timesEquals (delta, -0.0001);
MatrixOps.plusEquals (parameters, delta);
maxable.setParameters (parameters);
testValueAndGradientCurrentParameters (maxable);
return true;
}
示例10: ignoretestSpaceSerializable
import cc.mallet.optimize.Optimizable; //导入依赖的package包/类
public void ignoretestSpaceSerializable () throws IOException, ClassNotFoundException
{
Pipe p = makeSpacePredictionPipe ();
InstanceList training = new InstanceList (p);
training.addThruPipe (new ArrayIterator (data));
MEMM memm = new MEMM (p, null);
memm.addFullyConnectedStatesForLabels ();
memm.addStartState();
memm.setWeightsDimensionAsIn(training);
MEMMTrainer memmt = new MEMMTrainer (memm);
memmt.train (training, 10);
MEMM memm2 = (MEMM) TestSerializable.cloneViaSerialization (memm);
Optimizable.ByGradientValue mcrf1 = memmt.getOptimizableMEMM(training);
double val1 = mcrf1.getValue ();
Optimizable.ByGradientValue mcrf2 = memmt.getOptimizableMEMM(training);
double val2 = mcrf2.getValue ();
assertEquals (val1, val2, 1e-5);
}
示例11: train
import cc.mallet.optimize.Optimizable; //导入依赖的package包/类
boolean train (ACRF acrf,
InstanceList training,
InstanceList validation,
InstanceList testing,
ACRFEvaluator eval,
int numIter,
Optimizable.ByGradientValue macrf);
示例12: train
import cc.mallet.optimize.Optimizable; //导入依赖的package包/类
public boolean train (ACRF acrf,
InstanceList trainingList,
InstanceList validationList,
InstanceList testSet,
ACRFEvaluator eval,
int numIter)
{
Optimizable.ByGradientValue macrf = createOptimizable (acrf, trainingList);
return train (acrf, trainingList, validationList, testSet,
eval, numIter, macrf);
}