Examples of MatrixWritable


Examples of org.apache.mahout.math.MatrixWritable

    VectorWritable vector = new VectorWritable(model.getInitialProbabilities());
    vector.write(output);
  }

  static HmmModel deserialize(DataInput input) throws IOException {
    MatrixWritable matrix = new MatrixWritable();
    matrix.readFields(input);
    Matrix emissionMatrix = matrix.get();

    matrix.readFields(input);
    Matrix transitionMatrix = matrix.get();

    VectorWritable vector = new VectorWritable();
    vector.readFields(input);
    Vector initialProbabilities = vector.get();
View Full Code Here

Examples of org.apache.mahout.math.MatrixWritable

    seed = 1;
    Matrix y2 = null;

    // step 1, compute R as in R'R = Y'Y where Y = A \Omega
    for (File file : partsOfA) {
      MatrixWritable m = new MatrixWritable();
      DataInputStream in = new DataInputStream(new FileInputStream(file));
      try {
        m.readFields(in);
      } finally {
        in.close();
      }

      Matrix aI = m.get();
      Matrix omega = new RandomTrinaryMatrix(seed, aI.columnSize(), internalDimension, false);
      Matrix y = aI.times(omega);

      if (y2 == null) {
        y2 = y.transpose().times(y);
      } else {
        y2.assign(y.transpose().times(y), Functions.PLUS);
      }
    }
    r2 = new CholeskyDecomposition(y2);

    // step 2, compute B
    int ncols = 0;
    for (File file : partsOfA) {
      MatrixWritable m = new MatrixWritable();
      DataInputStream in = new DataInputStream(new FileInputStream(file));
      try {
        m.readFields(in);
      } finally {
        in.close();
      }
      Matrix aI = m.get();
      ncols = Math.max(ncols, aI.columnSize());

      Matrix omega = new RandomTrinaryMatrix(seed, aI.numCols(), internalDimension, false);
      for (int j = 0; j < aI.numCols(); j += columnsPerSlice) {
        Matrix yI = aI.times(omega);
        Matrix aIJ = aI.viewPart(0, aI.rowSize(), j, Math.min(columnsPerSlice, aI.columnSize() - j));
        Matrix bIJ = r2.solveRight(yI).transpose().times(aIJ);
        addToSavedCopy(bFile(tmpDir, j), bIJ);
      }
    }

    // step 3, compute BB', L and SVD(L)
    Matrix b2 = new DenseMatrix(internalDimension, internalDimension);
    MatrixWritable bTmp = new MatrixWritable();
    for (int j = 0; j < ncols; j += columnsPerSlice) {
      if (bFile(tmpDir, j).exists()) {
        DataInputStream in = new DataInputStream(new FileInputStream(bFile(tmpDir, j)));
        try {
          bTmp.readFields(in);
        } finally {
          in.close();
        }

        b2.assign(bTmp.get().times(bTmp.get().transpose()), Functions.PLUS);
      }
    }
    l2 = new CholeskyDecomposition(b2);
    svd = new SingularValueDecomposition(l2.getL());
  }
View Full Code Here

Examples of org.apache.mahout.math.MatrixWritable

  public void computeV(File tmpDir, int ncols) throws IOException {
    // step 5, compute pieces of V
    for (int j = 0; j < ncols; j += columnsPerSlice) {
      File bPath = bFile(tmpDir, j);
      if (bPath.exists()) {
        MatrixWritable m = new MatrixWritable();
        DataInputStream in = new DataInputStream(new FileInputStream(bPath));
        try {
          m.readFields(in);
        } finally {
          in.close();
        }
        m.set(l2.solveRight(m.get().transpose()).times(svd.getV()));
        DataOutputStream out = new DataOutputStream(new FileOutputStream(
            new File(tmpDir, String.format("V-%s", bPath.getName().replaceAll(".*-", "")))));
        try {
          m.write(out);
        } finally {
          out.close();
        }
      }
    }
View Full Code Here

Examples of org.apache.mahout.math.MatrixWritable

  }

  public void computeU(Iterable<File> partsOfA, File tmpDir) throws IOException {
    // step 4, compute pieces of U
    for (File file : partsOfA) {
      MatrixWritable m = new MatrixWritable();
      m.readFields(new DataInputStream(new FileInputStream(file)));
      Matrix aI = m.get();

      Matrix y = aI.times(new RandomTrinaryMatrix(seed, aI.numCols(), dim, false));
      Matrix uI = r2.solveRight(y).times(svd.getU());
      m.set(uI);
      DataOutputStream out = new DataOutputStream(new FileOutputStream(
          new File(tmpDir, String.format("U-%s", file.getName().replaceAll(".*-", "")))));
      try {
        m.write(out);
      } finally {
        out.close();
      }
    }
  }
View Full Code Here

