@Override public double calculateLoss(DoubleMatrix y, DoubleMatrix hypothesis) { double sum = 0d; for (int col = 0; col < y.getColumnCount(); col++) { for (int row = 0; row < y.getRowCount(); row++) { double diff = y.get(row, col) - hypothesis.get(row, col); sum += (diff * diff); } } return sum / y.getRowCount(); }
@Override public double calculateLoss(DoubleMatrix y, DoubleMatrix hypothesis) { DoubleMatrix multiplyElementWise = y.multiplyElementWise(hypothesis); double sum = 0d; for (int i = 0; i < multiplyElementWise.getRowCount(); i++) { sum += FastMath.max(0, 1 - multiplyElementWise.get(i, 0)); } return sum / multiplyElementWise.getRowCount(); }
static double[] computeScores(int classes, DoubleVector features, DoubleMatrix weights) { double[] scores = new double[classes]; Iterator<DoubleVectorElement> iterateNonZero = features.iterateNonZero(); while (iterateNonZero.hasNext()) { DoubleVectorElement next = iterateNonZero.next(); for (int i = 0; i < scores.length; i++) { scores[i] += weights.get(i, next.getIndex()); } } return scores; }
@Override public DoubleMatrix gradient(DoubleMatrix matrix) { DoubleMatrix newInstance = newInstance(matrix); if (matrix.isSparse()) { // if we have a sparse matrix, it is more efficient to loop over the // sparse column vectors int[] columnIndices = matrix.columnIndices(); for (int col : columnIndices) { newInstance.setColumnVector(col, gradient(matrix.getColumnVector(col))); } } else { // on dense matrices we can be faster by directly looping over the items for (int i = 0; i < matrix.getRowCount(); i++) { for (int j = 0; j < matrix.getColumnCount(); j++) { newInstance.set(i, j, gradient(matrix.get(i, j))); } } } return newInstance; }
@Override public DoubleMatrix multiply(DoubleMatrix other) { int m = this.numRows; int n = this.numColumns; int p = other.getColumnCount(); DenseDoubleMatrix matrix = new DenseDoubleMatrix(m, p); for (int k = 0; k < n; k++) { for (int i = 0; i < m; i++) { for (int j = 0; j < p; j++) { matrix.set(i, j, matrix.get(i, j) + get(i, k) * other.get(k, j)); } } } return matrix; }
@Override public double calculateLoss(DoubleMatrix y, DoubleMatrix hypothesis) { double sum = 0d; for (int col = 0; col < y.getColumnCount(); col++) { for (int row = 0; row < y.getRowCount(); row++) { sum += FastMath.abs(y.get(row, col) - hypothesis.get(row, col)); } } return sum / y.getRowCount(); }
private double getProbabilityForClass(DoubleVector document, int classIndex) { double probabilitySum = 0.0d; Iterator<DoubleVectorElement> iterateNonZero = document.iterateNonZero(); while (iterateNonZero.hasNext()) { DoubleVectorElement next = iterateNonZero.next(); double wordCount = next.getValue(); double probabilityOfToken = probabilityMatrix.get(classIndex, next.getIndex()); if (probabilityOfToken == 0d) { probabilityOfToken = LOW_PROBABILITY; } probabilitySum += (wordCount * probabilityOfToken); } return probabilitySum; }
static DoubleMatrix binarize(Random r, DoubleMatrix hiddenActivations) { for (int i = 0; i < hiddenActivations.getRowCount(); i++) { for (int j = 0; j < hiddenActivations.getColumnCount(); j++) { hiddenActivations.set(i, j, hiddenActivations.get(i, j) > r.nextDouble() ? 1d : 0d); } } return hiddenActivations; }
@Override public DoubleMatrix multiplyElementWise(DoubleMatrix other) { DenseDoubleMatrix matrix = new DenseDoubleMatrix(this.numRows, this.numColumns); for (int i = 0; i < numRows; i++) { for (int j = 0; j < numColumns; j++) { matrix.set(i, j, this.get(i, j) * (other.get(i, j))); } } return matrix; }
/** * Folds a single matrix into a single vector by rows. */ public static DoubleVector foldMatrix(DoubleMatrix mat) { DoubleVector vec = new DenseDoubleVector(mat.getRowCount() * mat.getColumnCount()); int index = 0; for (int i = 0; i < mat.getRowCount(); i++) { for (int j = 0; j < mat.getColumnCount(); j++) { vec.set(index++, mat.get(i, j)); } } return vec; }
private double getProbabilityForClass(DoubleVector document, int classIndex) { double probabilitySum = 0.0d; Iterator<DoubleVectorElement> iterateNonZero = document.iterateNonZero(); while (iterateNonZero.hasNext()) { DoubleVectorElement next = iterateNonZero.next(); double wordCount = next.getValue(); double probabilityOfToken = model.getProbabilityMatrix().get(classIndex, next.getIndex()); if (probabilityOfToken == 0d) { probabilityOfToken = LOW_PROBABILITY; } probabilitySum += (wordCount * probabilityOfToken); } return probabilitySum; }
/** * @return a log'd matrix that was guarded against edge cases of the * logarithm. */ public static DoubleMatrix logMatrix(DoubleMatrix input) { DenseDoubleMatrix log = new DenseDoubleMatrix(input.getRowCount(), input.getColumnCount()); for (int row = 0; row < log.getRowCount(); row++) { for (int col = 0; col < log.getColumnCount(); col++) { double d = input.get(row, col); log.set(row, col, guardedLogarithm(d)); } } return log; }
@Override public DoubleMatrix divide(DoubleMatrix other) { DoubleMatrix m = new DenseDoubleMatrix(this.numRows, this.numColumns); for (int i = 0; i < numRows; i++) { for (int j = 0; j < numColumns; j++) { m.set(i, j, this.matrix[translate(i, j, numRows)] / other.get(i, j)); } } return m; }
@Override public DoubleMatrix subtract(DoubleMatrix other) { SparseDoubleRowMatrix result = new SparseDoubleRowMatrix( other.getRowCount(), other.getColumnCount()); for (int row : this.matrix.keys()) { Iterator<DoubleVectorElement> iterate = matrix.get(row).iterate(); while (iterate.hasNext()) { DoubleVectorElement e = iterate.next(); result.set(row, e.getIndex(), e.getValue() - other.get(row, e.getIndex())); } } return result; }
@Override public DoubleMatrix subtract(DoubleMatrix other) { DoubleMatrix m = new DenseDoubleMatrix(this.numRows, this.numColumns); for (int i = 0; i < numRows; i++) { for (int j = 0; j < numColumns; j++) { m.set(i, j, this.matrix[translate(i, j, numRows)] - other.get(i, j)); } } return m; }
@Override public DoubleMatrix add(DoubleMatrix other) { SparseDoubleRowMatrix result = new SparseDoubleRowMatrix( other.getRowCount(), other.getColumnCount()); for (int row : this.matrix.keys()) { Iterator<DoubleVectorElement> iterate = matrix.get(row).iterate(); while (iterate.hasNext()) { DoubleVectorElement e = iterate.next(); result.set(row, e.getIndex(), e.getValue() + other.get(row, e.getIndex())); } } return result; }
@Override public DoubleMatrix add(DoubleMatrix other) { DoubleMatrix m = new DenseDoubleMatrix(this.numRows, this.numColumns); for (int i = 0; i < numRows; i++) { for (int j = 0; j < numColumns; j++) { m.set(i, j, this.matrix[translate(i, j, numRows)] + other.get(i, j)); } } return m; }
/** * @return the transition probabilities for the states. */ public DoubleVector getTransitionProbabilities(int[] stateSequence) { DenseDoubleVector distribution = new DenseDoubleVector( stateSequence.length - 1); for (int i = 0; i < distribution.getDimension(); i++) { distribution.set(i, transitionProbabilities.get(stateSequence[i], stateSequence[i + 1])); } return distribution; }
private void observe(DoubleVector document, DoubleVector outcome, int numDistinctClasses, int[] tokenPerClass, int[] numDocumentsPerClass) { int predictedClass = outcome.maxIndex(); if (numDistinctClasses == 2) { predictedClass = (int) outcome.get(0); } tokenPerClass[predictedClass] += document.getLength(); numDocumentsPerClass[predictedClass]++; Iterator<DoubleVectorElement> iterateNonZero = document.iterateNonZero(); while (iterateNonZero.hasNext()) { DoubleVectorElement next = iterateNonZero.next(); double currentCount = probabilityMatrix.get(predictedClass, next.getIndex()); probabilityMatrix.set(predictedClass, next.getIndex(), currentCount + next.getValue()); } }
@Override public DoubleMatrix multiplyElementWise(DoubleMatrix other) { DoubleMatrix result = new SparseDoubleRowMatrix(this.getRowCount(), this.getColumnCount()); for (int row : this.matrix.keys()) { Iterator<DoubleVectorElement> iterateNonZero = matrix.get(row) .iterateNonZero(); while (iterateNonZero.hasNext()) { DoubleVectorElement e = iterateNonZero.next(); result.set(row, e.getIndex(), get(row, e.getIndex()) * other.get(row, e.getIndex())); } } return result; }