List of usage examples for org.apache.mahout.math Vector minus
Vector minus(Vector x);
From source file:org.trustedanalytics.atk.giraph.algorithms.lda.CVB0LDAComputation.java
License:Apache License
/** * Update edge value according to vertex and messages * * @param vertex of the graph/*from w ww . j a v a2s .co m*/ * @param map of type HashMap */ private void updateEdge(Vertex<LdaVertexId, LdaVertexData, LdaEdgeData> vertex, HashMap<LdaVertexId, Vector> map) { Vector vector = vertex.getValue().getLdaResult(); double maxDelta = 0d; for (Edge<LdaVertexId, LdaEdgeData> edge : vertex.getMutableEdges()) { Vector gamma = edge.getValue().getVector(); LdaVertexId id = edge.getTargetVertexId(); if (map.containsKey(id)) { Vector otherVector = map.get(id); Vector newGamma = null; if (vertex.getId().isDocument()) { newGamma = vector.minus(gamma).plus(config.alpha()) .times(otherVector.minus(gamma).plus(config.beta())) .times(nk.minus(gamma).plus(numWords * config.beta()).assign(Functions.INV)); } else { newGamma = vector.minus(gamma).plus(config.beta()) .times(otherVector.minus(gamma).plus(config.alpha())) .times(nk.minus(gamma).plus(numWords * config.beta()).assign(Functions.INV)); } newGamma = newGamma.normalize(1d); double delta = gamma.minus(newGamma).norm(1d) / config.numTopics(); if (delta > maxDelta) { maxDelta = delta; } // update edge vector edge.getValue().setVector(newGamma); } else { // this happens when you don't have your Vertex Id's being setup correctly throw new IllegalArgumentException( String.format("Vertex ID %s: A message is mis-matched.", vertex.getId())); } } aggregate(MAX_DELTA, new DoubleWritable(maxDelta)); }
From source file:org.trustedanalytics.atk.giraph.algorithms.lda.GiraphLdaComputation.java
License:Apache License
/** * Update vertex and outgoing edge values using current vertex values and messages * * @param vertex of the graph//from w w w . j a va 2s . c o m * @param map Map of vertices */ private void updateVertex(Vertex<LdaVertexId, LdaVertexData, LdaEdgeData> vertex, HashMap<LdaVertexId, Vector> map) { Vector vector = vertex.getValue().getLdaResult(); Vector updatedVector = vertex.getValue().getLdaResult().clone().assign(0d); double maxDelta = 0d; for (Edge<LdaVertexId, LdaEdgeData> edge : vertex.getMutableEdges()) { Vector gamma = edge.getValue().getVector(); LdaVertexId id = edge.getTargetVertexId(); if (map.containsKey(id)) { Vector otherVector = map.get(id); Vector newGamma = null; if (vertex.getId().isDocument()) { newGamma = vector.minus(gamma).plus(config.alpha()) .times(otherVector.minus(gamma).plus(config.beta())) .times(nk.minus(gamma).plus(numWords * config.beta()).assign(Functions.INV)); } else { newGamma = vector.minus(gamma).plus(config.beta()) .times(otherVector.minus(gamma).plus(config.alpha())) .times(nk.minus(gamma).plus(numWords * config.beta()).assign(Functions.INV)); } newGamma = newGamma.normalize(1d); double delta = gamma.minus(newGamma).norm(1d) / config.numTopics(); if (delta > maxDelta) { maxDelta = delta; } // update edge vector edge.getValue().setVector(newGamma); } else { // this happens when you don't have your Vertex Id's being setup correctly throw new IllegalArgumentException( String.format("Vertex ID %s: A message is mis-matched.", vertex.getId())); } updatedVector = updateVector(updatedVector, edge); } vertex.getValue().setLdaResult(updatedVector); aggregateWord(vertex); aggregate(MAX_DELTA, new DoubleWritable(maxDelta)); }