@Override public double calculateLoss(DoubleMatrix y, DoubleMatrix hypothesis) { return y.multiplyElementWise(MathUtils.logMatrix(hypothesis)).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(); }
@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(); }
public static DoubleMatrix[] backwardPropagate(DoubleMatrix y, DoubleMatrix[] thetas, DoubleMatrix[] ax, DoubleMatrix[] zx, NetworkConfiguration conf) { // now backpropagate the error backwards by calculating the deltas. // also here we are following the math equations and nulling out the 0th // entry. DoubleMatrix[] deltaX = new DoubleMatrix[conf.layerSizes.length]; // set the last delta to the difference of outcome and prediction deltaX[deltaX.length - 1] = ax[conf.layerSizes.length - 1].subtract(y); // compute the deltas onto the input layer for (int i = (conf.layerSizes.length - 2); i > 0; i--) { DoubleMatrix slice = thetas[i].slice(0, thetas[i].getRowCount(), 1, thetas[i].getColumnCount()); deltaX[i] = multiply(deltaX[i + 1], slice, false, false, conf); // apply the gradient of the activations deltaX[i] = deltaX[i].multiplyElementWise(conf.activations[i] .gradient(zx[i])); } return deltaX; }