List of usage examples for org.apache.mahout.math Vector norm
double norm(double power);
From source file:org.trustedanalytics.atk.giraph.algorithms.lda.CVB0LDAComputation.java
License:Apache License
/** * Initialize vertex/edges, collect graph statistics and send out messages * * @param vertex of the graph/*from w w w .ja va2 s . c o m*/ */ private void initialize(Vertex<LdaVertexId, LdaVertexData, LdaEdgeData> vertex) { // initialize vertex vector, i.e., the theta for doc and phi for word in LDA double[] vertexValues = new double[config.numTopics()]; vertex.getValue().setLdaResult(new DenseVector(vertexValues)); // initialize edge vector, i.e., the gamma in LDA Random rand1 = new Random(vertex.getId().seed()); long seed1 = rand1.nextInt(); double maxDelta = 0d; double sumWeights = 0d; for (Edge<LdaVertexId, LdaEdgeData> edge : vertex.getMutableEdges()) { double weight = edge.getValue().getWordCount(); // generate the random seed for this edge Random rand2 = new Random(edge.getTargetVertexId().seed()); long seed2 = rand2.nextInt(); long seed = seed1 + seed2; Random rand = new Random(seed); double[] edgeValues = new double[config.numTopics()]; for (int i = 0; i < config.numTopics(); i++) { edgeValues[i] = rand.nextDouble(); } Vector vector = new DenseVector(edgeValues); vector = vector.normalize(1d); edge.getValue().setVector(vector); // find the max delta among all edges double delta = vector.norm(1d) / config.numTopics(); if (delta > maxDelta) { maxDelta = delta; } // the sum of weights from all edges sumWeights += weight; } // update vertex value updateVertex(vertex); // aggregate max delta value aggregate(MAX_DELTA, new DoubleWritable(maxDelta)); // collect graph statistics if (vertex.getId().isDocument()) { aggregate(SUM_DOC_VERTEX_COUNT, new LongWritable(1)); } else { aggregate(SUM_OCCURRENCE_COUNT, new DoubleWritable(sumWeights)); aggregate(SUM_WORD_VERTEX_COUNT, new LongWritable(1)); } // send out messages LdaMessage newMessage = new LdaMessage(vertex.getId().copy(), vertex.getValue().getLdaResult()); sendMessageToAllEdges(vertex, newMessage); }
From source file:org.trustedanalytics.atk.giraph.algorithms.lda.GiraphLdaComputation.java
License:Apache License
/** * Initialize vertex/edges, collect graph statistics and send out messages * * @param vertex of the graph//from www . ja v a 2s .c o m */ private void initialize(Vertex<LdaVertexId, LdaVertexData, LdaEdgeData> vertex) { // initialize vertex vector, i.e., the theta for doc and phi for word in LDA double[] vertexValues = new double[config.numTopics()]; vertex.getValue().setLdaResult(new DenseVector(vertexValues)); Vector updatedVector = vertex.getValue().getLdaResult().clone().assign(0d); // initialize edge vector, i.e., the gamma in LDA Random rand1 = new Random(vertex.getId().seed()); long seed1 = rand1.nextInt(); double maxDelta = 0d; double sumWeights = 0d; for (Edge<LdaVertexId, LdaEdgeData> edge : vertex.getMutableEdges()) { double weight = edge.getValue().getWordCount(); // generate the random seed for this edge Random rand2 = new Random(edge.getTargetVertexId().seed()); long seed2 = rand2.nextInt(); long seed = seed1 + seed2; Random rand = new Random(seed); double[] edgeValues = new double[config.numTopics()]; for (int i = 0; i < config.numTopics(); i++) { edgeValues[i] = rand.nextDouble(); } Vector vector = new DenseVector(edgeValues); vector = vector.normalize(1d); edge.getValue().setVector(vector); // find the max delta among all edges double delta = vector.norm(1d) / config.numTopics(); if (delta > maxDelta) { maxDelta = delta; } // the sum of weights from all edges sumWeights += weight; updatedVector = updateVector(updatedVector, edge); } // update vertex value vertex.getValue().setLdaResult(updatedVector); ; // aggregate max delta value aggregateWord(vertex); aggregate(MAX_DELTA, new DoubleWritable(maxDelta)); // collect graph statistics if (vertex.getId().isDocument()) { aggregate(SUM_DOC_VERTEX_COUNT, new LongWritable(1)); } else { aggregate(SUM_OCCURRENCE_COUNT, new DoubleWritable(sumWeights)); aggregate(SUM_WORD_VERTEX_COUNT, new LongWritable(1)); } // send out messages LdaMessage newMessage = new LdaMessage(vertex.getId().copy(), vertex.getValue().getLdaResult()); sendMessageToAllEdges(vertex, newMessage); }
From source file:zx.soft.mahout.knn.search.AbstractSearchTest.java
License:Apache License
@Test public void testNearMatch() { List<MatrixSlice> queries = Lists.newArrayList(Iterables.limit(testData(), 100)); Searcher s = getSearch(20);/* w w w. j av a2 s . c om*/ s.addAllMatrixSlicesAsWeightedVectors(testData()); MultiNormal noise = new MultiNormal(0.01, new DenseVector(20)); for (MatrixSlice slice : queries) { Vector query = slice.vector(); final Vector epsilon = noise.sample(); // List<WeightedThing<Vector>> r0 = s.search(query, 2); query = query.plus(epsilon); List<WeightedThing<Vector>> r = s.search(query, 2); r = s.search(query, 2); assertEquals("Distance has to be small", epsilon.norm(2), r.get(0).getWeight(), 1e-5); assertEquals("Answer must be substantially the same as query", epsilon.norm(2), r.get(0).getValue().minus(query).norm(2), 1e-5); assertTrue("Wrong answer must be further away", r.get(1).getWeight() > r.get(0).getWeight()); } }