congrats Icon
New! Announcing Tabnine Chat Beta
Learn More
Tabnine Logo
DoubleMatrix
Code IndexAdd Tabnine to your IDE (free)

How to use
DoubleMatrix
in
de.jungblut.math

Best Java code snippets using de.jungblut.math.DoubleMatrix (Showing top 20 results out of 315)

origin: de.jungblut.common/thomasjungblut-common

@Override
public DoubleMatrix gradient(DoubleMatrix matrix) {
  DoubleMatrix newInstance = newInstance(matrix);
  if (matrix.isSparse()) {
    // if we have a sparse matrix, it is more efficient to loop over the
    // sparse column vectors
    int[] columnIndices = matrix.columnIndices();
    for (int col : columnIndices) {
      newInstance.setColumnVector(col, gradient(matrix.getColumnVector(col)));
    }
  } else {
    // on dense matrices we can be faster by directly looping over the items
    for (int i = 0; i < matrix.getRowCount(); i++) {
      for (int j = 0; j < matrix.getColumnCount(); j++) {
        newInstance.set(i, j, gradient(matrix.get(i, j)));
      }
    }
  }
  return newInstance;
}
origin: de.jungblut.common/thomasjungblut-common

@Override
public DoubleMatrix apply(DoubleMatrix matrix) {
  DoubleMatrix newInstance = newInstance(matrix);
  if (matrix.isSparse()) {
    // if we have a sparse matrix, it is more efficient to loop over the
    // sparse row vectors
    int[] rows = matrix.rowIndices();
    for (int row : rows) {
      DoubleVector rowVector = matrix.getRowVector(row);
      if (rowVector.getLength() > 0) {
        DoubleVector apply = apply(rowVector);
        newInstance.setRowVector(row, apply);
      }
    }
  } else {
    // on dense matrices we can be faster by directly looping over the items
    for (int i = 0; i < matrix.getRowCount(); i++) {
      for (int j = 0; j < matrix.getColumnCount(); j++) {
        newInstance.set(i, j, apply(matrix.get(i, j)));
      }
    }
  }
  return newInstance;
}
origin: de.jungblut.common/thomasjungblut-common

@Override
public double calculateLoss(DoubleMatrix y, DoubleMatrix hypothesis) {
  double sum = 0d;
  for (int col = 0; col < y.getColumnCount(); col++) {
    for (int row = 0; row < y.getRowCount(); row++) {
      double diff = y.get(row, col) - hypothesis.get(row, col);
      sum += (diff * diff);
    }
  }
  return sum / y.getRowCount();
}
origin: de.jungblut.common/thomasjungblut-common

public ConditionalLikelihoodCostFunction(DoubleMatrix features,
                     DoubleMatrix outcome) {
  this.features = features;
  this.outcome = outcome;
  this.m = outcome.getRowCount();
  this.classes = outcome.getColumnCount() == 1 ? 2 : outcome.getColumnCount();
}
origin: de.jungblut.common/thomasjungblut-common

public static double calculateRegularization(DoubleMatrix[] thetas,
                       final int m, NetworkConfiguration conf) {
  double regularization = 0d;
  // only calculate the regularization term if lambda is not 0
  if (conf.lambda != 0d) {
    for (DoubleMatrix theta : thetas) {
      regularization += (theta.slice(0, theta.getRowCount(), 1,
          theta.getColumnCount())).pow(2).sum();
    }
    regularization = (conf.lambda / (2.0d * m)) * regularization;
  }
  return regularization;
}
origin: de.jungblut.common/thomasjungblut-common

static DoubleMatrix binarize(Random r, DoubleMatrix hiddenActivations) {
  for (int i = 0; i < hiddenActivations.getRowCount(); i++) {
    for (int j = 0; j < hiddenActivations.getColumnCount(); j++) {
      hiddenActivations.set(i, j,
          hiddenActivations.get(i, j) > r.nextDouble() ? 1d : 0d);
    }
  }
  return hiddenActivations;
}
origin: de.jungblut.math/tjungblut-math

@Override
public DoubleMatrix subtract(DoubleMatrix other) {
 DoubleMatrix m = new DenseDoubleMatrix(this.numRows, this.numColumns);
 for (int i = 0; i < numRows; i++) {
  for (int j = 0; j < numColumns; j++) {
   m.set(i, j, this.matrix[translate(i, j, numRows)] - other.get(i, j));
  }
 }
 return m;
}
origin: de.jungblut.common/thomasjungblut-common

    .deepCopy();
DoubleMatrix emissionProbabilityMatrix = this.emissionProbabilityMatrix
    .deepCopy();
DoubleVector hiddenPriorProbability = this.hiddenPriorProbability
    .deepCopy();
hiddenPriorProbability = alpha.getRowVector(0).multiply(
    beta.getRowVector(0));
