double instanceWeight = trainingList.getInstanceWeight(instance);
Labeling labeling = instance.getLabeling ();
//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 ("MCMaxEntTrainer: 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;
// CPAL - this is a loop over classes and their scores
// - we compute the gradient by taking the dot product of the feature value
// and the probability of the class
for (int si = 0; si < scores.length; si++) {
if (scores[si] == 0) continue;
assert (!Double.isInfinite(scores[si]));
// CPAL - accumulating the current classifiers expectation of the feature
// vector counts for this class label
// Current classifier has expectation over class label, not over feature vector
MatrixOps.rowPlusEquals (cachedGradient, numFeatures,
si, fv, -instanceWeight * scores[si]);
cachedGradient[numFeatures*si + defaultFeatureIndex] += (-instanceWeight * scores[si]);
}
// CPAL - if we wish to do multiconditional training we need another term for this accumulated
// expectation
if (usingMultiConditionalTraining) {
// need something analogous to this
// this.theClassifier.getClassificationScores (instance, scores);
// this.theClassifier.getFeatureDistributions (instance,
// Note: li is the "label" for this instance
// Get the sum of the feature vector
// which is the number of counts for the document if we use that as input
double Ncounts = MatrixOps.sum(fv);
// CPAL - get the additional term for the value of our - log probability
// - this computation amounts to the dot product of the feature vector and the probability vector
cachedValue -= (instanceWeight * fv.dotProduct(lprobs[li]));
// CPAL - get the model expectation over features for the given class
for (int fi = 0; fi < numFeatures; fi++) {
//if(parameters[numFeatures*li + fi] != 0) {