// Store the dictionary
if (outputAlphabet == null) {
System.err.println("Output dictionary null.");
}
MEMM crf = new MEMM(inputAlphabet, outputAlphabet);
MEMMTrainer memmt = new MEMMTrainer (crf);
String[] stateNames = new String[numStates];
for (int i = 0; i < numStates; i++)
stateNames[i] = "state" + i;
MEMM saveCRF = crf;
//inputAlphabet = (Feature.Alphabet) crf.getInputAlphabet();
FeatureVectorSequence fvs = new FeatureVectorSequence(new FeatureVector[]{
new FeatureVector(crf.getInputAlphabet(), new int[]{1, 2, 3}, new double[]{1, 1, 1}),
new FeatureVector(crf.getInputAlphabet(), new int[]{1, 2, 3}, new double[]{1, 1, 1}),
new FeatureVector(crf.getInputAlphabet(), new int[]{1, 2, 3}, new double[]{1, 1, 1}),
new FeatureVector(crf.getInputAlphabet(), new int[]{1, 2, 3}, new double[]{1, 1, 1}),
});
FeatureSequence ss = new FeatureSequence(crf.getOutputAlphabet(), new int[]{0, 1, 2, 3});
InstanceList ilist = new InstanceList(null);
ilist.add(fvs, ss, null, null);
crf.addFullyConnectedStates(stateNames);
try {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f));
oos.writeObject(crf);
oos.close();
} catch (IOException e) {
System.err.println("Exception writing file: " + e);
}
System.err.println("Wrote out CRF");
// And read it back in
crf = null;
try {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f));
crf = (MEMM) ois.readObject();
ois.close();
} catch (IOException e) {
System.err.println("Exception reading file: " + e);
} catch (ClassNotFoundException cnfe) {
System.err.println("Cound not find class reading in object: " + cnfe);
}
System.err.println("Read in CRF.");
try {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f2));
oos.writeObject(crf);
oos.close();
} catch (IOException e) {
System.err.println("Exception writing file: " + e);
}
System.err.println("Wrote out CRF");
if (useSave == 1) {
crf = saveCRF;
}
// MEMM.OptimizableCRF mcrf = crf.getMaximizableCRF(ilist);
Optimizable.ByGradientValue mcrf = memmt.getOptimizableMEMM(ilist);
double unconstrainedCost = new SumLatticeDefault (crf, fvs).getTotalWeight();
double constrainedCost = new SumLatticeDefault (crf, fvs, ss).getTotalWeight();
double minimizableCost = 0, minimizableGradientNorm = 0;
double[] gradient = new double [mcrf.getNumParameters()];