final double modelLikelihood = estimateLikelihood(alpha);
          .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);
        DoubleVectorElement next = iterateNonZero.next();
        if (next.getIndex() == j) {
          temp += alpha.get(t, i) * beta.get(t, i);
    emissionProbabilityMatrix.set(i, j, temp / modelLikelihood);
    .subtract(transitionProbabilityMatrix).pow(2).sum()
    + this.emissionProbabilityMatrix.subtract(emissionProbabilityMatrix)
    .pow(2).sum()
    + this.getHiddenPriorProbability().subtract(hiddenPriorProbability)
origin: de.jungblut.common/thomasjungblut-common

    unfoldParameters)[0].transpose();
positiveHiddenProbs.setColumnVector(0,
    DenseDoubleVector.ones(positiveHiddenProbs.getRowCount()));
DoubleMatrix positiveAssociations = multiply(data, positiveHiddenProbs,
    true, false);
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);
double j = data.subtract(negativeData).pow(2).sum();
DoubleMatrix thetaGradient = positiveAssociations.subtract(
    negativeAssociations).divide(data.getRowCount());
  DoubleVector bias = thetaGradient.getColumnVector(0);
  thetaGradient = thetaGradient.subtract(thetaGradient.multiply(lambda
      / data.getRowCount()));
  thetaGradient.setColumnVector(0, bias);
        .multiply(-1).transpose()));
