Package de.jungblut.math

Examples of de.jungblut.math.DoubleVector


  }

  @Override
  public DoubleVector apply(DoubleVector vector) {
    double max = vector.max();
    DoubleVector subtract = vector.subtract(max);
    if (vector.isSparse()) {
      Iterator<DoubleVectorElement> iterateNonZero = vector.iterateNonZero();
      while (iterateNonZero.hasNext()) {
        DoubleVectorElement next = iterateNonZero.next();
        subtract.set(next.getIndex(), Math.exp(subtract.get(next.getIndex())));
      }
    } else {
      for (int i = 0; i < subtract.getLength(); i++) {
        subtract.set(i, Math.exp(subtract.get(i)));
      }
    }
    return subtract.divide(subtract.sum());
  }
View Full Code Here


      emissionProbabilityMatrix.setRowVector(rowIndex,
          DenseDoubleVector.ones(numVisibleStates));
    }

    for (int i = 0; i < features.length; i++) {
      DoubleVector feat = features[i];
      DoubleVector out = outcome[i];

      int index = getOutcomeState(out);
      hiddenPriorProbability.set(index, hiddenPriorProbability.get(index) + 1);

      // count the emissions from feature layer to the hidden layer
      Iterator<DoubleVectorElement> iterateNonZero = feat.iterateNonZero();
      while (iterateNonZero.hasNext()) {
        DoubleVectorElement next = iterateNonZero.next();
        emissionProbabilityMatrix.set(index, next.getIndex(),
            emissionProbabilityMatrix.get(index, next.getIndex()) + 1);
      }
      // now handle the feature by counting the transitions between the hidden
      // states with the next feature
      if (i + 1 < features.length) {
        DoubleVector nextOut = outcome[i + 1];
        int nextIndex = getOutcomeState(nextOut);
        transitionProbabilityMatrix.set(index, nextIndex,
            transitionProbabilityMatrix.get(index, nextIndex) + 1);
      }
    }
View Full Code Here

  @Override
  public DoubleVector predict(DoubleVector features) {
    // clamp the features to the visible units, calculate the joint
    // probability for each hidden state and put it into the vector
    DoubleVector probabilities = emissionProbabilityMatrix
        .multiplyVectorRow(features);
    double max = probabilities.max();
    for (int state = 0; state < probabilities.getDimension(); state++) {
      probabilities.set(state, FastMath.exp(probabilities.get(state) - max)
          * hiddenPriorProbability.get(state));
    }
    // normalize again
    return probabilities.divide(probabilities.sum());
  }
View Full Code Here

  @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

  public DoubleVector predict(DoubleVector features,
      DoubleVector previousOutcome) {
    // clamp the features to the visible units, calculate the joint
    // probability for each hidden state and put it into the vector
    DoubleVector probabilities = emissionProbabilityMatrix
        .multiplyVectorRow(features);
    // we can add here, both are logarithms
    probabilities.add(transitionProbabilityMatrix
        .multiplyVectorRow(previousOutcome));
    double max = probabilities.max();
    for (int state = 0; state < probabilities.getDimension(); state++) {
      probabilities.set(state, FastMath.exp(probabilities.get(state) - max)
          * hiddenPriorProbability.get(state));
    }
    // normalize again
    return probabilities.divide(probabilities.sum());
  }
View Full Code Here

  /**
   * Predicts the outcome of the given input by doing a forward pass.
   */
  @Override
  public DoubleVector predict(DoubleVector xi) {
    DoubleVector activationVector = addBias(xi);
    final int len = layers.length - 1;
    for (int i = 1; i <= len; i++) {
      activationVector = activations[i].apply(weights[i - 1].getWeights()
          .multiplyVectorRow(activationVector));
      // only add the bias if we are not in the last layer
View Full Code Here

   * Predicts the outcome of the given input by doing a forward pass. Used for
   * binary classification by a threshold. Everything above threshold will be
   * considered as 1, the other case as 0.
   */
  public DoubleVector predict(DoubleVector xi, double threshold) {
    DoubleVector activations = predict(xi);
    for (int i = 0; i < activations.getLength(); i++) {
      activations.set(i, activations.get(i) > threshold ? 1.0d : 0.0d);
    }

    return activations;
  }
View Full Code Here

   * @return the cost of the training.
   */
  private double trainInternal(Minimizer minimizer, int maxIterations,
      boolean verbose, CostFunction costFunction, DoubleVector initialTheta) {
    Preconditions.checkNotNull(minimizer, "Minimizer must be supplied!");
    DoubleVector theta = minimizer.minimize(costFunction, initialTheta,
        maxIterations, verbose);
    int[][] unfoldParameters = MultilayerPerceptronCostFunction
        .computeUnfoldParameters(layers);

    DoubleMatrix[] unfoldMatrices = DenseMatrixFolder.unfoldMatrices(theta,
View Full Code Here

    }
  }

  public static DoubleVector readVector(DataInput in) throws IOException {
    final int length = in.readInt();
    DoubleVector vector = new DenseDoubleVector(length);
    for (int i = 0; i < length; i++) {
      vector.set(i, in.readDouble());
    }
    return vector;
  }
View Full Code Here

  public static int compareVector(VectorWritable a, VectorWritable o) {
    return compareVector(a.getVector(), o.getVector());
  }

  public static int compareVector(DoubleVector a, DoubleVector o) {
    DoubleVector subtract = a.subtract(o);
    return (int) subtract.sum();
  }
View Full Code Here

TOP

Related Classes of de.jungblut.math.DoubleVector

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.