@Override
public CostGradientTuple evaluateCost(DoubleVector input) {
// TODO if you are really caring about performance and memory usage, you can
// implement some methods to translate the indices from matrices to vectors,
// so you don't have to copy all that memory.
DoubleMatrix theta = DenseMatrixFolder.unfoldMatrix(input, classes,
(int) (input.getLength() / (double) classes));
DenseDoubleMatrix gradient = new DenseDoubleMatrix(theta.getRowCount(),
theta.getColumnCount());
double cost = 0d;
// loop over all feature rows to determine the probabilities
for (int row = 0; row < m; row++) {
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);