Package org.apache.mahout.math

Examples of org.apache.mahout.math.DenseMatrix


    // make sure the pseudo count is not zero
    pseudoCount = pseudoCount == 0 ? Double.MIN_VALUE : pseudoCount;

    // initialize parameters
    DenseMatrix transitionMatrix = new DenseMatrix(nrOfHiddenStates,
        nrOfHiddenStates);
    DenseMatrix emissionMatrix = new DenseMatrix(nrOfHiddenStates,
        nrOfOutputStates);
    DenseVector initialProbabilities = new DenseVector(nrOfHiddenStates);

    // assign pseudo count to avoid zero probabilities
    transitionMatrix.assign(pseudoCount);
    emissionMatrix.assign(pseudoCount);
    initialProbabilities.assign(pseudoCount);

    // now loop over the sequences to count the number of transitions
    Iterator<int[]> hiddenSequenceIt = hiddenSequences.iterator();
    Iterator<int[]> observedSequenceIt = observedSequences.iterator();
    while (hiddenSequenceIt.hasNext() && observedSequenceIt.hasNext()) {
      // fetch the current set of sequences
      int[] hiddenSequence = hiddenSequenceIt.next();
      int[] observedSequence = observedSequenceIt.next();
      // increase the count for initial probabilities
      initialProbabilities.setQuick(hiddenSequence[0], initialProbabilities
          .getQuick(hiddenSequence[0]) + 1);
      countTransitions(transitionMatrix, emissionMatrix, observedSequence,
          hiddenSequence);
    }

    // make sure that probabilities are normalized
    double isum = 0; // sum of initial probabilities
    for (int i = 0; i < nrOfHiddenStates; i++) {
      isum += initialProbabilities.getQuick(i);
      // compute sum of probabilities for current row of transition matrix
      double sum = 0;
      for (int j = 0; j < nrOfHiddenStates; j++) {
        sum += transitionMatrix.getQuick(i, j);
      }
      // normalize current row of transition matrix
      for (int j = 0; j < nrOfHiddenStates; j++) {
        transitionMatrix.setQuick(i, j, transitionMatrix.getQuick(i, j) / sum);
      }
      // compute sum of probabilities for current row of emission matrix
      sum = 0;
      for (int j = 0; j < nrOfOutputStates; j++) {
        sum += emissionMatrix.getQuick(i, j);
      }
      // normalize current row of emission matrix
      for (int j = 0; j < nrOfOutputStates; j++) {
        emissionMatrix.setQuick(i, j, emissionMatrix.getQuick(i, j) / sum);
      }
    }
    // normalize the initial probabilities
    for (int i = 0; i < nrOfHiddenStates; ++i) {
      initialProbabilities.setQuick(i, initialProbabilities.getQuick(i) / isum);
View Full Code Here


  }

  @Override
  public Matrix getDiagonalMatrix() {
    if (diagonalMatrix == null) {
      diagonalMatrix = new DenseMatrix(desiredRank, desiredRank);
    }
    if (diagonalMatrix.get(0, 1) <= 0) {
      try {
        Vector norms = fetchVector(new Path(baseDir, "norms"), 0);
        Vector projections = fetchVector(new Path(baseDir, "projections"), 0);
View Full Code Here

    HmmModel iteration = initialModel.clone();

    // allocate space for baum-welch factors
    int hiddenCount = initialModel.getNrOfHiddenStates();
    int visibleCount = observedSequence.length;
    Matrix alpha = new DenseMatrix(visibleCount, hiddenCount);
    Matrix beta = new DenseMatrix(visibleCount, hiddenCount);

    // now run the baum Welch training iteration
    for (int it = 0; it < maxIterations; ++it) {
      // fetch emission and transition matrix of current iteration
      Vector initialProbabilities = iteration.getInitialProbabilities();
View Full Code Here

   * Recalculating Y^TY or X^TX which is needed for further calculations
   * @param recomputeUserFeatures
   */
  public void reCalculateTrans(boolean recomputeUserFeatures) {
    if (recomputeUserFeatures) {
      Matrix iMatrix = new DenseMatrix(itemMatrix);
      itemTransItem = iMatrix.transpose().times(iMatrix);
    } else {
      Matrix uMatrix = new DenseMatrix(userMatrix);
      userTransUser = uMatrix.transpose().times(uMatrix);
    }
  }
View Full Code Here

      Matrix XTCX;
      if (recomputeUserFeatures) {
        Matrix I = identityV(dataModel.getNumItems());
        Matrix I2 = identityV(numFeatures);
        Matrix iTi = itemTransItem.clone();
        Matrix itemM = new DenseMatrix(itemMatrix);
        XTCX = iTi.plus(itemM.transpose().times(C.minus(I)).times(itemM));

        Matrix diag = solve(XTCX.plus(I2.times(preventOverfitting)), I2);
        Matrix results = diag.times(itemM.transpose().times(C)).times(prefVector.transpose());
        updateMatrix(id, results);
      } else {
        Matrix I = identityV(dataModel.getNumUsers());
        Matrix I2 = identityV(numFeatures);
        Matrix uTu = userTransUser.clone();
        Matrix userM = new DenseMatrix(userMatrix);
        XTCX = uTu.plus(userM.transpose().times(C.minus(I)).times(userM));

        Matrix diag = solve(XTCX.plus(I2.times(preventOverfitting)), I2);
        Matrix results = diag.times(userM.transpose().times(C)).times(prefVector.transpose());
        updateMatrix(id, results);
      }
      return null;
    }
View Full Code Here

        if (i != j) {
          YtY[j][i] = dot;
        }
      }
    }
    return new DenseMatrix(YtY, true);
  }
View Full Code Here

    OpenIntObjectHashMap<Vector> CuMinusIY = new OpenIntObjectHashMap<Vector>(userRatings.getNumNondefaultElements());
    for (Element e : userRatings.nonZeroes()) {
      CuMinusIY.put(e.index(), Y.get(e.index()).times(confidence(e.get()) - 1));
    }

    Matrix YtransponseCuMinusIY = new DenseMatrix(numFeatures, numFeatures);

    /* Y' (Cu -I) Y by outer products */
    for (Element e : userRatings.nonZeroes()) {
      for (Vector.Element feature : Y.get(e.index()).all()) {
        Vector partial = CuMinusIY.get(e.index()).times(feature.get());
        YtransponseCuMinusIY.viewRow(feature.index()).assign(partial, Functions.PLUS);
      }
    }

    /* Y' (Cu - I) Y + λ I  add lambda on the diagonal */
    for (int feature = 0; feature < numFeatures; feature++) {
      YtransponseCuMinusIY.setQuick(feature, feature, YtransponseCuMinusIY.getQuick(feature, feature) + lambda);
    }

    return YtransponseCuMinusIY;
  }
View Full Code Here

  private Matrix columnVectorAsMatrix(Vector v) {
    double[][] matrix =  new double[numFeatures][1];
    for (Vector.Element e : v.all()) {
      matrix[e.index()][0] =  e.get();
    }
    return new DenseMatrix(matrix, true);
  }
View Full Code Here

        } else {
          result[i][i] = dot + lambdaTimesNui;
        }
      }
    }
    return new DenseMatrix(result, true);
  }
View Full Code Here

      for (int m = 0; m < numFeatures; m++) {
        MiIi[m][n] = featureVector.getQuick(m);
      }
      n++;
    }
    return new DenseMatrix(MiIi, true);
  }
View Full Code Here

TOP

Related Classes of org.apache.mahout.math.DenseMatrix

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.