List of usage examples for org.apache.mahout.math Vector getQuick
double getQuick(int index);
From source file:org.qcri.sparkpca.PCAUtils.java
/*** * Mi = (Yi-Ym)' x (Xi-Xm) = Yi' x (Xi-Xm) - Ym' x (Xi-Xm) * //from ww w . ja v a 2 s . com * M = Sum(Mi) = Sum(Yi' x (Xi-Xm)) - Ym' x (Sum(Xi)-N*Xm) * * The second part is done in this function */ public static Matrix updateXtXAndYtx(Matrix realCentralYtx, Vector realCentralSumX, Vector ym, Vector xm, int nRows) { for (int yRow = 0; yRow < ym.size(); yRow++) { double scale = ym.getQuick(yRow); for (int xCol = 0; xCol < realCentralSumX.size(); xCol++) { double centeredValue = realCentralSumX.getQuick(xCol) - nRows * xm.getQuick(xCol); double currValue = realCentralYtx.getQuick(yRow, xCol); currValue -= centeredValue * scale; realCentralYtx.setQuick(yRow, xCol, currValue); } } return realCentralYtx; }
From source file:org.trustedanalytics.atk.giraph.algorithms.lbp.LoopyBeliefPropagationComputation.java
License:Apache License
/** * Initialize vertex//w w w. j a v a 2 s . c om * * @param vertex of the graph */ private void initializeVertex(Vertex<LongWritable, VertexData4LBPWritable, DoubleWritable> vertex) { // normalize prior and posterior Vector prior = vertex.getValue().getPriorVector(); Vector posterior = vertex.getValue().getPosteriorVector(); int nStates = prior.size(); double sum = 0d; for (int i = 0; i < nStates; i++) { double v = prior.getQuick(i); if (v < 0d) { throw new IllegalArgumentException("Vertex ID: " + vertex.getId() + " has negative prior value."); } else if (v < MIN_PRIOR_VALUE) { v = MIN_PRIOR_VALUE; prior.setQuick(i, v); } sum += v; } for (int i = 0; i < nStates; i++) { posterior.setQuick(i, prior.getQuick(i) / sum); prior.setQuick(i, Math.log(posterior.getQuick(i))); } // collect graph statistics VertexType vt = vertex.getValue().getType(); vt = ignoreVertexType ? VertexType.TRAIN : vt; switch (vt) { case TRAIN: aggregate(SUM_TRAIN_VERTICES, new LongWritable(1)); break; case VALIDATE: aggregate(SUM_VALIDATE_VERTICES, new LongWritable(1)); break; case TEST: aggregate(SUM_TEST_VERTICES, new LongWritable(1)); break; default: throw new IllegalArgumentException("Unknown vertex type: " + vt.toString()); } // if it's not a training vertex, use uniform posterior and don't send out messages if (vt != VertexType.TRAIN) { posterior.assign(1.0 / nStates); return; } // calculate messages IdWithVectorMessage newMessage = new IdWithVectorMessage(); newMessage.setData(vertex.getId().get()); // calculate initial belief Vector belief = prior.clone(); for (Edge<LongWritable, DoubleWritable> edge : vertex.getEdges()) { double weight = edge.getValue().get(); if (weight <= 0d) { throw new IllegalArgumentException("Vertex ID: " + vertex.getId() + " has an edge with negative or zero weight value " + weight); } for (int i = 0; i < nStates; i++) { sum = 0d; for (int j = 0; j < nStates; j++) { double msg = Math.exp( prior.getQuick(j) + edgePotential(Math.abs(i - j) / (double) (nStates - 1), weight)); if (maxProduct) { sum = sum > msg ? sum : msg; } else { sum += msg; } } belief.setQuick(i, sum > 0d ? Math.log(sum) : Double.MIN_VALUE); } belief = belief.plus(-belief.maxValue()); // send out messages newMessage.setVector(belief); sendMessage(edge.getTargetVertexId(), newMessage); } }
From source file:org.trustedanalytics.atk.giraph.algorithms.lbp.LoopyBeliefPropagationComputation.java
License:Apache License
@Override public void compute(Vertex<LongWritable, VertexData4LBPWritable, DoubleWritable> vertex, Iterable<IdWithVectorMessage> messages) throws IOException { long step = getSuperstep(); if (step == 0) { initializeVertex(vertex);/*from w ww. j a v a2 s.com*/ return; } // collect messages sent to this vertex HashMap<Long, Vector> map = new HashMap<Long, Vector>(); for (IdWithVectorMessage message : messages) { map.put(message.getData(), message.getVector()); } // update posterior according to prior and messages VertexData4LBPWritable vertexValue = vertex.getValue(); VertexType vt = vertexValue.getType(); vt = ignoreVertexType ? VertexType.TRAIN : vt; Vector prior = vertexValue.getPriorVector(); double nStates = prior.size(); if (vt != VertexType.TRAIN) { // assign a uniform prior for validate/test vertex prior = prior.clone().assign(Math.log(1.0 / nStates)); } // sum of prior and messages Vector sumPosterior = prior; for (IdWithVectorMessage message : messages) { sumPosterior = sumPosterior.plus(message.getVector()); } sumPosterior = sumPosterior.plus(-sumPosterior.maxValue()); // update posterior if this isn't an anchor vertex if (prior.maxValue() < anchorThreshold) { // normalize posterior Vector posterior = sumPosterior.clone().assign(Functions.EXP); posterior = posterior.normalize(1d); Vector oldPosterior = vertexValue.getPosteriorVector(); double delta = posterior.minus(oldPosterior).norm(1d); // aggregate deltas switch (vt) { case TRAIN: aggregate(SUM_TRAIN_DELTA, new DoubleWritable(delta)); break; case VALIDATE: aggregate(SUM_VALIDATE_DELTA, new DoubleWritable(delta)); break; case TEST: aggregate(SUM_TEST_DELTA, new DoubleWritable(delta)); break; default: throw new IllegalArgumentException("Unknown vertex type: " + vt.toString()); } // update posterior vertexValue.setPosteriorVector(posterior); } if (step < maxSupersteps) { // if it's not a training vertex, don't send out messages if (vt != VertexType.TRAIN) { return; } IdWithVectorMessage newMessage = new IdWithVectorMessage(); newMessage.setData(vertex.getId().get()); // update belief Vector belief = prior.clone(); for (Edge<LongWritable, DoubleWritable> edge : vertex.getEdges()) { double weight = edge.getValue().get(); long id = edge.getTargetVertexId().get(); Vector tempVector = sumPosterior; if (map.containsKey(id)) { tempVector = sumPosterior.minus(map.get(id)); } for (int i = 0; i < nStates; i++) { double sum = 0d; for (int j = 0; j < nStates; j++) { double msg = Math.exp( tempVector.getQuick(j) + edgePotential(Math.abs(i - j) / (nStates - 1), weight)); if (maxProduct) { sum = sum > msg ? sum : msg; } else { sum += msg; } } belief.setQuick(i, sum > 0d ? Math.log(sum) : Double.MIN_VALUE); } belief = belief.plus(-belief.maxValue()); newMessage.setVector(belief); sendMessage(edge.getTargetVertexId(), newMessage); } } else { // convert prior back to regular scale before output prior = vertexValue.getPriorVector(); prior = prior.assign(Functions.EXP); vertexValue.setPriorVector(prior); vertex.voteToHalt(); } }
From source file:org.trustedanalytics.atk.giraph.algorithms.lp.LabelPropagationComputation.java
License:Apache License
@Override public void compute(Vertex<LongWritable, VertexData4LPWritable, DoubleWritable> vertex, Iterable<IdWithVectorMessage> messages) throws IOException { long superStep = getSuperstep(); if (superStep == 0) { initializeVertexEdges(vertex);/*from www. jav a 2 s. co m*/ vertex.voteToHalt(); } else if (superStep <= maxSupersteps) { VertexData4LPWritable vertexValue = vertex.getValue(); Vector prior = vertexValue.getPriorVector(); Vector posterior = vertexValue.getPosteriorVector(); double degree = vertexValue.getDegree(); // collect messages sent to this vertex HashMap<Long, Vector> map = new HashMap(); for (IdWithVectorMessage message : messages) { map.put(message.getData(), message.getVector()); } // Update belief and calculate cost double hi = prior.getQuick(0); double fi = posterior.getQuick(0); double crossSum = 0d; Vector newBelief = posterior.clone().assign(0d); for (Edge<LongWritable, DoubleWritable> edge : vertex.getEdges()) { double weight = edge.getValue().get(); if (weight <= 0d) { throw new IllegalArgumentException( "Vertex ID: " + vertex.getId() + "has an edge with negative or zero value"); } long targetVertex = edge.getTargetVertexId().get(); if (map.containsKey(targetVertex)) { Vector tempVector = map.get(targetVertex); newBelief = newBelief.plus(tempVector.times(weight)); double fj = tempVector.getQuick(0); crossSum += weight * fi * fj; } } double cost = degree * ((1 - lambda) * (Math.pow(fi, 2) - crossSum) + 0.5 * lambda * Math.pow((fi - hi), 2)); aggregate(SUM_COST, new DoubleWritable(cost)); // Update posterior if the vertex was not processed if (vertexValue.wasLabeled() == false) { newBelief = (newBelief.times(1 - lambda).plus(prior.times(lambda))).normalize(1d); vertexValue.setPosteriorVector(newBelief); } // Send out messages if not the last step if (superStep != maxSupersteps) { IdWithVectorMessage newMessage = new IdWithVectorMessage(vertex.getId().get(), vertexValue.getPosteriorVector()); sendMessageToAllEdges(vertex, newMessage); } } vertex.voteToHalt(); }