List of usage examples for org.apache.mahout.math Vector size
int size();
From source file:at.illecker.hadoop.rootbeer.examples.matrixmultiplication.DistributedRowMatrix.java
License:Apache License
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++;// w ww . ja v a 2 s . c o m System.out.print(e.get() + " "); } System.out.println(); } return count; }
From source file:at.illecker.hadoop.rootbeer.examples.matrixmultiplication.DistributedRowMatrix.java
License:Apache License
public double[][] toDoubleArray() { final double[][] matrix = new double[this.numRows][this.numCols]; Iterator<MatrixSlice> iterator = this.iterateAll(); int i = 0;// ww w . j a v a 2 s . c om while (iterator.hasNext()) { Vector rowVector = iterator.next().vector(); for (int j = 0; j < rowVector.size(); j++) { matrix[i][j] = rowVector.getElement(j).get(); } i++; } return matrix; }
From source file:at.illecker.hadoop.rootbeer.examples.matrixmultiplication.DistributedRowMatrix.java
License:Apache License
public boolean verify(DistributedRowMatrix other) { Iterator<MatrixSlice> iteratorThis = this.iterateAll(); Iterator<MatrixSlice> iteratorOther = other.iterateAll(); while (iteratorThis.hasNext()) { Vector thisVector = iteratorThis.next().vector(); Vector otherVector = iteratorOther.next().vector(); for (int j = 0; j < thisVector.size(); j++) { if (thisVector.getElement(j).get() != otherVector.getElement(j).get()) { // System.out.println("Verify failed!"); // System.out.println(" Vector1: " + thisVector.toString()); // System.out.println(" Vector2: " + otherVector.toString()); return false; }/*from w ww.j a v a 2 s . co m*/ } } return true; }
From source file:at.illecker.hama.rootbeer.examples.matrixmultiplication.compositeinput.cpu.MatrixMultiplicationBSPCpu.java
License:Apache License
@Override public void bsp(BSPPeer<IntWritable, TupleWritable, IntWritable, VectorWritable, MatrixRowMessage> peer) throws IOException, SyncException, InterruptedException { IntWritable key = new IntWritable(); TupleWritable value = new TupleWritable(); while (peer.readNext(key, value)) { // Logging if (isDebuggingEnabled) { for (int i = 0; i < value.size(); i++) { Vector vector = ((VectorWritable) value.get(i)).get(); logger.writeChars("bsp,input,key=" + key + ",value=" + vector.toString() + "\n"); }/*w w w .ja va 2 s. com*/ } Vector firstVector = ((VectorWritable) value.get(0)).get(); Vector secondVector = ((VectorWritable) value.get(1)).get(); // outCardinality is resulting column size n // (l x m) * (m x n) = (l x n) boolean firstIsOutFrag = secondVector.size() == outCardinality; // outFrag is Matrix which has the resulting column cardinality // (matrixB) Vector outFrag = firstIsOutFrag ? secondVector : firstVector; // multiplier is Matrix which has the resulting row count // (transposed matrixA) Vector multiplier = firstIsOutFrag ? firstVector : secondVector; if (isDebuggingEnabled) { logger.writeChars("bsp,firstIsOutFrag=" + firstIsOutFrag + "\n"); logger.writeChars("bsp,outFrag=" + outFrag + "\n"); logger.writeChars("bsp,multiplier=" + multiplier + "\n"); } for (Vector.Element e : multiplier.nonZeroes()) { VectorWritable outVector = new VectorWritable(); // Scalar Multiplication (Vector x Element) outVector.set(outFrag.times(e.get())); peer.send(masterTask, new MatrixRowMessage(e.index(), outVector)); if (isDebuggingEnabled) { logger.writeChars("bsp,send,key=" + e.index() + ",value=" + outVector.get().toString() + "\n"); } } if (isDebuggingEnabled) { logger.flush(); } } peer.sync(); }
From source file:at.illecker.hama.rootbeer.examples.matrixmultiplication.compositeinput.gpu.MatrixMultiplicationBSPGpu.java
License:Apache License
@Override public void bsp(BSPPeer<IntWritable, TupleWritable, IntWritable, VectorWritable, MatrixRowMessage> peer) throws IOException, SyncException, InterruptedException { IntWritable key = new IntWritable(); TupleWritable value = new TupleWritable(); while (peer.readNext(key, value)) { // Logging if (isDebuggingEnabled) { for (int i = 0; i < value.size(); i++) { Vector vector = ((VectorWritable) value.get(i)).get(); logger.writeChars("bsp,input,key=" + key + ",value=" + vector.toString() + "\n"); }//from w ww .ja va 2s .c o m } Vector firstVector = ((VectorWritable) value.get(0)).get(); Vector secondVector = ((VectorWritable) value.get(1)).get(); // outCardinality is resulting column size n // (l x m) * (m x n) = (l x n) boolean firstIsOutFrag = secondVector.size() == outCardinality; // outFrag is Matrix which has the resulting column cardinality // (matrixB) Vector outFrag = firstIsOutFrag ? secondVector : firstVector; // multiplier is Matrix which has the resulting row count // (transposed matrixA) Vector multiplier = firstIsOutFrag ? firstVector : secondVector; if (isDebuggingEnabled) { logger.writeChars("bsp,firstIsOutFrag=" + firstIsOutFrag + "\n"); logger.writeChars("bsp,outFrag=" + outFrag + "\n"); logger.writeChars("bsp,multiplier=" + multiplier + "\n"); } // outFrag to double[] double[] outFragArray = new double[outFrag.size()]; int i = 0; for (Vector.Element e : outFrag.all()) { outFragArray[i] = e.get(); i++; } // One map task consists of multiple kernels within one block // Each kernel computes a scalar multiplication blockSize = multiplier.size(); gridSize++; for (int j = 0; j < blockSize; j++) { kernels.add(new MatrixMultiplicationBSPKernel(j, multiplier.get(j), outFragArray)); } // Run GPU Kernels Rootbeer rootbeer = new Rootbeer(); Context context = rootbeer.createDefaultContext(); Stopwatch watch = new Stopwatch(); watch.start(); // blockSize = rows of Matrix A (multiplier) // gridSize = cols of Matrix B (for each row a scalar multiplication // has to be made) rootbeer.run(kernels, new ThreadConfig(blockSize, gridSize, kernels.size()), context); watch.stop(); List<StatsRow> stats = context.getStats(); for (StatsRow row : stats) { System.out.println(" StatsRow:\n"); System.out.println(" serial time: " + row.getSerializationTime() + "\n"); System.out.println(" exec time: " + row.getExecutionTime() + "\n"); System.out.println(" deserial time: " + row.getDeserializationTime() + "\n"); System.out.println(" num blocks: " + row.getNumBlocks() + "\n"); System.out.println(" num threads: " + row.getNumThreads() + "\n"); } if (isDebuggingEnabled) { logger.writeChars( "bsp,KernelCount=" + kernels.size() + ",GPUTime=" + watch.elapsedTimeMillis() + "ms\n"); logger.writeChars("bps,blockSize=" + blockSize + ",gridSize=" + gridSize + "\n"); logger.flush(); } // Collect results of GPU kernels for (Kernel kernel : kernels) { MatrixMultiplicationBSPKernel bspKernel = (MatrixMultiplicationBSPKernel) kernel; if (isDebuggingEnabled) { logger.writeChars("bsp,thread_idxx=" + bspKernel.thread_idxx + ",multiplier=" + bspKernel.multiplierVal + ",vector=" + Arrays.toString(bspKernel.vectorVal) + "\n"); } peer.send(masterTask, new MatrixRowMessage(bspKernel.row, new VectorWritable(new DenseVector(bspKernel.results)))); if (isDebuggingEnabled) { logger.writeChars("bsp,send,key=" + bspKernel.row + ",value=" + Arrays.toString(bspKernel.results) + "\n"); } } } peer.sync(); }
From source file:at.illecker.hama.rootbeer.examples.matrixmultiplication.compositeinput.util.DistributedRowMatrix.java
License:Apache License
public boolean verify(DistributedRowMatrix other) { Iterator<MatrixSlice> iteratorThis = this.iterateAll(); Iterator<MatrixSlice> iteratorOther = other.iterateAll(); while (iteratorThis.hasNext()) { Vector thisVector = iteratorThis.next().vector(); Vector otherVector = iteratorOther.next().vector(); if (thisVector.size() != otherVector.size()) { return false; }//from w w w .j ava 2 s. c o m for (int j = 0; j < thisVector.size(); j++) { if (thisVector.getElement(j).get() != otherVector.getElement(j).get()) { // System.out.println("Verify failed!"); // System.out.println(" Vector1: " + thisVector.toString()); // System.out.println(" Vector2: " + otherVector.toString()); return false; } } } return true; }
From source file:com.chimpler.example.eigenface.Helper.java
License:Apache License
public static double[][] readMatrixSequenceFile(String fileName) throws Exception { Configuration configuration = new Configuration(); FileSystem fs = FileSystem.get(configuration); Reader matrixReader = new SequenceFile.Reader(fs, new Path(fileName), configuration); List<double[]> rows = new ArrayList<double[]>(); IntWritable key = new IntWritable(); VectorWritable value = new VectorWritable(); while (matrixReader.next(key, value)) { Vector vector = value.get(); double[] row = new double[vector.size()]; for (int i = 0; i < vector.getNumNondefaultElements(); i++) { Element element = vector.getElement(i); row[element.index()] = element.get(); }/*from w w w .j a v a 2s .co m*/ rows.add(row); } return rows.toArray(new double[rows.size()][]); }
From source file:com.cloudera.science.ml.core.vectors.Vectors.java
License:Open Source License
/** * Converts the given {@code Vector} into a {@code double[]}. * //from ww w . j a v a 2 s.c om * @param v The vector to convert * @return The resulting array of values */ public static double[] toArray(Vector v) { double[] ret = new double[v.size()]; for (int i = 0; i < ret.length; i++) { ret[i] = v.getQuick(i); } return ret; }
From source file:com.cloudera.science.ml.kmeans.parallel.CentersIndex.java
License:Open Source License
private BitSet index(Vector vec) { double[] prod = new double[projectionBits]; if (vec.isDense()) { for (int i = 0; i < vec.size(); i++) { double v = vec.getQuick(i); if (v != 0.0) { for (int j = 0; j < projectionBits; j++) { prod[j] += v * projection[i + j * dimensions]; }/*from ww w .j a v a 2 s . c o m*/ } } } else { Iterator<Vector.Element> iter = vec.iterateNonZero(); while (iter.hasNext()) { Vector.Element e = iter.next(); for (int j = 0; j < projectionBits; j++) { prod[j] = e.get() * projection[e.index() + j * dimensions]; } } } BitSet bitset = new BitSet(projectionBits); for (int i = 0; i < projectionBits; i++) { if (prod[i] > 0.0) { bitset.set(i); } } return bitset; }
From source file:com.elex.dmp.core.TopicModel.java
License:Apache License
public TopicModel(Matrix topicTermCounts, Vector topicSums, double eta, double alpha, String[] dictionary, int numThreads, double modelWeight) { this.dictionary = dictionary; this.topicTermCounts = topicTermCounts; this.topicSums = topicSums; this.numTopics = topicSums.size(); this.numTerms = topicTermCounts.numCols(); this.eta = eta; this.alpha = alpha; this.sampler = new Sampler(RandomUtils.getRandom()); this.numThreads = numThreads; if (modelWeight != 1) { topicSums.assign(Functions.mult(modelWeight)); for (int x = 0; x < numTopics; x++) { topicTermCounts.viewRow(x).assign(Functions.mult(modelWeight)); }/*from w ww. j a v a 2 s. c o m*/ } initializeThreadPool(); }