/** * @param x normal feature matrix, column 0 should contain the bias. * @param y normal outcome matrix, for multiple classes use the one-hot * encoding. This matrix should be transposed. * @param lambda l1 reg parameter. */ public LogisticRegressionCostFunction(DoubleMatrix x, DoubleMatrix y, double lambda) { this.x = x; this.lambda = lambda; this.m = x.getRowCount(); this.xTransposed = this.x.transpose(); this.y = y; }
@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(); }
private static double estimateLikelihood(DoubleMatrix alpha) { // sum the last row in our alpha matrix generated by the forward algorithm, // this denotes the endstate of our sequence. return alpha.getRowVector(alpha.getRowCount() - 1).sum(); }
@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(); }
public ConditionalLikelihoodCostFunction(DoubleMatrix features, DoubleMatrix outcome) { this.features = features; this.outcome = outcome; this.m = outcome.getRowCount(); this.classes = outcome.getColumnCount() == 1 ? 2 : outcome.getColumnCount(); }
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 double calculateLoss(DoubleMatrix y, DoubleMatrix hypothesis) { return y.subtract(hypothesis).sum() / y.getRowCount(); }
protected DoubleMatrix newInstance(DoubleMatrix mat) { if (mat.isSparse()) { return new SparseDoubleRowMatrix(mat.getRowCount(), mat.getColumnCount()); } else { return new DenseDoubleMatrix(mat.getRowCount(), mat.getColumnCount()); } }
/** * 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; }
@Override public double calculateLoss(DoubleMatrix y, DoubleMatrix hypothesis) { return y.multiplyElementWise(MathUtils.logMatrix(hypothesis)).sum() / y.getRowCount(); }
/** * Row-copies the given matrix to this sparse implementation. * * @param mat the matrix to copy. */ public SparseDoubleRowMatrix(DoubleMatrix mat) { this(mat.getRowCount(), mat.getColumnCount()); for (int i = 0; i < numColumns; i++) { setRowVector(i, mat.getRowVector(i)); } }
/** * Sets the weights in the whole matrix uniformly between -eInit and eInit * (eInit is the standard deviation) with zero mean. */ private void setWeightsUniformly(RandomDataImpl rnd, double eInit) { for (int i = 0; i < weights.getColumnCount(); i++) { for (int j = 0; j < weights.getRowCount(); j++) { weights.set(j, i, rnd.nextUniform(-eInit, eInit)); } } }
public static double calculateRegularization(DoubleMatrix[] thetas, final int m, NetworkConfiguration conf) { double regularization = 0d; // only calculate the regularization term if lambda is not 0 if (conf.lambda != 0d) { for (DoubleMatrix theta : thetas) { regularization += (theta.slice(0, theta.getRowCount(), 1, theta.getColumnCount())).pow(2).sum(); } regularization = (conf.lambda / (2.0d * m)) * regularization; } return regularization; }
/** * Creates a new matrix with the given vector into the first column and the * other matrix to the other columns. This is usually used in machine learning * algorithms that add a bias on the zero-index column. * * @param first the new first column. * @param otherMatrix the other matrix to set on from the second column. */ public SparseDoubleRowMatrix(DenseDoubleVector first, DoubleMatrix otherMatrix) { this(otherMatrix.getRowCount(), otherMatrix.getColumnCount() + 1); setColumnVector(0, first); for (int i = 1; i < numColumns; i++) { setColumnVector(i, otherMatrix.getColumnVector(i - 1)); } }
@Override public DoubleMatrix apply(DoubleMatrix matrix) { DoubleMatrix dm = newInstance(matrix); for (int row = 0; row < matrix.getRowCount(); row++) { DoubleVector apply = apply(matrix.getRowVector(row)); if (apply.getLength() != 0) { dm.setRowVector(row, apply); } } return dm; }
@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 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; }
/** * @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 double calculateLoss(DoubleMatrix y, DoubleMatrix hypothesis) { DoubleMatrix negativeOutcome = y.subtractBy(1.0d); DoubleMatrix inverseOutcome = y.multiply(-1d); DoubleMatrix negativeHypo = hypothesis.subtractBy(1d); DoubleMatrix negativeLogHypo = MathUtils.logMatrix(negativeHypo); DoubleMatrix positiveLogHypo = MathUtils.logMatrix(hypothesis); DoubleMatrix negativePenalty = negativeOutcome .multiplyElementWise(negativeLogHypo); DoubleMatrix positivePenalty = inverseOutcome .multiplyElementWise(positiveLogHypo); return (positivePenalty.subtract(negativePenalty)).sum() / y.getRowCount(); }