Examples of org.apache.mahout.math.MatrixWritable

      }
    }
  }

  private static void addToSavedCopy(File file, Matrix matrix) throws IOException {
    MatrixWritable mw = new MatrixWritable();
    if (file.exists()) {
      DataInputStream in = new DataInputStream(new FileInputStream(file));
      try {
        mw.readFields(in);
      } finally {
        in.close();
      }
      mw.get().assign(matrix, Functions.PLUS);
    } else {
      mw.set(matrix);
    }
    DataOutputStream out = new DataOutputStream(new FileOutputStream(file));
    try {
      mw.write(out);
    } finally {
      out.close();
    }
  }
View Full Code Here

Examples of org.apache.mahout.math.MatrixWritable

   
    Path inverseCovarianceFile = new Path(getTestTempDirPath("mahalanobis"),
        "MahalanobisDistanceMeasureInverseCovarianceFile");
    conf.set("MahalanobisDistanceMeasure.inverseCovarianceFile", inverseCovarianceFile.toString());
    FileSystem fs = FileSystem.get(inverseCovarianceFile.toUri(), conf);
    MatrixWritable inverseCovarianceMatrix = new MatrixWritable(measure.getInverseCovarianceMatrix());
    DataOutputStream out = fs.create(inverseCovarianceFile);
    try {
      inverseCovarianceMatrix.write(out);
    } finally {
      Closeables.close(out, true);
    }
   
    Path meanVectorFile = new Path(getTestTempDirPath("mahalanobis"), "MahalanobisDistanceMeasureMeanVectorFile");
View Full Code Here

Examples of org.apache.mahout.math.MatrixWritable

   
    Path inverseCovarianceFile = new Path(getTestTempDirPath("mahalanobis"),
        "MahalanobisDistanceMeasureInverseCovarianceFile");
    conf.set("MahalanobisDistanceMeasure.inverseCovarianceFile", inverseCovarianceFile.toString());
    FileSystem fs = FileSystem.get(inverseCovarianceFile.toUri(), conf);
    MatrixWritable inverseCovarianceMatrix = new MatrixWritable(measure.getInverseCovarianceMatrix());
    DataOutputStream out = fs.create(inverseCovarianceFile);
    try {
      inverseCovarianceMatrix.write(out);
    } finally {
      Closeables.close(out, false);
    }
   
    Path meanVectorFile = new Path(getTestTempDirPath("mahalanobis"), "MahalanobisDistanceMeasureMeanVectorFile");
View Full Code Here

Examples of org.apache.mahout.math.MatrixWritable

    }
    return 0;
  }
 
  private static void exportText(Path inputPath, PrintStream out) throws IOException {
    MatrixWritable mw = new MatrixWritable();
    Text key = new Text();
    readSeqFile(inputPath, key, mw);
    Matrix m = mw.get();
    ConfusionMatrix cm = new ConfusionMatrix(m);
    out.println(String.format("%-40s", "Label") + TAB_SEPARATOR + String.format("%-10s", "Total")
                + TAB_SEPARATOR + String.format("%-10s", "Correct") + TAB_SEPARATOR
                + String.format("%-6s", "%") + TAB_SEPARATOR);
    out.println(String.format("%-70s", "-").replace(' ', '-'));
View Full Code Here

Examples of org.apache.mahout.math.MatrixWritable

    out.println(String.format("%-70s", "-").replace(' ', '-'));
    out.println(cm.toString());
  }
 
  private static void exportTable(Path inputPath, PrintStream out, boolean wrapHtml) throws IOException {
    MatrixWritable mw = new MatrixWritable();
    Text key = new Text();
    readSeqFile(inputPath, key, mw);
    String fileName = inputPath.getName();
    fileName = fileName.substring(fileName.lastIndexOf('/') + 1, fileName.length());
    Matrix m = mw.get();
    ConfusionMatrix cm = new ConfusionMatrix(m);
    if (wrapHtml) {
      printHeader(out, fileName);
    }
    out.println("<p/>");
View Full Code Here

Examples of org.apache.mahout.math.MatrixWritable

    // initially, we don't know what size buffer to hold
    int nrows = -1;
    int ncols = -1;
    Matrix r = null;

    MatrixWritable m = new MatrixWritable();

    int row = 0;
    for (File file : files) {
      DataInputStream in = new DataInputStream(new FileInputStream(file));
      m.readFields(in);
      in.close();
      if (nrows == -1) {
        // now we can set an upper bound on how large our result will be
        nrows = m.get().rowSize() * files.size();
        ncols = m.get().columnSize();
        r = new DenseMatrix(nrows, ncols);
      }
      r.viewPart(row, m.get().rowSize(), 0, r.columnSize()).assign(m.get());
      row += m.get().rowSize();
    }
    // at the end, row will have the true size of the result
    if (row != nrows && r != null) {
      // and if that isn't the size of the buffer, we need to crop the result a bit
      r = r.viewPart(0, row, 0, ncols);
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.