Examples of MatrixWritable


Examples of org.apache.mahout.math.MatrixWritable

    Matrix v = new RandomTrinaryMatrix(2, columns, rank, false);
    Matrix a = u.times(d).times(v.transpose());

    if (tmpDir != null) {
      for (int i = 0; i < a.rowSize(); i += rowsPerSlice) {
        MatrixWritable m = new MatrixWritable(a.viewPart(i, Math.min(a.rowSize() - i, rowsPerSlice), 0, a.columnSize()));
        DataOutputStream out = new DataOutputStream(new FileOutputStream(new File(tmpDir, String.format("%s-%09d", aBase, i))));
        try {
          m.write(out);
        } finally {
          out.close();
        }
      }
    }
View Full Code Here

Examples of org.apache.mahout.math.MatrixWritable

      ParameteredGeneralizations.configureParameters(this, jobConf);
    }
    try {
      if (inverseCovarianceFile.get() != null) {
        FileSystem fs = FileSystem.get(inverseCovarianceFile.get().toUri(), jobConf);
        MatrixWritable inverseCovarianceMatrix = (MatrixWritable) matrixClass.get().newInstance();
        if (!fs.exists(inverseCovarianceFile.get())) {
          throw new FileNotFoundException(inverseCovarianceFile.get().toString());
        }
        DataInputStream in = fs.open(inverseCovarianceFile.get());
        try {
          inverseCovarianceMatrix.readFields(in);
        } finally {
          in.close();
        }
        this.inverseCovarianceMatrix = inverseCovarianceMatrix.get();
      }
     
      if (meanVectorFile.get() != null) {
        FileSystem fs = FileSystem.get(meanVectorFile.get().toUri(), jobConf);
        VectorWritable meanVector = (VectorWritable) vectorClass.get().newInstance();
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 {
      out.close();
    }

    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 {
      out.close();
    }

    Path meanVectorFile = new Path(getTestTempDirPath("mahalanobis"), "MahalanobisDistanceMeasureMeanVectorFile");
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

  }

  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);
      final 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
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.