origin: de.jungblut.common/thomasjungblut-common

                 DoubleMatrix emissionProbabilityMatrix,
                 DoubleVector hiddenPriorProbability, DoubleVector[] features) {
final int numHiddenStates = beta.getColumnCount();
beta.setRowVector(features.length - 1,
    DenseDoubleVector.ones(numHiddenStates));
      while (iterateNonZero.hasNext()) {
        DoubleVectorElement next = iterateNonZero.next();
        sum += beta.get(t + 1, j) * transitionProbabilityMatrix.get(i, j)
            * emissionProbabilityMatrix.get(j, next.getIndex());
    beta.set(t, i, sum);
origin: de.jungblut.common/thomasjungblut-common

public static void calculateGradients(DoubleMatrix[] thetas,
                   DoubleMatrix[] thetaGradients, DoubleMatrix[] ax, DoubleMatrix[] deltaX,
                   final int m, NetworkConfiguration conf) {
  // calculate the gradients of the weights
  for (int i = 0; i < thetaGradients.length; i++) {
    DoubleMatrix gradDXA = multiply(deltaX[i + 1], ax[i], true, false, conf);
    if (m != 1) {
      thetaGradients[i] = gradDXA.divide(m);
    } else {
      thetaGradients[i] = gradDXA;
    }
    if (conf.lambda != 0d) {
      thetaGradients[i] = thetaGradients[i].add((thetas[i]
          .multiply(conf.lambda / m)));
      // subtract the regularized bias
      DoubleVector regBias = thetas[i]
          .slice(0, thetas[i].getRowCount(), 0, 1).multiply(conf.lambda / m)
          .getColumnVector(0);
      thetaGradients[i].setColumnVector(0, regBias);
    }
  }
}
origin: de.jungblut.common/thomasjungblut-common

                DoubleMatrix emissionProbabilityMatrix,
                DoubleVector hiddenPriorProbability, DoubleVector[] features) {
final int numHiddenStates = alpha.getColumnCount();
for (int i = 0; i < numHiddenStates; i++) {
  double emissionSum = 0d;
  while (firstFeatures.hasNext()) {
    emissionSum += emissionProbabilityMatrix.get(i, firstFeatures.next()
        .getIndex());
  alpha.set(0, i, hiddenPriorProbability.get(i) * emissionSum);
    double sum = 0.0d;
    for (int j = 0; j < numHiddenStates; j++) {
      sum += alpha.get(t - 1, j) * transitionProbabilityMatrix.get(j, i);
      emissionSum += emissionProbabilityMatrix.get(i, featureIterator
          .next().getIndex());
    alpha.set(t, i, sum * emissionSum);
origin: thomasjungblut/tjungblut-online-ml

DoubleVector rowVector = probabilityMatrix.getRowVector(row);
  + probabilityMatrix.getColumnCount() - 1);
while (iterateNonZero.hasNext()) {
 DoubleVectorElement next = iterateNonZero.next();
 double currentWordCount = next.getValue();
 double logProbability = FastMath.log(currentWordCount) - normalizer;
 probabilityMatrix.set(row, next.getIndex(), logProbability);
origin: de.jungblut.common/thomasjungblut-common

@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;
}
origin: de.jungblut.common/thomasjungblut-common

DenseDoubleMatrix gradient = new DenseDoubleMatrix(theta.getRowCount(),
    theta.getColumnCount());
  DoubleVector rowVector = features.getRowVector(row);
  double[] logProbabilities = new double[classes];
    DoubleVectorElement next = iterateNonZero.next();
    for (int i = 0; i < classes; i++) {
      logProbabilities[i] += theta.get(i, next.getIndex());
      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);
origin: de.jungblut.common/thomasjungblut-common

    int count = (int) transitionProbabilities.get(array[i], array[i + 1]);
    transitionProbabilities.set(array[i], array[i + 1], ++count);
final int[] rowEntries = transitionProbabilities.rowIndices();
for (int rowIndex : rowEntries) {
  DoubleVector rowVector = transitionProbabilities.getRowVector(rowIndex);
  double sum = rowVector.sum();
  Iterator<DoubleVectorElement> iterateNonZero = rowVector.iterateNonZero();
    double probability = FastMath.log(columnElement.getValue())
        - FastMath.log(sum);
    transitionProbabilities.set(rowIndex, columnIndex, probability);
origin: de.jungblut.common/thomasjungblut-common

/**
 * Scales a matrix into the interval given by min and max.
 *
 * @param input   the input value.
 * @param fromMin the lower bound of the input interval.
 * @param fromMax the upper bound of the input interval.
 * @param toMin   the lower bound of the target interval.
 * @param toMax   the upper bound of the target interval.
 * @return the new matrix with scaled values.
 */
public static DoubleMatrix minMaxScale(DoubleMatrix input, double fromMin,
                    double fromMax, double toMin, double toMax) {
  DoubleMatrix newOne = new DenseDoubleMatrix(input.getRowCount(),
      input.getColumnCount());
  double[][] array = input.toArray();
  for (int row = 0; row < newOne.getRowCount(); row++) {
    for (int col = 0; col < newOne.getColumnCount(); col++) {
      newOne.set(row, col,
          minMaxScale(array[row][col], fromMin, fromMax, toMin, toMax));
    }
  }
  return newOne;
}
origin: de.jungblut.common/thomasjungblut-common

/**
 * Sets the weights in the whole matrix uniformly between -eInit and eInit
 * (eInit is the standard deviation) with zero mean.
 */
private void setWeightsUniformly(RandomDataImpl rnd, double eInit) {
  for (int i = 0; i < weights.getColumnCount(); i++) {
    for (int j = 0; j < weights.getRowCount(); j++) {
      weights.set(j, i, rnd.nextUniform(-eInit, eInit));
    }
  }
}
origin: de.jungblut.math/tjungblut-math

/**
 * Creates a new matrix with the given vector into the first column and the
 * other matrix to the other columns. This is usually used in machine learning
 * algorithms that add a bias on the zero-index column.
 * 
 * @param first the new first column.
 * @param otherMatrix the other matrix to set on from the second column.
 */
public DenseDoubleMatrix(DenseDoubleVector first, DoubleMatrix otherMatrix) {
 this(otherMatrix.getRowCount(), otherMatrix.getColumnCount() + 1);
 // copy the first column
 System.arraycopy(first.toArray(), 0, matrix, 0, first.getDimension());
 int offset = first.getDimension();
 for (int col : otherMatrix.columnIndices()) {
  double[] clv = otherMatrix.getColumnVector(col).toArray();
  System.arraycopy(clv, 0, matrix, offset, clv.length);
  offset += clv.length;
 }
}
origin: de.jungblut.math/tjungblut-math

/**
 * Row-copies the given matrix to this sparse implementation.
 * 
 * @param mat the matrix to copy.
 */
public SparseDoubleRowMatrix(DoubleMatrix mat) {
 this(mat.getRowCount(), mat.getColumnCount());
 for (int i = 0; i < numColumns; i++) {
  setRowVector(i, mat.getRowVector(i));
 }
}
de.jungblut.mathDoubleMatrix

Javadoc

Standard matrix interface for double elements. Every implementation should return a fresh new Matrix when operating with other elements.

Most used methods

  • get
    Get a specific value of the matrix.
  • getColumnCount
    Returns the number of columns in the matrix. Always a constant time operation.
  • getRowVector
    Get a single row of the matrix as a vector.
  • set
    Sets the value at the given row and column index.
  • columnIndices
  • getColumnVector
    Get a whole column of the matrix as vector.
  • getRowCount
    Returns the number of rows in this matrix. Always a constant time operation.
  • setColumnVector
    Sets a whole column at index col with the given vector.
  • setRowVector
    Sets the whole row at index rowIndex with the given vector.
  • add
    Adds the elements in the given matrix to the elements in this matrix.
  • deepCopy
  • divide
    Divides each element in a column by the related element in the given vector.
  • deepCopy,
  • divide,
  • isSparse,
  • multiply,
  • multiplyElementWise,
  • multiplyVectorRow,
  • pow,
  • rowIndices,
  • slice,
  • subtract

Popular in Java

  • Start an intent from android
  • orElseThrow (Optional)
    Return the contained value, if present, otherwise throw an exception to be created by the provided s
  • getApplicationContext (Context)
  • requestLocationUpdates (LocationManager)
  • MalformedURLException (java.net)
    This exception is thrown when a program attempts to create an URL from an incorrect specification.
  • Vector (java.util)
    Vector is an implementation of List, backed by an array and synchronized. All optional operations in
  • JFileChooser (javax.swing)
  • JLabel (javax.swing)
  • DateTimeFormat (org.joda.time.format)
    Factory that creates instances of DateTimeFormatter from patterns and styles. Datetime formatting i
  • Logger (org.slf4j)
    The org.slf4j.Logger interface is the main user entry point of SLF4J API. It is expected that loggin
  • Top plugins for Android Studio
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now