DoubleVector rowVector = features.getRowVector(row);
double[] logProbabilities = new double[classes];
// sum the probabilities for each class over all features
Iterator<DoubleVectorElement> iterateNonZero = rowVector.iterateNonZero();
while (iterateNonZero.hasNext()) {
DoubleVectorElement next = iterateNonZero.next();
for (int i = 0; i < classes; i++) {
logProbabilities[i] += theta.get(i, next.getIndex());
}
}
double z = logSum(logProbabilities);
for (int i = 0; i < classes; i++) {
double prob = Math.exp(logProbabilities[i] - z);
iterateNonZero = rowVector.iterateNonZero();
while (iterateNonZero.hasNext()) {
DoubleVectorElement next = iterateNonZero.next();
gradient.set(i, next.getIndex(), gradient.get(i, next.getIndex())
+ prob);
if (correctPrediction(i, outcome.getRowVector(row))) {
gradient.set(i, next.getIndex(),
gradient.get(i, next.getIndex()) - 1d);
}
}
if (correctPrediction(i, outcome.getRowVector(row))) {
cost -= Math.log(prob);
}