Package de.jungblut.math

Examples of de.jungblut.math.DoubleMatrix


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

    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

    MatrixWritable.writeSparseMatrix(mat, out);

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

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

  }
View Full Code Here

  @Override
  public CostGradientTuple evaluateCost(DoubleVector input) {
    // 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);
View Full Code Here

        .checkArgument(
            features.length == outcome.length && features.length > 0,
            "There wasn't at least a single featurevector, or the two array didn't match in size.");
    this.classes = outcome[0].getDimension() == 1 ? 2 : outcome[0]
        .getDimension();
    DoubleMatrix mat = null;
    if (features[0].isSparse()) {
      mat = new SparseDoubleRowMatrix(features);
    } else {
      mat = new DenseDoubleMatrix(features);
    }
    ConditionalLikelihoodCostFunction func = new ConditionalLikelihoodCostFunction(
        mat, new DenseDoubleMatrix(outcome));
    DenseDoubleVector vx = new DenseDoubleVector(mat.getColumnCount() * classes);
    DoubleVector input = minimizer.minimize(func, vx, numIterations, verbose);
    theta = DenseMatrixFolder.unfoldMatrix(input, classes,
        (int) (input.getLength() / (double) classes));
  }
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.