Package de.jungblut.math

Examples of de.jungblut.math.DoubleMatrix


public final class LogisticErrorFunction implements ErrorFunction {

  @Override
  public double calculateError(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();
  }
View Full Code Here


  @Override
  protected CostGradientTuple evaluateBatch(DoubleVector input,
      DoubleMatrix data, DoubleMatrix outcomeBatch) {
    // input contains the weights between the visible and the hidden units
    DoubleMatrix theta = DenseMatrixFolder.unfoldMatrices(input,
        unfoldParameters)[0].transpose();

    /*
     * POSITIVE PHASE
     */
    DoubleMatrix positiveHiddenProbs = activationFunction.apply(multiply(data,
        theta, false, false));
    // set out hidden bias back to 1
    positiveHiddenProbs.setColumnVector(0,
        DenseDoubleVector.ones(positiveHiddenProbs.getRowCount()));
    DoubleMatrix positiveAssociations = multiply(data, positiveHiddenProbs,
        true, false);
    /*
     * END OF POSITIVE PHASE
     */
    binarize(random, positiveHiddenProbs);
    /*
     * START NEGATIVE PHASE
     */
    DoubleMatrix negativeData = activationFunction.apply(multiply(
        positiveHiddenProbs, theta, false, true));
    negativeData.setColumnVector(0,
        DenseDoubleVector.ones(negativeData.getRowCount()));
    DoubleMatrix negativeHiddenProbs = activationFunction.apply(multiply(
        negativeData, theta, false, false));
    negativeHiddenProbs.setColumnVector(0,
        DenseDoubleVector.ones(negativeHiddenProbs.getRowCount()));
    DoubleMatrix negativeAssociations = multiply(negativeData,
        negativeHiddenProbs, true, false);
    /*
     * END OF NEGATIVE PHASE
     */

    // measure a very simple reconstruction error
    double j = data.subtract(negativeData).pow(2).sum();

    // calculate the approx. gradient
    DoubleMatrix thetaGradient = positiveAssociations.subtract(
        negativeAssociations).divide(data.getRowCount());

    // calculate the weight decay and apply it
    if (lambda != 0d) {
      DoubleVector bias = thetaGradient.getColumnVector(0);
      thetaGradient = thetaGradient.subtract(thetaGradient.multiply(lambda
          / data.getRowCount()));
      thetaGradient.setColumnVector(0, bias);
    }

    // transpose the gradient and negate it, because we transposed theta at the
    // top and our gradient descent subtracts instead of addition.
    return new CostGradientTuple(j,
        DenseMatrixFolder.foldMatrices((DenseDoubleMatrix) thetaGradient
            .multiply(-1).transpose()));
  }
View Full Code Here

    for (int i = 0; i < numHiddenStates; i++) {
      hiddenPriorProbability.set(i, random.nextDouble());
    }
    normalizeProbabilities();

    DoubleMatrix alpha = new DenseDoubleMatrix(features.length, numHiddenStates);
    DoubleMatrix beta = new DenseDoubleMatrix(features.length, numHiddenStates);

