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(DoubleVector y, DoubleVector hypothesis) { DoubleVector negativeOutcome = y.subtractFrom(1.0d); DoubleVector inverseOutcome = y.multiply(-1d); DoubleVector negativeHypo = hypothesis.subtractFrom(1d); DoubleVector negativeLogHypo = MathUtils.logVector(negativeHypo); DoubleVector positiveLogHypo = MathUtils.logVector(hypothesis); DoubleVector negativePenalty = negativeOutcome.multiply(negativeLogHypo); DoubleVector positivePenalty = inverseOutcome.multiply(positiveLogHypo); return (positivePenalty.subtract(negativePenalty)).sum(); }
@Override public double calculateLoss(DoubleMatrix y, DoubleMatrix hypothesis) { return y.multiplyElementWise(MathUtils.logMatrix(hypothesis)).sum() / y.getRowCount(); }
/** * @return a log'd matrix that was guarded against edge cases of the * logarithm. */ public static DoubleVector logVector(DoubleVector input) { DenseDoubleVector log = new DenseDoubleVector(input.getDimension()); for (int col = 0; col < log.getDimension(); col++) { log.set(col, guardedLogarithm(input.get(col))); } return log; }
private void addMultInto(DoubleVector a, DoubleVector b, DoubleVector c, double d) { for (int i = 0; i < a.getDimension(); i++) { a.set(i, b.get(i) + c.get(i) * d); } }
@Override public DoubleMatrix divide(DoubleVector vec) { DoubleMatrix cop = new DenseDoubleMatrix(this.getRowCount(), this.getColumnCount()); for (int i = 0; i < this.getColumnCount(); i++) { cop.setColumnVector(i, getColumnVector(i).divide(vec)); } return cop; }
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 slice(int rowOffset, int rowMax, int colOffset, int colMax) { DoubleMatrix m = new SparseDoubleRowMatrix(rowMax - rowOffset, colMax - colOffset); for (int col : columnIndices()) { DoubleVector columnVector = getColumnVector(col); columnVector = columnVector.slice(rowOffset, rowMax); m.setColumnVector(col - colOffset, columnVector); } return m; }
@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(); }
@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(DoubleVector y, DoubleVector hypothesis) { double sum = 0d; for (int col = 0; col < y.getDimension(); col++) { double diff = y.get(col) - hypothesis.get(col); sum += (diff * diff); } return sum; }
/** * Initialize the node with a single vector, mainly used for initializing * the bottom. */ ClusterNode(DoubleVector mean) { this.mean = mean.deepCopy(); this.splitDistance = 0d; }
/** * Normalizes the given dataset (inplace), by subtracting the mean and * dividing by the stddev. Dataset will have 0 mean and stddev of 1. * * @return a tuple of the mean and the stddev. */ public static Tuple<DoubleVector, DoubleVector> meanNormalizeColumns( Dataset dataset) { return meanNormalizeColumns(dataset, (x) -> true); }
private void fixDirectionSigns() { if (l1weight > 0) { for (int i = 0; i < dir.getDimension(); i++) { if (dir.get(i) * steepestDescDir.get(i) <= 0) { dir.set(i, 0); } } } }
private void scale(DoubleVector a, double b) { for (int i = 0; i < a.getDimension(); i++) { a.set(i, a.get(i) * b); } }