Package edu.cmu.graphchi.preprocessing

Examples of edu.cmu.graphchi.preprocessing.VertexIdTranslate


        logger.info("Ready.");

        /* Output results */
        int i = 0;
        VertexIdTranslate trans = engine.getVertexIdTranslate();
        TreeSet<IdFloat> top20 = Toplist.topListFloat(baseFilename, engine.numVertices(), 20);
        for(IdFloat vertexRank : top20) {
            System.out.println(++i + ": " + trans.backward(vertexRank.getVertexId()) + " = " + vertexRank.getValue());
        }
    }
View Full Code Here


        int nTop = 100;
        companion.outputDistributions(baseFilename + "_ppr_" + firstSource + "_"
                + (firstSource + numSources - 1) + ".top" + nTop, nTop);

        /* For debug */
        VertexIdTranslate vertexIdTranslate = this.drunkardMobEngine.getVertexIdTranslate();
        IdCount[] topForFirst = companion.getTop(firstSource, 10);

        System.out.println("Top visits from source vertex " + vertexIdTranslate.forward(firstSource) + " (internal id=" + firstSource + ")");
        for(IdCount idc : topForFirst) {
            System.out.println(vertexIdTranslate.backward(idc.id) + ": " + idc.count);
        }

        /* If local, shutdown the companion */
        if (companion instanceof DrunkardCompanion) {
            ((DrunkardCompanion) companion).close();
View Full Code Here

        /* First read the original matrix dimensions */
        BipartiteGraphInfo graphInfo = getGraphInfo();
        int numLeft = graphInfo.getNumLeft();
        int numRight = graphInfo.getNumRight();

        VertexIdTranslate vertexIdTranslate =
                VertexIdTranslate.fromFile(new File(ChiFilenames.getVertexTranslateDefFile(baseFilename, numShards)));

        /* Output left */
        String leftFileName = baseFilename + "_U.mm";
        BufferedWriter wr = new BufferedWriter(new FileWriter(leftFileName));
        wr.write("%%MatrixMarket matrix array real general\n");
        wr.write(this.D + " " + numLeft + "\n");

        for(int j=0; j < numLeft; j++) {
            int vertexId = vertexIdTranslate.forward(j)// Translate to internal vertex id
            for(int i=0; i < D; i++) {
                wr.write(vertexValueMatrix.getValue(vertexId, i) + "\n");
            }
        }
        wr.close();

        /* Output right */
        String rightFileName = baseFilename + "_V.mm";
        wr = new BufferedWriter(new FileWriter(rightFileName));
        wr.write("%%MatrixMarket matrix array real general\n");
        wr.write(this.D + " " + numRight + "\n");

        for(int j=0; j < numRight; j++) {
            int vertexId = vertexIdTranslate.forward(numLeft + j);   // Translate to internal vertex id
            for(int i=0; i < D; i++) {
                wr.write(vertexValueMatrix.getValue(vertexId, i) + "\n");
            }
        }
        wr.close();
View Full Code Here

        drunkardMobEngine.setEdataConverter(new FloatConverter());

        /* Create list of user vertices (i.e vertices on left). But we need to find their internal ids. */
        ALSMatrixFactorization.BipartiteGraphInfo graphInfo = als.getGraphInfo();
        VertexIdTranslate vertexIdTranslate = drunkardMobEngine.getVertexIdTranslate();
        ArrayList<Integer> userVertices = new ArrayList<Integer>(graphInfo.getNumLeft());

        int numUsers = 50000; // NOTE: hard-coded
        int walksPerSource = 1000;

        if (numUsers > graphInfo.getNumLeft())  graphInfo.getNumLeft();
        logger.info("Compute predictions for first " + numUsers + " users");

        for(int i=0; i< numUsers; i++) {
            userVertices.add(vertexIdTranslate.forward(i));
        }

        /* Configure */
        positiveJob.configureWalkSources(userVertices, walksPerSource);
        negativeJob.configureWalkSources(userVertices, walksPerSource);

       /* Run */
        drunkardMobEngine.run(6);


        /* TODO: handle results */
        for(int i=0; i< 500; i++) {
            int userId = vertexIdTranslate.forward(i);
            IdCount[] posTop = positiveJob.getCompanion().getTop(userId, 20);
            IdCount[] negTop =  negativeJob.getCompanion().getTop(userId, 20);

            double sumEstimatePos = 0.0;
            double sumEstimateNeg = 0.0;

            int n = Math.min(posTop.length, negTop.length);
            for(int j=0; j<n; j++) {
                sumEstimatePos += als.predict(userId, posTop[j].id);
                sumEstimateNeg += als.predict(userId, negTop[j].id);
            }


            long t = System.currentTimeMillis();
            // Compute all
            double allSum = 0;
            int numMovies = graphInfo.getNumRight();
            for(int m=0; m < numMovies; m++) {
                int movieId = vertexIdTranslate.forward(graphInfo.getNumLeft() + m);
                allSum += als.predict(userId, movieId);
            }

            System.out.println(i + " avg pos: " + sumEstimatePos / n + "; avg neg: " + sumEstimateNeg / n + "; all="
                    + allSum / graphInfo.getNumRight() + " (" + (System.currentTimeMillis() - t) + " ms for " + numMovies + " movies");
View Full Code Here

        logger.info("Ready.");

        /* Output results */
        int i = 0;
        VertexIdTranslate trans = engine.getVertexIdTranslate();
        TreeSet<IdFloat> top20 = Toplist.topListFloat(baseFilename, engine.numVertices(), 20);
        for(IdFloat vertexRank : top20) {
            System.out.println(++i + ": " + trans.backward(vertexRank.getVertexId()) + " = " + vertexRank.getValue());
        }
    }
View Full Code Here

        String graphName = args[0];
        int nShards = Integer.parseInt(args[1]);

        CircleOfTrustSalsa csalsa = new CircleOfTrustSalsa(new VertexQuery(graphName, nShards), 10000);

        VertexIdTranslate vertexTrans = VertexIdTranslate.fromFile(new File(ChiFilenames.getVertexTranslateDefFile(graphName, nShards)));

        BufferedReader cmdIn = new BufferedReader(new InputStreamReader(System.in));
        while(true) {
            System.out.print("Enter vertex id to query >> :: ");
            String ln = cmdIn.readLine();
            int vertex = Integer.parseInt(ln);

            // Circle of trust is just the vertex's followers for now
            HashSet<Integer> circle = csalsa.queryService.queryOutNeighbors(vertexTrans.forward(vertex));

            int maxCircleSize = 300;
            // max 500
            if (circle.size() > maxCircleSize) {
                int[] all = new int[circle.size()];
                int i = 0;
                for(Integer v : circle) all[i++] = v;
                HashSet<Integer> filteredCircle = new HashSet<Integer>();
                Random r  = new Random(260379);
                for(i=0; i < maxCircleSize; i++) filteredCircle.add(all[Math.abs(r.nextInt()) % all.length]);
                circle = filteredCircle;
            }

            csalsa.initializeGraph(circle);

            long t = System.currentTimeMillis();
            csalsa.computeSALSA(3);
            logger.info("SALSA computation took " + (System.currentTimeMillis() - t) + "ms");

            circle.add(vertexTrans.forward(vertex));
            ArrayList<SalsaVertex> top = csalsa.topAuthorities(20, circle);
            int j = 1;
            for(SalsaVertex sv : top) {
                int originalId = vertexTrans.backward(sv.id);
                logger.info("Top " + (j++) + " = " + originalId + " " + csalsa.namify(originalId) + " (" + sv.value + ")");
            }

        }
    }
