Package de.jungblut.math.dense

Examples of de.jungblut.math.dense.DenseDoubleMatrix


    }
  }

  @Test
  public void testPolynomials() {
    DenseDoubleMatrix mat = new DenseDoubleMatrix(new double[][] { { 2, 5 },
        { 5, 1 }, { 7, 25 } });
    DenseDoubleMatrix expected = new DenseDoubleMatrix(new double[][] {
        { 2, 4, 5, 25 }, { 5, 25, 1, 1 }, { 7, 49, 25, 625 } });
    DenseDoubleMatrix polys = MathUtils.createPolynomials(mat, 2);
    assertEquals(polys.subtract(expected).sum(), 0, 1E-5);
    assertEquals(mat, MathUtils.createPolynomials(mat, 1));
  }
View Full Code Here


  @Test
  public void testDimensionalityNoTranspose() {

    MatrixDimension dimension = new MatrixDimension(
        new DenseDoubleMatrix(2, 3), new DenseDoubleMatrix(15, 2));

    assertEquals(2, dimension.getM());
    assertEquals(2, dimension.getN());
    assertEquals(3, dimension.getK());
View Full Code Here

  @Test
  public void testDimensionality() {

    MatrixDimension dimension = new MatrixDimension(
        new DenseDoubleMatrix(3, 2), new DenseDoubleMatrix(15, 2), true, false);

    assertEquals(2, dimension.getM());
    assertEquals(2, dimension.getN());
    assertEquals(3, dimension.getK());

    assertEquals(3, dimension.getLdA());
    assertEquals(15, dimension.getLdB());
    assertEquals(2, dimension.getLdC());

    assertEquals(true, dimension.isTransposeA());
    assertEquals(false, dimension.isTransposeB());

    dimension = new MatrixDimension(new DenseDoubleMatrix(3, 2),
        new DenseDoubleMatrix(2, 15), true, true);
    assertEquals(2, dimension.getM());
    assertEquals(2, dimension.getN());
    assertEquals(15, dimension.getK());

    assertEquals(3, dimension.getLdA());
    assertEquals(2, dimension.getLdB());
    assertEquals(2, dimension.getLdC());

    assertEquals(true, dimension.isTransposeA());
    assertEquals(true, dimension.isTransposeB());

    assertEquals(
        "MatrixDimension [m=2, n=2, k=15, ldA=3, ldB=2, ldC=2, transposeA=true, transposeB=true]",
        dimension.toString());

    dimension = new MatrixDimension(new DenseDoubleMatrix(3, 2),
        new DenseDoubleMatrix(2, 15), false, true);
    assertEquals(3, dimension.getM());
    assertEquals(2, dimension.getN());
    assertEquals(2, dimension.getK());

    assertEquals(3, dimension.getLdA());
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

public class LogisticErrorFunctionTest {

  @Test
  public void testSigmoidErrorMatrix() {
    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 LogisticErrorFunction().calculateError(y, hypothesis);
    assertEquals(10d, error, 1e-4);
  }
View Full Code Here

import de.jungblut.math.dense.DenseDoubleMatrix;

public class CrossEntropyErrorFunctionTest {
  @Test
  public void testSoftmaxError() {
    DoubleMatrix y = new DenseDoubleMatrix(new double[][] {
        { 0d, 1d, 0.5d, 1d, 0.2d }, { 1d, 0d, 0.5d, 0d, 0.8d } });
    DoubleMatrix hypothesis = new DenseDoubleMatrix(new double[][] {
        { 0d, 0d, 0d, 1d, 0d }, { 1d, 1d, 1d, 0d, 0d } });
    double error = new CrossEntropyErrorFunction()
        .calculateError(y, hypothesis);
    assertEquals(25d, error, 1e-4);
  }
View Full Code Here

  @Test
  public void testFoldAndUnfold() {
    DenseDoubleVector referenceFold = new DenseDoubleVector(new double[] { 1.0,
        4.0, 2.0, 5.0, 3.0, 6.0, 7.0, 10.0, 8.0, 11.0, 9.0, 12.0 });
    DenseDoubleMatrix mat1 = new DenseDoubleMatrix(new double[][] {
        { 1, 2, 3 }, { 4, 5, 6 } });
    DenseDoubleMatrix mat2 = new DenseDoubleMatrix(new double[][] {
        { 7, 8, 9 }, { 10, 11, 12 } });

    DoubleVector foldMatrices = DenseMatrixFolder.foldMatrices(mat1, mat2);
    assertEquals(12, foldMatrices.getLength());
    assertEquals(0.0d, referenceFold.subtract(foldMatrices).sum(), 1e-5);
View Full Code Here

public class MatrixWritableTest {

  @Test
  public void testDenseSerDe() throws Exception {
    DenseDoubleMatrix mat = new DenseDoubleMatrix(new double[][] { { 1, 2, 3 },
        { 4, 5, 6 } });

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream out = new DataOutputStream(baos);

    MatrixWritable.writeDenseMatrix(mat, out);

    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    DataInputStream in = new DataInputStream(bais);
    DoubleMatrix readMat = MatrixWritable.readDenseMatrix(in);

    assertEquals(0.0d, mat.subtract(readMat).sum(), 1e-4);

  }
View Full Code Here

  }

  @Test
  public void testSparseSerDe() throws Exception {
    SparseDoubleRowMatrix mat = new SparseDoubleRowMatrix(
        new DenseDoubleMatrix(new double[][] { { 1, 0, 3 }, { 0, 0, 0 },
            { 1, 0, 0 } }));

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream out = new DataOutputStream(baos);
View Full Code Here

    // TODO if you are really caring about performance and memory usage, you can
    // implement some methods to translate the indices from matrices to vectors,
    // so you don't have to copy all that memory.
    DoubleMatrix theta = DenseMatrixFolder.unfoldMatrix(input, classes,
        (int) (input.getLength() / (double) classes));
    DenseDoubleMatrix gradient = new DenseDoubleMatrix(theta.getRowCount(),
        theta.getColumnCount());

    double cost = 0d;
    // loop over all feature rows to determine the probabilities
    for (int row = 0; row < m; row++) {
      DoubleVector rowVector = features.getRowVector(row);
      double[] logProbabilities = new double[classes];
      // sum the probabilities for each class over all features
      Iterator<DoubleVectorElement> iterateNonZero = rowVector.iterateNonZero();
      while (iterateNonZero.hasNext()) {
        DoubleVectorElement next = iterateNonZero.next();
        for (int i = 0; i < classes; i++) {
          logProbabilities[i] += theta.get(i, next.getIndex());
        }
      }
      double z = logSum(logProbabilities);
      for (int i = 0; i < classes; i++) {
        double prob = Math.exp(logProbabilities[i] - z);
        iterateNonZero = rowVector.iterateNonZero();
        while (iterateNonZero.hasNext()) {
          DoubleVectorElement next = iterateNonZero.next();
          gradient.set(i, next.getIndex(), gradient.get(i, next.getIndex())
              + prob);
          if (correctPrediction(i, outcome.getRowVector(row))) {
            gradient.set(i, next.getIndex(),
                gradient.get(i, next.getIndex()) - 1d);
          }
        }
        if (correctPrediction(i, outcome.getRowVector(row))) {
          cost -= Math.log(prob);
        }
View Full Code Here

TOP

Related Classes of de.jungblut.math.dense.DenseDoubleMatrix

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.