Package de.jungblut.math

Examples of de.jungblut.math.DoubleVector


  }

  @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.calculateError(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);
View Full Code Here


      final int maxIterations, boolean verbose) {

    double[] lastCosts = new double[COST_HISTORY];
    Arrays.fill(lastCosts, Double.MAX_VALUE);
    final int lastIndex = lastCosts.length - 1;
    DoubleVector lastTheta = null;
    DoubleVector lastGradient = null;
    DoubleVector theta = pInput;
    double alpha = this.alpha;
    for (int iteration = 0; iteration < maxIterations; iteration++) {
      CostGradientTuple evaluateCost = f.evaluateCost(theta);
      if (verbose) {
        LOG.info("Iteration " + iteration + " | Cost: "
            + evaluateCost.getCost());
      }
      shiftLeft(lastCosts);
      lastCosts[lastIndex] = evaluateCost.getCost();
      // break if we converged below the limit
      if (converged(lastCosts, breakDifference)) {
        break;
      }
      // break if we are going in the wrong direction
      if (breakOnDivergence && ascending(lastCosts)) {
        break;
      }

      DoubleVector gradient = evaluateCost.getGradient();
      // check the bold driver
      if (boldDriver) {
        if (lastGradient != null) {
          double costDifference = getCostDifference(lastCosts);
          if (costDifference < 0) {
            // we can increase, because cost decreased
            alpha += (alpha * boldDecreasePercentage);
          } else {
            // we decrease, because cost increased
            // we undo the last theta change
            theta = lastTheta;
            gradient = lastGradient;
            alpha -= (alpha * boldIncreasePercentage);
          }
          if (verbose) {
            LOG.info("Iteration " + iteration + " | Alpha: " + alpha + "\n");
          }
        }
        lastGradient = gradient;
      }
      // check annealing
      if (annealingIteration > 0) {
        // always pick the initial learning rate
        alpha = this.alpha / (1d + iteration / annealingIteration);
      }
      // save our last parameter
      lastTheta = theta;
      // basically subtract the gradient multiplied with the learning rate
      theta = theta.subtract(gradient.multiply(alpha));
      if (lastTheta != null && momentum != 0d) {
        // we add momentum as the parameter "m" multiplied by the difference of
        // both theta vectors
        theta = theta.add((lastTheta.subtract(theta)).multiply(momentum));
      }
View Full Code Here

   */
  public static MultinomialNaiveBayes deserialize(DataInput in)
      throws IOException {
    MatrixWritable matrixWritable = new MatrixWritable();
    matrixWritable.readFields(in);
    DoubleVector classProbability = VectorWritable.readVector(in);

    return new MultinomialNaiveBayes(matrixWritable.getMatrix(),
        classProbability);
  }
View Full Code Here

      }
      // add the bias to hidden and visible layer, random init with 0.1*randn
      DenseDoubleMatrix start = new DenseDoubleMatrix(layerSizes[i] + 1,
          currentTrainingSet[0].getDimension() + 1, new Random(seed))
          .multiply(0.1d);
      DoubleVector folded = DenseMatrixFolder.foldMatrices(start);
      start = null;
      // now do the real training
      RBMCostFunction fnc = new RBMCostFunction(currentTrainingSet,
          miniBatchSize, batchParallelism, layerSizes[i], activationFunction,
          type, lambda, seed, stochastic);
      DoubleVector theta = minimizer.minimize(fnc, folded, numIterations,
          verbose);
      // get back our weights as a matrix
      DoubleMatrix thetaMat = DenseMatrixFolder.unfoldMatrices(theta,
          fnc.getUnfoldParameters())[0];
      weights[i] = thetaMat;
View Full Code Here

   * @param input the input of the first RBM.
   * @return a vector that contains the values of the hidden activations on the
   *         last layer.
   */
  public DoubleVector predict(DoubleVector input) {
    DoubleVector lastOutput = input;
    for (int i = 0; i < layerSizes.length; i++) {
      lastOutput = computeHiddenActivations(lastOutput, weights[i]);
    }
    // slice the hidden bias away
    return lastOutput.slice(1, lastOutput.getDimension());
  }
View Full Code Here

    // check the binary case to calculate special metrics
    if (result.isBinary()) {
      List<PredictionOutcomePair> outcomePredictedPairs = new ArrayList<>();
      for (int i = 0; i < testFeatures.length; i++) {
        int outcomeClass = ((int) testOutcome[i].get(0));
        DoubleVector predictedVector = classifier.predict(testFeatures[i]);
        outcomePredictedPairs.add(PredictionOutcomePair.from(outcomeClass,
            predictedVector.get(0)));
        int prediction = 0;
        if (threshold == null) {
          prediction = classifier.extractPredictedClass(predictedVector);
        } else {
          prediction = classifier.extractPredictedClass(predictedVector,
View Full Code Here

   *
   * @param hiddenActivations the activations of the predict method.
   * @return the reconstructed input vector.
   */
  public DoubleVector reconstructInput(DoubleVector hiddenActivations) {
    DoubleVector lastOutput = hiddenActivations;
    for (int i = weights.length - 1; i >= 0; i--) {
      lastOutput = computeHiddenActivations(lastOutput, weights[i].transpose());
    }
    // slice the hidden bias away
    return lastOutput.slice(1, lastOutput.getDimension());
  }
View Full Code Here

  }

  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));
  }
View Full Code Here

      }
    }

    final int[] rowEntries = transitionProbabilities.rowIndices();
    for (int rowIndex : rowEntries) {
      DoubleVector rowVector = transitionProbabilities.getRowVector(rowIndex);
      double sum = rowVector.sum();
      Iterator<DoubleVectorElement> iterateNonZero = rowVector.iterateNonZero();
      // loop over all counts and take the log of the probability
      while (iterateNonZero.hasNext()) {
        DoubleVectorElement columnElement = iterateNonZero.next();
        int columnIndex = columnElement.getIndex();
        double probability = FastMath.log(columnElement.getValue())
View Full Code Here

   *
   * @return value between 0d and 1d, where 1d is very likely that the sequence
   *         is happening.
   */
  public double getProbabilityForSequence(int[] stateSequence) {
    DoubleVector distribution = getTransitionProbabilities(stateSequence);
    // normalize it by the maximum of the log probabilities
    double max = distribution.max();
    double probabilitySum = 0.0d;
    for (int i = 0; i < distribution.getDimension(); i++) {
      double probability = distribution.get(i);
      double normalizedProbability = probability - max;
      // add up the log probabilities
      probabilitySum += normalizedProbability;
    }
    // no we can exp them to get the real probability
View Full Code Here

TOP

Related Classes of de.jungblut.math.DoubleVector

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.