Examples of SparseRowMatrix


Examples of org.apache.mahout.math.SparseRowMatrix

  @Override
  public void map(IntWritable docId, VectorWritable doc, Context context)
      throws IOException, InterruptedException {
    int numTopics = getNumTopics();
    Vector docTopics = new DenseVector(new double[numTopics]).assign(1.0 /numTopics);
    Matrix docModel = new SparseRowMatrix(numTopics, doc.get().size());
    int maxIters = getMaxIters();
    ModelTrainer modelTrainer = getModelTrainer();
    for(int i = 0; i < maxIters; i++) {
      modelTrainer.getReadModel().trainDocTopicModel(doc.get(), docTopics, docModel);
    }
View Full Code Here

Examples of org.apache.mahout.math.SparseRowMatrix

    if (inMemory) {
      List<Vector> eigenVectors = Lists.newArrayList();
      for (MatrixSlice slice : eigens) {
        eigenVectors.add(slice.vector());
      }
      eigensToVerify = new SparseRowMatrix(eigenVectors.size(), eigenVectors.get(0).size(),
                                           eigenVectors.toArray(new Vector[eigenVectors.size()]),
                                           true,
                                           true);

    } else {
View Full Code Here

Examples of org.apache.mahout.math.SparseRowMatrix

        vectorList.add(record.getSecond().get());
      }
    }
    int numRows = vectorList.size();
    int numCols = vectorList.get(0).size();
    return new SparseRowMatrix(numRows, numCols,
        vectorList.toArray(new Vector[vectorList.size()]), true,
        vectorList.get(0).isSequentialAccess());
  }
View Full Code Here

Examples of org.apache.mahout.math.SparseRowMatrix

    while(true) {
      try {
        List<TrainerRunnable> runnables = Lists.newArrayList();
        for(Map.Entry<Vector, Vector> entry : batch.entrySet()) {
          runnables.add(new TrainerRunnable(readModel, null, entry.getKey(),
              entry.getValue(), new SparseRowMatrix(numTopics, numTerms, true),
              numDocTopicsIters));
        }
        threadPool.invokeAll(runnables);
        if(update) {
          for(TrainerRunnable runnable : runnables) {
View Full Code Here

Examples of org.apache.mahout.math.SparseRowMatrix

  public void train(Vector document, Vector docTopicCounts, boolean update, int numDocTopicIters) {
    while(true) {
      try {
        workQueue.put(new TrainerRunnable(readModel,
            update ? writeModel : null, document, docTopicCounts, new SparseRowMatrix(
            numTopics, numTerms, true), numDocTopicIters));
        return;
      } catch (InterruptedException e) {
        log.warn("Interrupted waiting to submit document to work queue: " + document, e);
      }
View Full Code Here

Examples of org.apache.mahout.math.SparseRowMatrix

  }

  public void trainSync(Vector document, Vector docTopicCounts, boolean update,
      int numDocTopicIters) {
    new TrainerRunnable(readModel,
            update ? writeModel : null, document, docTopicCounts, new SparseRowMatrix(
            numTopics, numTerms, true), numDocTopicIters).run();
  }
View Full Code Here

Examples of org.apache.mahout.math.SparseRowMatrix

            numTopics, numTerms, true), numDocTopicIters).run();
  }

  public double calculatePerplexity(Vector document, Vector docTopicCounts, int numDocTopicIters) {
    TrainerRunnable runner =  new TrainerRunnable(readModel,
            null, document, docTopicCounts, new SparseRowMatrix(
            numTopics, numTerms, true), numDocTopicIters);
    return runner.call();
  }
View Full Code Here

Examples of org.apache.mahout.math.SparseRowMatrix

    }
  }

  public static Matrix sampledCorpus(Matrix matrix, Random random,
      int numDocs, int numSamples, int numTopicsPerDoc) {
    Matrix corpus = new SparseRowMatrix(numDocs, matrix.numCols());
    LDASampler modelSampler = new LDASampler(matrix, random);
    Vector topicVector = new DenseVector(matrix.numRows());
    for(int i = 0; i < numTopicsPerDoc; i++) {
      int topic = random.nextInt(topicVector.size());
      topicVector.set(topic, topicVector.get(topic) + 1);
    }
    for(int docId = 0; docId < numDocs; docId++) {
      for(int sample : modelSampler.sample(topicVector, numSamples)) {
        corpus.set(docId, sample, corpus.get(docId, sample) + 1);
      }
    }
    return corpus;
  }
View Full Code Here

Examples of org.apache.mahout.math.SparseRowMatrix

  public static Matrix randomSequentialAccessSparseMatrix(int numRows,
                                                          int nonNullRows,
                                                          int numCols,
                                                          int entriesPerRow,
                                                          double entryMean) {
    Matrix m = new SparseRowMatrix(numRows, numCols);
    //double n = 0;
    Random r = RandomUtils.getRandom();
    for (int i = 0; i < nonNullRows; i++) {
      Vector v = new SequentialAccessSparseVector(numCols);
      for (int j = 0; j < entriesPerRow; j++) {
        int col = r.nextInt(numCols);
        double val = r.nextGaussian();
        v.set(col, val * entryMean);
      }
      int c = r.nextInt(numRows);
      if (r.nextBoolean() || numRows == nonNullRows) {
        m.assignRow(numRows == nonNullRows ? i : c, v);
      } else {
        Vector other = m.viewRow(r.nextInt(numRows));
        if (other != null && other.getLengthSquared() > 0) {
          m.assignRow(c, other.clone());
        }
      }
      //n += m.getRow(c).getLengthSquared();
    }
    return m;
View Full Code Here

Examples of org.apache.mahout.math.SparseRowMatrix

  public static Matrix randomSequentialAccessSparseMatrix(int numRows,
                                                          int nonNullRows,
                                                          int numCols,
                                                          int entriesPerRow,
                                                          double entryMean) {
    SparseRowMatrix m = new SparseRowMatrix(new int[]{numRows, numCols});
    //double n = 0;
    Random r = new Random(1234L);
    for (int i = 0; i < nonNullRows; i++) {
      SequentialAccessSparseVector v = new SequentialAccessSparseVector(numCols);
      for (int j = 0; j < entriesPerRow; j++) {
        int col = r.nextInt(numCols);
        double val = r.nextGaussian();
        v.set(col, val * entryMean);
      }
      int c = r.nextInt(numRows);
      if (r.nextBoolean() || numRows == nonNullRows) {
        m.assignRow(numRows == nonNullRows ? i : c, v);
      } else {
        Vector other = m.getRow(r.nextInt(numRows));
        if (other != null && other.getLengthSquared() > 0) {
          m.assignRow(c, other.clone());
        }
      }
      //n += m.getRow(c).getLengthSquared();
    }
    return m;
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.