Package de.jungblut.math.dense

Examples of de.jungblut.math.dense.DenseDoubleMatrix


          rows);
    } else {
      JCublas.cublasGetMatrix(rows, columns, Sizeof.DOUBLE, src, rows, dst,
          rows);
    }
    return new DenseDoubleMatrix(raw, rows, columns);
  }
View Full Code Here


    int n = 40000;
    int k = 784;
    int m = 300;

    DenseDoubleMatrix a = new DenseDoubleMatrix(n, k, new Random());
    DenseDoubleMatrix b = new DenseDoubleMatrix(k, m, new Random());
    long start = System.currentTimeMillis();
    DenseDoubleMatrix multiplyGPU = multiply(a, b);
    LOG.info("GPU took: " + (System.currentTimeMillis() - start) / 1000f + "s!");
    start = System.currentTimeMillis();
    DenseDoubleMatrix multiplyCPU = (DenseDoubleMatrix) a.multiply(b);
    LOG.info("CPU took: " + (System.currentTimeMillis() - start) / 1000f + "s!");
    LOG.info("Matrix difference: " + multiplyCPU.subtract(multiplyGPU).sum());
  }
View Full Code Here

  protected DoubleMatrix newInstance(DoubleMatrix mat) {
    if (mat.isSparse()) {
      return new SparseDoubleRowMatrix(mat.getRowCount(), mat.getColumnCount());
    } else {
      return new DenseDoubleMatrix(mat.getRowCount(), mat.getColumnCount());
    }
  }
View Full Code Here

  // test constructor
  HMM(int numVisibleStates, int numHiddenStates, long seed) {
    this.seed = seed;
    this.numVisibleStates = numVisibleStates;
    this.numHiddenStates = numHiddenStates;
    this.transitionProbabilityMatrix = new DenseDoubleMatrix(numHiddenStates,
        numHiddenStates);
    this.emissionProbabilityMatrix = new DenseDoubleMatrix(numHiddenStates,
        numVisibleStates);
    this.hiddenPriorProbability = new DenseDoubleVector(numHiddenStates);
  }
View Full Code Here

   * @param observationSequence the given sequence of observations (features).
   * @return the likelihood (not a probability!) that the given sequence is
   *         about to happen.
   */
  public double estimateLikelihood(DoubleVector[] observationSequence) {
    return estimateLikelihood(forward(new DenseDoubleMatrix(
        observationSequence.length, numHiddenStates),
        transitionProbabilityMatrix, emissionProbabilityMatrix,
        hiddenPriorProbability, observationSequence));
  }
View Full Code Here

   */
  public void trainUnsupervised(DoubleVector[] features, double epsilon,
      int maxIterations, boolean verbose) {
    // initialize a random starting state
    Random random = new Random(seed);
    transitionProbabilityMatrix = new DenseDoubleMatrix(numHiddenStates,
        numHiddenStates, random);
    emissionProbabilityMatrix = new DenseDoubleMatrix(numHiddenStates,
        numVisibleStates, random);
    hiddenPriorProbability = new DenseDoubleVector(numHiddenStates);
    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);
        }
View Full Code Here

  @Test
  public void testStaticThresholding() {

    // rows are the terms, columns are the contexts
    DenseDoubleMatrix weights = new DenseDoubleMatrix(new double[][] {
        { 0.2, 0.8, 0.9 }, // A
        { 0.1, 0.8, 0.7 }, // B
        { 0.1, 0.88, 0.79 }, // C
        { 0.45, 0.6, 0.4 }, // D
        { 0.89, 0.4, 0.1 }, // E
View Full Code Here

        bestLabel = label;
        bestScore = scores[m - 1][label];
      }
    }

    DoubleMatrix outcome = new DenseDoubleMatrix(features.getRowCount(),
        classes == 2 ? 1 : classes);
    // follow the backpointers
    for (position = m - 1; position >= 0; position--) {
      DenseDoubleVector vec = null;
      if (classes != 2) {
        vec = new DenseDoubleVector(classes);
        vec.set(bestLabel, 1);
      } else {
        vec = new DenseDoubleVector(1);
        vec.set(0, bestLabel);
      }
      outcome.setRowVector(position, vec);
      bestLabel = backpointers[position][bestLabel];
    }

    return outcome;
  }
View Full Code Here

   * @return mean normalized matrix (0 mean and stddev of 1) as well as the
   *         mean.
   */
  public static Tuple<DoubleMatrix, DoubleVector> meanNormalizeRows(
      DoubleMatrix pMatrix) {
    DoubleMatrix matrix = new DenseDoubleMatrix(pMatrix.getRowCount(),
        pMatrix.getColumnCount());
    DoubleVector meanVector = new DenseDoubleVector(matrix.getRowCount());
    for (int row = 0; row < matrix.getRowCount(); row++) {
      double mean = 0.0d;
      int nonZeroElements = 0;
      for (int column = 0; column < matrix.getColumnCount(); column++) {
        double val = pMatrix.get(row, column);
        if (val != DoubleMatrix.NOT_FLAGGED) {
          mean += val;
          nonZeroElements++;
        }
      }
      // prevent division by zero
      if (nonZeroElements != 0.0d) {
        mean = mean / nonZeroElements;
      }
      meanVector.set(row, mean);
      for (int column = 0; column < matrix.getColumnCount(); column++) {
        double val = pMatrix.get(row, column);
        if (val != DoubleMatrix.NOT_FLAGGED) {
          matrix.set(row, column, val - mean);
        }
      }
    }

    return new Tuple<>(matrix, meanVector);
View Full Code Here

   * @return a new dense matrix from the stream.
   * @throws IOException in case of an IO error.
   */
  public static DenseDoubleMatrix readDenseMatrix(DataInput in)
      throws IOException {
    DenseDoubleMatrix mat = new DenseDoubleMatrix(in.readInt(), in.readInt());
    for (int row = 0; row < mat.getRowCount(); row++) {
      for (int col = 0; col < mat.getColumnCount(); col++) {
        mat.set(row, col, in.readDouble());
      }
    }
    return mat;
  }
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.