Iterator<Instance> iter = trainingList.iterator();
int ii=0;
while (iter.hasNext()) {
ii++;
Instance instance = iter.next();
FeatureVectorSequence fvs = (FeatureVectorSequence) instance.getData();
// scores stores Pr of subList[i] being positive instance
double[] scores = new double[fvs.size()];
double instanceWeight = trainingList.getInstanceWeight(instance);
// labeling is a String representation of an int, indicating which FeatureVector from
// the subList is the positive example
// If is String, proceed as usual. Else, if is String[], do
// not penalize scores for duplicate entries. This improved accuracy in some expts.
Object target = instance.getTarget();
int li = -1;
if (target instanceof Label) {
li = Integer.valueOf(((Label)target).toString()).intValue();
if (li == -1) // hack to avoid invalid instances
continue;
assert (li >=0 && li < fvs.size());
this.theClassifier.getClassificationScores (instance, scores);
} else if (target instanceof Labels){
Labels labels = (Labels)target;
int[] bestPositions = new int[labels.size()];
for (int pi = 0; pi < labels.size(); pi++)
bestPositions[pi] = Integer.valueOf(labels.get(pi).toString());
li = bestPositions[0];
this.theClassifier.getClassificationScoresForTies (instance, scores, bestPositions);
}
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;
}
cachedValue += value;
double positiveScore = scores[li];
for (int si=0; si < fvs.size(); si++) {
if (scores[si]==0)
continue;
assert (!Double.isInfinite(scores[si]));
FeatureVector cfv = (FeatureVector)fvs.get(si);
MatrixOps.rowPlusEquals (cachedGradient, numFeatures,
0, cfv, -instanceWeight * scores[si]);
cachedGradient[numFeatures*0 + defaultFeatureIndex] += (-instanceWeight * scores[si]);
}
}