/** * Predicts the outcome of the given input by doing a forward pass. Used for * binary classification by a threshold. Everything above threshold will be * considered as 1, the other case as 0. */ public DoubleVector predict(DoubleVector xi, double threshold) { DoubleVector activations = predict(xi); for (int i = 0; i < activations.getLength(); i++) { activations.set(i, activations.get(i) > threshold ? 1.0d : 0.0d); } return activations; }
@Override public DoubleVector calculateGradient(DoubleVector feature, DoubleVector y, DoubleVector hypothesis) { return feature.multiply(hypothesis.subtract(y).get(0)); } }
@Override public double calculateLoss(DoubleVector y, DoubleVector hypothesis) { DoubleVector v = y.multiply(hypothesis); return FastMath.max(0, 1 - v.get(0)); }
private void scale(DoubleVector a, double b) { for (int i = 0; i < a.getDimension(); i++) { a.set(i, a.get(i) * b); } }
static boolean strictLower(DoubleVector upper, DoubleVector current) { Iterator<DoubleVectorElement> iterateNonZero = upper.iterateNonZero(); while (iterateNonZero.hasNext()) { DoubleVectorElement next = iterateNonZero.next(); if (current.get(next.getIndex()) > next.getValue()) return false; } return true; }
static DoubleVector binarize(Random r, DoubleVector v) { for (int j = 0; j < v.getDimension(); j++) { v.set(j, v.get(j) > r.nextDouble() ? 1d : 0d); } return v; }
@Override public DoubleVector calculateGradient(DoubleVector feature, DoubleVector y, DoubleVector hypothesis) { return feature.multiply(hypothesis.subtract(y).get(0)); }
@Override public DoubleVector subtract(DoubleVector other) { DoubleVector result = new SparseDoubleVector(this.vector.fastDeepCopy(), this.getDimension()); Iterator<DoubleVectorElement> iter = other.iterateNonZero(); while (iter.hasNext()) { DoubleVectorElement e = iter.next(); int index = e.getIndex(); result.set(index, result.get(index) - e.getValue()); } return result; }
/** * @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; }
@Override public DoubleVector divide(DoubleVector vector) { DoubleVector v = new SparseDoubleVector(this.dimension, this.vector.size()); Iterator<DoubleVectorElement> it = iterateNonZero(); while (it.hasNext()) { DoubleVectorElement e = it.next(); v.set(e.getIndex(), e.getValue() / vector.get(e.getIndex())); } return v; }
@Override public DoubleVector multiplyVectorColumn(DoubleVector v) { DoubleVector vector = new DenseDoubleVector(this.getColumnCount()); for (int col = 0; col < numColumns; col++) { double sum = 0.0d; for (int row = 0; row < numRows; row++) { sum += (matrix[translate(row, col, numRows)] * v.get(row)); } vector.set(col, sum); } return vector; }
@Override public DoubleVector add(DoubleVector other) { DoubleVector result = new SparseDoubleVector(this.vector.fastDeepCopy(), this.getDimension()); Iterator<DoubleVectorElement> iter = other.iterateNonZero(); while (iter.hasNext()) { DoubleVectorElement e = iter.next(); int index = e.getIndex(); result.set(index, result.get(index) + e.getValue()); } return result; }
@Override public DoubleVector divide(DoubleVector vector) { DoubleVector v = new SparseBitVector(getDimension()); Iterator<DoubleVectorElement> it = iterateNonZero(); while (it.hasNext()) { DoubleVectorElement e = it.next(); v.set(e.getIndex(), e.getValue() / vector.get(e.getIndex())); } return v; }
@Override public DoubleVector apply(DoubleVector other, DoubleDoubleVectorFunction func) { SequentialSparseDoubleVector newV = new SequentialSparseDoubleVector(this); Iterator<DoubleVectorElement> iterate = iterate(); while (iterate.hasNext()) { DoubleVectorElement next = iterate.next(); double res = func.calculate(next.getIndex(), next.getValue(), other.get(next.getIndex())); newV.set(next.getIndex(), res); } return newV; }
@Override public DoubleMatrix subtract(DoubleVector vec) { DoubleMatrix result = new SparseDoubleRowMatrix(this.getRowCount(), this.getColumnCount()); for (int row : this.matrix.keys()) { SparseDoubleVector rowVec = matrix.get(row); result.setRowVector(row, rowVec.subtract(vec.get(row))); } return result; }