Package org.apache.mahout.math

Examples of org.apache.mahout.math.Matrix


        OnlineLogisticRegression model = state.getModels().get(0);
        // finish off pending regularization
        model.close();
       
        Matrix beta = model.getBeta();
        maxBeta = beta.aggregate(Functions.MAX, Functions.ABS);
        nonZeros = beta.aggregate(Functions.PLUS, new DoubleFunction() {
          @Override
          public double apply(double v) {
            return Math.abs(v) > 1.0e-6 ? 1 : 0;
          }
        });
        positive = beta.aggregate(Functions.PLUS, new DoubleFunction() {
          @Override
          public double apply(double v) {
            return v > 0 ? 1 : 0;
          }
        });
        norm = beta.aggregate(Functions.PLUS, Functions.ABS);

        lambda = learningAlgorithm.getBest().getMappedParams()[0];
        mu = learningAlgorithm.getBest().getMappedParams()[1];
      } else {
        maxBeta = 0;
View Full Code Here


    return timeSolver(corpus, 0.01, 20, rank, state);
  }

  public void testHebbianSolver() throws Exception {
    int numColumns = 800;
    Matrix corpus = randomSequentialAccessSparseMatrix(1000, 900, numColumns, 30, 1.0);
    int rank = 50;
    Matrix eigens = new DenseMatrix(rank, numColumns);
    TrainingState state = new TrainingState(eigens, null);
    long optimizedTime = timeSolver(corpus,
                                    0.00001,
                                    5,
                                    rank,
View Full Code Here

    super(name);
  }

  public void testLanczosSolver() throws Exception {
    int numColumns = 800;
    Matrix corpus = randomSequentialAccessSparseMatrix(1000, 900, numColumns, 30, 1.0);
    int rank = 50;
    Matrix eigens = new DenseMatrix(rank, numColumns);
    long time = timeLanczos(corpus, eigens, rank, false);
    assertTrue("Lanczos taking too long!  Are you in the debugger? :)", time < 10000);
    assertOrthonormal(eigens);
    assertEigen(eigens, corpus, 0.1, false);
  }
View Full Code Here

    assertEigen(eigens, corpus, 0.1, false);
  }

  public void testLanczosSolverSymmetric() throws Exception {
    int numColumns = 400;
    Matrix corpus = randomSequentialAccessSparseMatrix(500, 450, numColumns, 10, 1.0);
    Matrix gramMatrix = corpus.times(corpus.transpose());
    int rank = 30;
    Matrix eigens = new DenseMatrix(rank, gramMatrix.numCols());
    long time = timeLanczos(gramMatrix, eigens, rank, true);
    assertTrue("Lanczos taking too long!  Are you in the debugger? :)", time < 10000);
    assertOrthonormal(eigens);
    assertEigen(eigens, gramMatrix, 0.1, true);
  }
View Full Code Here

    values[1][0] = 0.0;
    values[2][0] = 0.0;
    values[1][1] = 0.0;
    values[2][2] = 0.0;

    Matrix dataset = new DenseMatrix(values);
    trainer.train(labelset, dataset);
    assertTrue(trainer.getModel().classify(dataset.getColumn(3)));
    assertFalse(trainer.getModel().classify(dataset.getColumn(0)));
  }
View Full Code Here

    String outputTmpPathString = parsedArgs.get("--tempDir");
    int numRows = Integer.parseInt(parsedArgs.get("--numRows"));
    int numCols = Integer.parseInt(parsedArgs.get("--numCols"));
    boolean isSymmetric = Boolean.parseBoolean(parsedArgs.get("--symmetric"));
    int desiredRank = Integer.parseInt(parsedArgs.get("--rank"));
    Matrix eigenVectors = new DenseMatrix(desiredRank, numCols);
    List<Double> eigenValues = new ArrayList<Double>();
    String outputEigenVectorPath =  parsedArgs.get("--output");
   
    DistributedRowMatrix matrix = new DistributedRowMatrix(inputPathString,
                                                           outputTmpPathString,
View Full Code Here

    values[1][0] = 0.0;
    values[2][0] = 0.0;
    values[1][1] = 0.0;
    values[2][2] = 0.0;

    Matrix dataset = new DenseMatrix(values);
    this.trainer.train(labelset, dataset);
    assertFalse(this.trainer.getModel().classify(dataset.getColumn(3)));
    assertTrue(this.trainer.getModel().classify(dataset.getColumn(0)));
  }
View Full Code Here

    return v;
  }
 
  private LDAState generateRandomState(int numWords, int numTopics) {
    double topicSmoothing = 50.0 / numTopics; // whatever
    Matrix m = new DenseMatrix(numTopics, numWords);
    double[] logTotals = new double[numTopics];
   
    for (int k = 0; k < numTopics; ++k) {
      double total = 0.0; // total number of pseudo counts we made
      for (int w = 0; w < numWords; ++w) {
        // A small amount of random noise, minimized by having a floor.
        double pseudocount = random.nextDouble() + 1.0E-10;
        total += pseudocount;
        m.setQuick(k, w, Math.log(pseudocount));
      }
     
      logTotals[k] = Math.log(total);
    }
   
View Full Code Here

   *         singular values) have been found.
   */
  public TrainingState solve(Matrix corpus,
                             int desiredRank) {
    int cols = corpus.numCols();
    Matrix eigens = new DenseMatrix(desiredRank, cols);
    List<Double> eigenValues = new ArrayList<Double>();
    log.info("Finding " + desiredRank + " singular vectors of matrix with " + corpus.numRows() + " rows, via Hebbian");
    /**
     * The corpusProjections matrix is a running cache of the residual projection of each corpus vector against all
     * of the previously found singular vectors.  Without this, if multiple passes over the data is made (per
     * singular vector), recalculating these projections eventually dominates the computational complexity of the
     * solver.
     */
    Matrix corpusProjections = new DenseMatrix(corpus.numRows(), desiredRank);
    TrainingState state = new TrainingState(eigens, corpusProjections);
    for (int i = 0; i < desiredRank; i++) {
      Vector currentEigen = new DenseVector(cols);
      Vector previousEigen = null;
      while (hasNotConverged(currentEigen, corpus, state)) {
View Full Code Here

    numPasses++;
    if (state.isFirstPass()) {
      log.info("First pass through the corpus, no need to check convergence...");
      return true;
    }
    Matrix previousEigens = state.getCurrentEigens();
    log.info("Have made {} passes through the corpus, checking convergence...", numPasses);
    /*
     * Step 1: orthogonalize currentPseudoEigen by subtracting off eigen(i) * helper.get(i)
     * Step 2: zero-out the helper vector because it has already helped.
     */
    for (int i = 0; i < state.getNumEigensProcessed(); i++) {
      Vector previousEigen = previousEigens.getRow(i);
      currentPseudoEigen.assign(previousEigen, new PlusMult(-state.getHelperVector().get(i)));
      state.getHelperVector().set(i, 0);
    }
    if (debug && currentPseudoEigen.norm(2) > 0) {
      for (int i = 0; i < state.getNumEigensProcessed(); i++) {
        Vector previousEigen = previousEigens.getRow(i);
        log.info("dot with previous: {}", (previousEigen.dot(currentPseudoEigen)) / currentPseudoEigen.norm(2));
      }
    }
    /*
     * Step 3: verify how eigen-like the prospective eigen is.  This is potentially asynchronous.
View Full Code Here

TOP

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

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.