View Full Code Here

    @Override
    public void update(ChiVertex<Integer, Float> vertex, GraphChiContext context) {
        if (vertex.numEdges() == 0) return;


        VertexIdTranslate idTranslate = context.getVertexIdTranslate();

        for(int side=LEFTSIDE; side<=RIGHTSIDE; side++) {
            /* The latent factors for both sides of the graph are kept in memory,
               but in separate matrices. This chooses which one matrix has the value
               of the vertex in question, and which has neighbors.
             */
            HugeDoubleMatrix thisSideMatrix = (side == LEFTSIDE ? leftSideMatrix : rightSideMatrix);
            HugeDoubleMatrix otherSideMatrix = (side == LEFTSIDE ? rightSideMatrix : leftSideMatrix);

            /* Check if this vertex is active on the given side (left or right) */
            if (side == LEFTSIDE && vertex.numOutEdges() == 0) continue;
            if (side == RIGHTSIDE && vertex.numInEdges() == 0) continue;

            /* Start computing the new factor */
            RealMatrix XtX = new BlockRealMatrix(D, D);
            RealVector Xty = new ArrayRealVector(D);

            try {
                double[] neighborLatent = new double[D];

                int ne = (side == LEFTSIDE ? vertex.numOutEdges() : vertex.numInEdges());
                // Compute XtX and Xty (NOTE: unweighted)
                for(int e=0; e < ne; e++) {
                    ChiEdge<Float> edge = (side == LEFTSIDE ? vertex.outEdge(e) : vertex.inEdge(e));
                    float observation = edge.getValue();
                    if (observation < 1.0) throw new RuntimeException("Had invalid observation: " + observation + " on edge " + idTranslate.backward(vertex.getId()) + "->" +
                                idTranslate.backward(edge.getVertexId()));
                    otherSideMatrix.getRow(idTranslate.backward(edge.getVertexId()), neighborLatent);

                    for(int i=0; i < D; i++) {
                        Xty.setEntry(i, Xty.getEntry(i) + neighborLatent[i] * observation);
                        for(int j=i; j < D; j++) {
                            XtX.setEntry(j,i, XtX.getEntry(j, i) + neighborLatent[i] * neighborLatent[j]);
                        }
                    }
                }

                // Symmetrize
                for(int i=0; i < D; i++) {
                    for(int j=i+1; j< D; j++) XtX.setEntry(i,j, XtX.getEntry(j, i));
                }

                // Diagonal -- add regularization
                for(int i=0; i < D; i++) XtX.setEntry(i, i, XtX.getEntry(i, i) + LAMBDA * vertex.numEdges());

                // Solve the least-squares optimization using Cholesky Decomposition
                RealVector newLatentFactor = new CholeskyDecompositionImpl(XtX).getSolver().solve(Xty);

                // Set the new latent factor for this vector
                for(int i=0; i < D; i++) {
                    thisSideMatrix.setValue(idTranslate.backward(vertex.getId()), i, newLatentFactor.getEntry(i));
                }

                if (context.isLastIteration() && side == RIGHTSIDE) {
                    /* On the last iteration - compute the RMSE error. But only for
                      vertices on the right side of the matrix, i.e vectors
                      that have only in-edges.
                    */
                    if (vertex.numInEdges() > 0) {
                        // Sanity check
                        double squaredError = 0;
                        for(int e=0; e < vertex.numInEdges(); e++) {
                            // Compute RMSE
                            ChiEdge<Float> edge = vertex.inEdge(e);
                            float observation = edge.getValue();
                            otherSideMatrix.getRow(idTranslate.backward(edge.getVertexId()), neighborLatent);
                            double prediction = new ArrayRealVector(neighborLatent).dotProduct(newLatentFactor);
                            squaredError += (prediction - observation) * (prediction - observation);
                        }

                        synchronized (this) {
View Full Code Here

        /* Outputting Core Values */
        startWriting(new File(outputDirectory + "out-cores-" + fileName), false);
        bw.write(nVertexes + "\n");

        VertexIdTranslate trans = engine.getVertexIdTranslate();
        TreeSet<IdInt> topToBottom = Toplist.topListInt(inputFilePath,
                engine.numVertices(), engine.numVertices());

        for(IdInt walker : topToBottom) {
            float coreValue = walker.getValue();
            bw.write(trans.backward(walker.getVertexId()) + ", " + String.valueOf((int)coreValue) + "\n");
        }

        stopWriting();

        System.out.println("Vertexes Processed: " + engine.numVertices());
View Full Code Here

        String baseFilename = args[0];
        int nShards = Integer.parseInt(args[1]);

        ArrayList<VertexInterval> intervals = ChiFilenames.loadIntervals(baseFilename, nShards);
        TreeSet<IdFloat> top20 = Toplist.topListFloat(baseFilename, intervals.get(intervals.size() - 1).getLastVertex(), 20);
        VertexIdTranslate trans = VertexIdTranslate.fromFile(new File(ChiFilenames.getVertexTranslateDefFile(baseFilename, nShards)));
        System.out.println("Result: " + top20);
        int i = 0;
        for(IdFloat vertexRank : top20) {
            System.out.println(++i + ": " + trans.backward(vertexRank.getVertexId()) + " = " + vertexRank.getValue());
        }
    }
View Full Code Here

TOP

Related Classes of edu.cmu.graphchi.preprocessing.VertexIdTranslate

Copyright © 2018 www.massapicom. 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.