    for (int iteration = 0; iteration < maxIterations; iteration++) {
      // in every iteration we initialize a new model that is a copy of the old
      DoubleMatrix transitionProbabilityMatrix = this.transitionProbabilityMatrix
          .deepCopy();
      DoubleMatrix emissionProbabilityMatrix = this.emissionProbabilityMatrix
          .deepCopy();
      DoubleVector hiddenPriorProbability = this.hiddenPriorProbability
          .deepCopy();

      // expectation step
      alpha = forward(alpha, transitionProbabilityMatrix,
          emissionProbabilityMatrix, hiddenPriorProbability, features);
      beta = backward(beta, transitionProbabilityMatrix,
          emissionProbabilityMatrix, hiddenPriorProbability, features);

      // now do the real baum-welch algorithm / maximization step calculate the
      // prior out of the alpha and beta factors in their first row
      hiddenPriorProbability = alpha.getRowVector(0).multiply(
          beta.getRowVector(0));
      final double modelLikelihood = estimateLikelihood(alpha);

      // compute real transition probabilities
      for (int i = 0; i < numHiddenStates; i++) {
        for (int j = 0; j < numHiddenStates; j++) {
          double temp = 0d;
          for (int t = 0; t < features.length - 1; t++) {
            Iterator<DoubleVectorElement> iterateNonZero = features[t + 1]
                .iterateNonZero();
            while (iterateNonZero.hasNext()) {
              temp += alpha.get(t, i)
                  * emissionProbabilityMatrix.get(j, iterateNonZero.next()
                      .getIndex()) * beta.get(t + 1, j);
            }
          }
          transitionProbabilityMatrix.set(i, j,
              transitionProbabilityMatrix.get(i, j) * temp / modelLikelihood);
        }
      }
      // compute real emission probabilities
      for (int i = 0; i < numHiddenStates; i++) {
        for (int j = 0; j < numVisibleStates; j++) {
          double temp = 0d;
          for (int t = 0; t < features.length; t++) {
            Iterator<DoubleVectorElement> iterateNonZero = features[t]
                .iterateNonZero();
            while (iterateNonZero.hasNext()) {
              DoubleVectorElement next = iterateNonZero.next();
              if (next.getIndex() == j) {
                temp += alpha.get(t, i) * beta.get(t, i);
              }
            }
          }
          emissionProbabilityMatrix.set(i, j, temp / modelLikelihood);
        }
      }

      // normalize again after baumwelch pass
      normalize(hiddenPriorProbability, transitionProbabilityMatrix,
View Full Code Here

    return subtract.divide(subtract.sum());
  }

  @Override
  public DoubleMatrix apply(DoubleMatrix matrix) {
    DoubleMatrix dm = newInstance(matrix);
    for (int row = 0; row < matrix.getRowCount(); row++) {
      DoubleVector apply = apply(matrix.getRowVector(row));
      if (apply.getLength() != 0) {
        dm.setRowVector(row, apply);
      }
    }
    return dm;
  }
View Full Code Here

    for (int l : model.layers) {
      out.writeInt(l);
    }
    // write the weight matrices
    for (WeightMatrix mat : model.weights) {
      DoubleMatrix weights = mat.getWeights();
      new MatrixWritable(weights).write(out);
    }
    // then write the activation classes
    for (ActivationFunction func : model.activations) {
      out.writeUTF(func.getClass().getName());
View Full Code Here

   * @param epsilon the radius of a point to detect other points.
   */
  public ArrayList<DoubleVector>[] cluster(List<DoubleVector> points,
      DistanceMeasurer measurer, int minPoints, double epsilon) {
    // compute the distance matrix
    DoubleMatrix distanceMatrix = generateDistanceMatrix(measurer, points);
    // generate adjacency list
    TIntObjectHashMap<int[]> adjacencyMatrix = generateAdjacencyMatrix(
        distanceMatrix, points, minPoints, epsilon);
    connectedComponents = findConnectedComponents(points, adjacencyMatrix);
    noise = findNoise(points);
View Full Code Here

        false);

    int[][] pms = MultilayerPerceptronCostFunction
        .computeUnfoldParameters(new int[] { test[0].getDimension(),
            hiddenUnits });
    DoubleMatrix thetaMat = DenseMatrixFolder.unfoldMatrices(theta, pms)[0]
        .transpose();
    double[][] result = new double[][] {
        { 0.3768283706784331, -0.429785280688955 },
        { -0.019007571880728952, 2.681789402615304 },
        { 1.853893280384037, 0.8872141826811614 },
        { -0.6779189212594092, -2.233814531158892 },
        { 3.768227750561635, -1.861672501324946 },
        { -1.7606548237507884, 2.5544868606627005 },
        { -0.9148771784733722, -1.9820382601667268 },
        { 3.936150254656125, 1.2253565233931112 } };

    assertEquals(0, thetaMat.subtract(new DenseDoubleMatrix(result)).sum(),
        1e-4);

  }
View Full Code Here

  public void internalChecks(MultinomialNaiveBayes classifier) {
    DoubleVector classProbability = classifier.getClassProbability();
    assertEquals(FastMath.log(2d / 5d), classProbability.get(0), 0.01d);
    assertEquals(FastMath.log(3d / 5d), classProbability.get(1), 0.01d);

    DoubleMatrix mat = classifier.getProbabilityMatrix();
    double[] realFirstRow = new double[] { 0.0, 0.0, -2.1972245773362196,
        -1.5040773967762742, -1.5040773967762742 };
    double[] realSecondRow = new double[] { -0.9808292530117262,
        -2.0794415416798357, 0.0, 0.0, 0.0 };

    double[] firstRow = mat.getRowVector(0).toArray();
    assertEquals(realFirstRow.length, firstRow.length);
    for (int i = 0; i < firstRow.length; i++) {
      assertEquals("" + Arrays.toString(firstRow), realFirstRow[i],
          firstRow[i], 0.05d);
    }

    double[] secondRow = mat.getRowVector(1).toArray();
    assertEquals(realSecondRow.length, secondRow.length);
    for (int i = 0; i < firstRow.length; i++) {
      assertEquals("" + Arrays.toString(secondRow), realSecondRow[i],
          secondRow[i], 0.05d);
    }
View Full Code Here

public class WeightMatrixTest {

  @Test
  public void testInit() {
    MultilayerPerceptron.SEED = 0l;
    DoubleMatrix mat = new WeightMatrix(2, 3).getWeights();

    assertEquals(0.532915792500396, mat.get(0, 0), 1e-4);
    assertEquals(0.05996064284821445, mat.get(0, 1), 1e-4);
    assertEquals(0.10130511193101194, mat.get(0, 2), 1e-4);

    assertEquals(0.20115981409625333, mat.get(1, 0), 1e-4);
    assertEquals(0.7585999616576491, mat.get(1, 1), 1e-4);
    assertEquals(0.07173419648944401, mat.get(1, 2), 1e-4);

    assertEquals(-0.3114541695609426, mat.get(2, 0), 1e-4);
    assertEquals(-0.6921393707523671, mat.get(2, 1), 1e-4);
    assertEquals(-0.7019011525722139, mat.get(2, 2), 1e-4);
  }
View Full Code Here

public class SquaredMeanErrorFunctionTest {

  @Test
  public void testSmeError() {
    DoubleMatrix y = new DenseDoubleMatrix(new double[] { 0d, 1d, 0d, 1d, 0d },
        1, 5);
    DoubleMatrix hypothesis = new DenseDoubleMatrix(new double[] { 0d, 0d, 0d,
        1d, 0d }, 1, 5);
    double error = new SquaredMeanErrorFunction().calculateError(y, hypothesis);
    assertEquals(1d, error, 1e-4);
  }
View Full Code Here

TOP

Related Classes of de.jungblut.math.DoubleMatrix

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.