private DoubleVector computeHiddenActivations(DoubleVector input, DoubleMatrix theta) { // add the bias to the input DoubleVector biased = new DenseDoubleVector(1d, input.toArray()); return activationFunction.apply(theta.multiplyVectorRow(biased)); }
/** * Predicts the outcome of the given input by doing a forward pass. */ @Override public DoubleVector predict(DoubleVector xi) { DoubleVector activationVector = addBias(xi); final int len = layers.length - 1; for (int i = 1; i <= len; i++) { activationVector = activations[i].apply(weights[i - 1].getWeights() .multiplyVectorRow(activationVector)); // only add the bias if we are not in the last layer if (i != len) { activationVector = addBias(activationVector); } } return activationVector; }
public DoubleVector predict(DoubleVector features, DoubleVector previousOutcome) { // clamp the features to the visible units, calculate the joint // probability for each hidden state and put it into the vector DoubleVector probabilities = emissionProbabilityMatrix .multiplyVectorRow(features); // we can add here, both are logarithms probabilities.add(transitionProbabilityMatrix .multiplyVectorRow(previousOutcome)); double max = probabilities.max(); for (int state = 0; state < probabilities.getDimension(); state++) { probabilities.set(state, FastMath.exp(probabilities.get(state) - max) * hiddenPriorProbability.get(state)); } // normalize again return probabilities.divide(probabilities.sum()); }
@Override public CostGradientTuple evaluateCost(DoubleVector theta) { DoubleVector activation = SIGMOID.get().apply(x.multiplyVectorRow(theta)); DenseDoubleMatrix hypo = new DenseDoubleMatrix(Arrays.asList(activation)); double error = ERROR_FUNCTION.calculateLoss(y, hypo); DoubleMatrix loss = hypo.subtract(y); double j = error / m; DoubleVector gradient = xTransposed.multiplyVectorRow(loss.getRowVector(0)) .divide(m); if (lambda != 0d) { DoubleVector reg = theta.multiply(lambda / m); // don't regularize the bias reg.set(0, 0d); gradient = gradient.add(reg); j += lambda * theta.pow(2).sum() / m; } return new CostGradientTuple(j, gradient); } }
@Override public DoubleVector predict(DoubleVector features) { // clamp the features to the visible units, calculate the joint // probability for each hidden state and put it into the vector DoubleVector probabilities = emissionProbabilityMatrix .multiplyVectorRow(features); double max = probabilities.max(); for (int state = 0; state < probabilities.getDimension(); state++) { probabilities.set(state, FastMath.exp(probabilities.get(state) - max) * hiddenPriorProbability.get(state)); } // normalize again return probabilities.divide(probabilities.sum()); }