Exemplo n.º 1
0
  private Map<MatrixSlice, EigenStatus> verifyEigens() {
    Map<MatrixSlice, EigenStatus> eigenMetaData = Maps.newHashMap();

    for (MatrixSlice slice : eigensToVerify) {
      EigenStatus status = eigenVerifier.verify(corpus, slice.vector());
      eigenMetaData.put(slice, status);
    }
    return eigenMetaData;
  }
 public int printDistributedRowMatrix() {
   System.out.println("RowPath: " + this.rowPath);
   Iterator<MatrixSlice> iterator = this.iterateAll();
   int count = 0;
   while (iterator.hasNext()) {
     MatrixSlice slice = iterator.next();
     Vector v = slice.vector();
     int size = v.size();
     for (int i = 0; i < size; i++) {
       Element e = v.getElement(i);
       count++;
       System.out.print(e.get() + " ");
     }
     System.out.println();
   }
   return count;
 }
 @Test
 public void testEigen() {
   double[] evals = {
     0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 2.0e-7, 0.0, 0.0, -2.0e-7, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0
   };
   int i = 0;
   Matrix a = new DenseMatrix(4, 4);
   for (MatrixSlice row : a) {
     for (Vector.Element element : row.vector()) {
       element.set(evals[i++]);
     }
   }
   EigenDecomposition eig = new EigenDecomposition(a);
   Matrix d = eig.getD();
   Matrix v = eig.getV();
   check("EigenvalueDecomposition (nonsymmetric)...", a.times(v), v.times(d));
 }
Exemplo n.º 4
0
  private void prepareEigens(Configuration conf, Path eigenInput, boolean inMemory) {
    DistributedRowMatrix eigens = new DistributedRowMatrix(eigenInput, tmpOut, 1, 1);
    eigens.setConf(conf);
    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 {
      eigensToVerify = eigens;
    }
  }
Exemplo n.º 5
0
  private void saveCleanEigens(
      Configuration conf, Collection<Map.Entry<MatrixSlice, EigenStatus>> prunedEigenMeta)
      throws IOException {
    Path path = new Path(outPath, CLEAN_EIGENVECTORS);
    FileSystem fs = FileSystem.get(path.toUri(), conf);
    SequenceFile.Writer seqWriter =
        new SequenceFile.Writer(fs, conf, path, IntWritable.class, VectorWritable.class);
    try {
      IntWritable iw = new IntWritable();
      int numEigensWritten = 0;
      int index = 0;
      for (Map.Entry<MatrixSlice, EigenStatus> pruneSlice : prunedEigenMeta) {
        MatrixSlice s = pruneSlice.getKey();
        EigenStatus meta = pruneSlice.getValue();
        EigenVector ev =
            new EigenVector(
                s.vector(), meta.getEigenValue(), Math.abs(1 - meta.getCosAngle()), s.index());
        // log.info("appending {} to {}", ev, path);
        Writable vw = new VectorWritable(ev);
        iw.set(index++);
        seqWriter.append(iw, vw);

        // increment the number of eigenvectors written and see if we've
        // reached our specified limit, or if we wish to write all eigenvectors
        // (latter is built-in, since numEigensWritten will always be > 0
        numEigensWritten++;
        if (numEigensWritten == maxEigensToKeep) {
          log.info(
              "{} of the {} total eigens have been written",
              maxEigensToKeep,
              prunedEigenMeta.size());
          break;
        }
      }
    } finally {
      Closeables.close(seqWriter, false);
    }
    cleanedEigensPath = path;
  }
  @Test
  public void testSequential() {
    int validld = 3;
    Matrix A = new DenseMatrix(validld, validld);
    double[] columnwise = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0};
    int i = 0;
    for (MatrixSlice row : A) {
      for (Vector.Element element : row.vector()) {
        element.set(columnwise[i++]);
      }
    }

    EigenDecomposition Eig = new EigenDecomposition(A);
    Matrix D = Eig.getD();
    Matrix V = Eig.getV();
    check("EigenvalueDecomposition (nonsymmetric)...", A.times(V), V.times(D));

    A = A.transpose().times(A);
    Eig = new EigenDecomposition(A);
    D = Eig.getD();
    V = Eig.getV();
    check("EigenvalueDecomposition (symmetric)...", A.times(V), V.times(D));
  }