Package edu.cmu.graphchi.util

Examples of edu.cmu.graphchi.util.HugeDoubleMatrix


        static int nShards;
        static int quiet;
       
        void init_feature_vectors(long size){
          logger.info("Initializing latent factors for " + size + " vertices");
          latent_factors_inmem = new HugeDoubleMatrix(size, D);

          /* Fill with random data */
          latent_factors_inmem.randomize(0f, 1.0f);
        }
View Full Code Here


         * Vertices' latent factors are stored in the vertexValueMatrix
         * so that each row contains one latent factor.
         */
        if (ctx.getIteration() == 0) {
            logger.info("Initializing latent factors for " + ctx.getNumVertices() + " vertices");
            vertexValueMatrix = new HugeDoubleMatrix(ctx.getNumVertices(), D);

            /* Fill with random data */
            vertexValueMatrix.randomize(0f, 1.0f);
        }
    }
View Full Code Here

        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

         */
        if (ctx.getIteration() == 0) {
            logger.info("Initializing latent factors for " + (1 + maxLeftVertexId) + " vertices on the left side");
            logger.info("Initializing latent factors for " + (1 + maxRightVertexId) + " vertices on the right side");

            leftSideMatrix = new HugeDoubleMatrix(maxLeftVertexId + 1, D);
            rightSideMatrix = new HugeDoubleMatrix(maxRightVertexId + 1, D);
            /* Fill with random data */
            leftSideMatrix.randomize(0f, 1.0f);
            rightSideMatrix.randomize(0f, 1.0f);
        }
    }
View Full Code Here

        logger.info("Train RMSE: " + trainRMSE + ", total edges:" + engine.numEdges());
    }

    @Override
    protected Tuple getNextResult(TupleFactory tupleFactory) throws ExecException {
        HugeDoubleMatrix matrix;
        int vertexId = 0;
        String factor;
        if (outputCounter < maxLeftVertexId) {
            matrix = leftSideMatrix;
            vertexId = outputCounter;
            factor = "U";
        } else {
            matrix = rightSideMatrix;
            vertexId = outputCounter - maxLeftVertexId;
            factor = "V";
            if (vertexId >= rightSideMatrix.getNumRows()) return null;
        }
        Tuple t = tupleFactory.newTuple(2 + D);
        t.set(0, factor);
        t.set(1, vertexId);
        for(int i=0; i<D; i++) {
            t.set(2 + i, matrix.getValue(vertexId, i));
        }
        outputCounter++;
        return t;

    }
View Full Code Here

TOP

Related Classes of edu.cmu.graphchi.util.HugeDoubleMatrix

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.