Search in sources :

Example 1 with RandomWalkIterator

use of org.deeplearning4j.graph.iterator.RandomWalkIterator in project deeplearning4j by deeplearning4j.

the class DeepWalkGradientCheck method checkGradients2.

@Test
public void checkGradients2() throws IOException {
    ClassPathResource cpr = new ClassPathResource("graph13.txt");
    int nVertices = 13;
    Graph<String, String> graph = GraphLoader.loadUndirectedGraphEdgeListFile(cpr.getTempFileFromArchive().getAbsolutePath(), 13, ",");
    int vectorSize = 10;
    int windowSize = 3;
    Nd4j.getRandom().setSeed(12345);
    DeepWalk<String, String> deepWalk = new DeepWalk.Builder<String, String>().learningRate(0.01).vectorSize(vectorSize).windowSize(windowSize).learningRate(0.01).build();
    deepWalk.initialize(graph);
    for (int i = 0; i < nVertices; i++) {
        INDArray vector = deepWalk.getVertexVector(i);
        assertArrayEquals(new int[] { 1, vectorSize }, vector.shape());
        System.out.println(Arrays.toString(vector.dup().data().asFloat()));
    }
    GraphWalkIterator<String> iter = new RandomWalkIterator<>(graph, 10);
    deepWalk.fit(iter);
    //Now, to check gradients:
    InMemoryGraphLookupTable table = (InMemoryGraphLookupTable) deepWalk.lookupTable();
    GraphHuffman tree = (GraphHuffman) table.getTree();
    //For each pair of input/output vertices: check gradients
    for (int i = 0; i < nVertices; i++) {
        //in
        //First: check probabilities p(out|in)
        double[] probs = new double[nVertices];
        double sumProb = 0.0;
        for (int j = 0; j < nVertices; j++) {
            probs[j] = table.calculateProb(i, j);
            assertTrue(probs[j] >= 0.0 && probs[j] <= 1.0);
            sumProb += probs[j];
        }
        assertTrue("Output probabilities do not sum to 1.0 (i=" + i + "), sum=" + sumProb, Math.abs(sumProb - 1.0) < 1e-5);
        for (int j = 0; j < nVertices; j++) {
            //out
            //p(j|i)
            int[] pathInnerNodes = tree.getPathInnerNodes(j);
            //Calculate gradients:
            INDArray[][] vecAndGrads = table.vectorsAndGradients(i, j);
            assertEquals(2, vecAndGrads.length);
            assertEquals(pathInnerNodes.length + 1, vecAndGrads[0].length);
            assertEquals(pathInnerNodes.length + 1, vecAndGrads[1].length);
            //Calculate gradients:
            //Two types of gradients to test:
            //(a) gradient of loss fn. wrt inner node vector representation
            //(b) gradient of loss fn. wrt vector for input word
            INDArray vertexVector = table.getVector(i);
            //Check gradients for inner nodes:
            for (int p = 0; p < pathInnerNodes.length; p++) {
                int innerNodeIdx = pathInnerNodes[p];
                INDArray innerNodeVector = table.getInnerNodeVector(innerNodeIdx);
                INDArray innerNodeGrad = vecAndGrads[1][p + 1];
                for (int v = 0; v < innerNodeVector.length(); v++) {
                    double backpropGradient = innerNodeGrad.getDouble(v);
                    double origParamValue = innerNodeVector.getDouble(v);
                    innerNodeVector.putScalar(v, origParamValue + epsilon);
                    double scorePlus = table.calculateScore(i, j);
                    innerNodeVector.putScalar(v, origParamValue - epsilon);
                    double scoreMinus = table.calculateScore(i, j);
                    //reset param so it doesn't affect later calcs
                    innerNodeVector.putScalar(v, origParamValue);
                    double numericalGradient = (scorePlus - scoreMinus) / (2 * epsilon);
                    double relError;
                    if (backpropGradient == 0.0 && numericalGradient == 0.0)
                        relError = 0.0;
                    else {
                        relError = Math.abs(backpropGradient - numericalGradient) / (Math.abs(backpropGradient) + Math.abs(numericalGradient));
                    }
                    String msg = "innerNode grad: i=" + i + ", j=" + j + ", p=" + p + ", v=" + v + " - relError: " + relError + ", scorePlus=" + scorePlus + ", scoreMinus=" + scoreMinus + ", numGrad=" + numericalGradient + ", backpropGrad = " + backpropGradient;
                    if (relError > MAX_REL_ERROR)
                        fail(msg);
                    else
                        System.out.println(msg);
                }
            }
            //Check gradients for input word vector:
            INDArray vectorGrad = vecAndGrads[1][0];
            assertArrayEquals(vectorGrad.shape(), vertexVector.shape());
            for (int v = 0; v < vectorGrad.length(); v++) {
                double backpropGradient = vectorGrad.getDouble(v);
                double origParamValue = vertexVector.getDouble(v);
                vertexVector.putScalar(v, origParamValue + epsilon);
                double scorePlus = table.calculateScore(i, j);
                vertexVector.putScalar(v, origParamValue - epsilon);
                double scoreMinus = table.calculateScore(i, j);
                vertexVector.putScalar(v, origParamValue);
                double numericalGradient = (scorePlus - scoreMinus) / (2 * epsilon);
                double relError;
                if (backpropGradient == 0.0 && numericalGradient == 0.0)
                    relError = 0.0;
                else {
                    relError = Math.abs(backpropGradient - numericalGradient) / (Math.abs(backpropGradient) + Math.abs(numericalGradient));
                }
                String msg = "vector grad: i=" + i + ", j=" + j + ", v=" + v + " - relError: " + relError + ", scorePlus=" + scorePlus + ", scoreMinus=" + scoreMinus + ", numGrad=" + numericalGradient + ", backpropGrad = " + backpropGradient;
                if (relError > MAX_REL_ERROR)
                    fail(msg);
                else
                    System.out.println(msg);
            }
            System.out.println();
        }
    }
}
Also used : ClassPathResource(org.nd4j.linalg.io.ClassPathResource) INDArray(org.nd4j.linalg.api.ndarray.INDArray) InMemoryGraphLookupTable(org.deeplearning4j.graph.models.embeddings.InMemoryGraphLookupTable) RandomWalkIterator(org.deeplearning4j.graph.iterator.RandomWalkIterator) Test(org.junit.Test)

Example 2 with RandomWalkIterator

use of org.deeplearning4j.graph.iterator.RandomWalkIterator in project deeplearning4j by deeplearning4j.

the class DeepWalkGradientCheck method checkGradients.

@Test
public void checkGradients() throws IOException {
    ClassPathResource cpr = new ClassPathResource("testgraph_7vertices.txt");
    Graph<String, String> graph = GraphLoader.loadUndirectedGraphEdgeListFile(cpr.getTempFileFromArchive().getAbsolutePath(), 7, ",");
    int vectorSize = 5;
    int windowSize = 2;
    Nd4j.getRandom().setSeed(12345);
    DeepWalk<String, String> deepWalk = new DeepWalk.Builder<String, String>().learningRate(0.01).vectorSize(vectorSize).windowSize(windowSize).learningRate(0.01).build();
    deepWalk.initialize(graph);
    for (int i = 0; i < 7; i++) {
        INDArray vector = deepWalk.getVertexVector(i);
        assertArrayEquals(new int[] { 1, vectorSize }, vector.shape());
        System.out.println(Arrays.toString(vector.dup().data().asFloat()));
    }
    GraphWalkIterator<String> iter = new RandomWalkIterator<>(graph, 8);
    deepWalk.fit(iter);
    //Now, to check gradients:
    InMemoryGraphLookupTable table = (InMemoryGraphLookupTable) deepWalk.lookupTable();
    GraphHuffman tree = (GraphHuffman) table.getTree();
    //For each pair of input/output vertices: check gradients
    for (int i = 0; i < 7; i++) {
        //in
        //First: check probabilities p(out|in)
        double[] probs = new double[7];
        double sumProb = 0.0;
        for (int j = 0; j < 7; j++) {
            probs[j] = table.calculateProb(i, j);
            assertTrue(probs[j] >= 0.0 && probs[j] <= 1.0);
            sumProb += probs[j];
        }
        assertTrue("Output probabilities do not sum to 1.0", Math.abs(sumProb - 1.0) < 1e-5);
        for (int j = 0; j < 7; j++) {
            //out
            //p(j|i)
            int[] pathInnerNodes = tree.getPathInnerNodes(j);
            //Calculate gradients:
            INDArray[][] vecAndGrads = table.vectorsAndGradients(i, j);
            assertEquals(2, vecAndGrads.length);
            assertEquals(pathInnerNodes.length + 1, vecAndGrads[0].length);
            assertEquals(pathInnerNodes.length + 1, vecAndGrads[1].length);
            //Calculate gradients:
            //Two types of gradients to test:
            //(a) gradient of loss fn. wrt inner node vector representation
            //(b) gradient of loss fn. wrt vector for input word
            INDArray vertexVector = table.getVector(i);
            //Check gradients for inner nodes:
            for (int p = 0; p < pathInnerNodes.length; p++) {
                int innerNodeIdx = pathInnerNodes[p];
                INDArray innerNodeVector = table.getInnerNodeVector(innerNodeIdx);
                INDArray innerNodeGrad = vecAndGrads[1][p + 1];
                for (int v = 0; v < innerNodeVector.length(); v++) {
                    double backpropGradient = innerNodeGrad.getDouble(v);
                    double origParamValue = innerNodeVector.getDouble(v);
                    innerNodeVector.putScalar(v, origParamValue + epsilon);
                    double scorePlus = table.calculateScore(i, j);
                    innerNodeVector.putScalar(v, origParamValue - epsilon);
                    double scoreMinus = table.calculateScore(i, j);
                    //reset param so it doesn't affect later calcs
                    innerNodeVector.putScalar(v, origParamValue);
                    double numericalGradient = (scorePlus - scoreMinus) / (2 * epsilon);
                    double relError;
                    if (backpropGradient == 0.0 && numericalGradient == 0.0)
                        relError = 0.0;
                    else {
                        relError = Math.abs(backpropGradient - numericalGradient) / (Math.abs(backpropGradient) + Math.abs(numericalGradient));
                    }
                    String msg = "innerNode grad: i=" + i + ", j=" + j + ", p=" + p + ", v=" + v + " - relError: " + relError + ", scorePlus=" + scorePlus + ", scoreMinus=" + scoreMinus + ", numGrad=" + numericalGradient + ", backpropGrad = " + backpropGradient;
                    if (relError > MAX_REL_ERROR)
                        fail(msg);
                    else
                        System.out.println(msg);
                }
            }
            //Check gradients for input word vector:
            INDArray vectorGrad = vecAndGrads[1][0];
            assertArrayEquals(vectorGrad.shape(), vertexVector.shape());
            for (int v = 0; v < vectorGrad.length(); v++) {
                double backpropGradient = vectorGrad.getDouble(v);
                double origParamValue = vertexVector.getDouble(v);
                vertexVector.putScalar(v, origParamValue + epsilon);
                double scorePlus = table.calculateScore(i, j);
                vertexVector.putScalar(v, origParamValue - epsilon);
                double scoreMinus = table.calculateScore(i, j);
                vertexVector.putScalar(v, origParamValue);
                double numericalGradient = (scorePlus - scoreMinus) / (2 * epsilon);
                double relError;
                if (backpropGradient == 0.0 && numericalGradient == 0.0)
                    relError = 0.0;
                else {
                    relError = Math.abs(backpropGradient - numericalGradient) / (Math.abs(backpropGradient) + Math.abs(numericalGradient));
                }
                String msg = "vector grad: i=" + i + ", j=" + j + ", v=" + v + " - relError: " + relError + ", scorePlus=" + scorePlus + ", scoreMinus=" + scoreMinus + ", numGrad=" + numericalGradient + ", backpropGrad = " + backpropGradient;
                if (relError > MAX_REL_ERROR)
                    fail(msg);
                else
                    System.out.println(msg);
            }
            System.out.println();
        }
    }
}
Also used : ClassPathResource(org.nd4j.linalg.io.ClassPathResource) INDArray(org.nd4j.linalg.api.ndarray.INDArray) InMemoryGraphLookupTable(org.deeplearning4j.graph.models.embeddings.InMemoryGraphLookupTable) RandomWalkIterator(org.deeplearning4j.graph.iterator.RandomWalkIterator) Test(org.junit.Test)

Example 3 with RandomWalkIterator

use of org.deeplearning4j.graph.iterator.RandomWalkIterator in project deeplearning4j by deeplearning4j.

the class TestDeepWalk method testBasic.

@Test
public void testBasic() throws IOException {
    //Very basic test. Load graph, build tree, call fit, make sure it doesn't throw any exceptions
    ClassPathResource cpr = new ClassPathResource("testgraph_7vertices.txt");
    Graph<String, String> graph = GraphLoader.loadUndirectedGraphEdgeListFile(cpr.getTempFileFromArchive().getAbsolutePath(), 7, ",");
    int vectorSize = 5;
    int windowSize = 2;
    DeepWalk<String, String> deepWalk = new DeepWalk.Builder<String, String>().learningRate(0.01).vectorSize(vectorSize).windowSize(windowSize).learningRate(0.01).build();
    deepWalk.initialize(graph);
    for (int i = 0; i < 7; i++) {
        INDArray vector = deepWalk.getVertexVector(i);
        assertArrayEquals(new int[] { 1, vectorSize }, vector.shape());
        System.out.println(Arrays.toString(vector.dup().data().asFloat()));
    }
    GraphWalkIterator<String> iter = new RandomWalkIterator<>(graph, 8);
    deepWalk.fit(iter);
    for (int t = 0; t < 5; t++) {
        iter.reset();
        deepWalk.fit(iter);
        System.out.println("--------------------");
        for (int i = 0; i < 7; i++) {
            INDArray vector = deepWalk.getVertexVector(i);
            assertArrayEquals(new int[] { 1, vectorSize }, vector.shape());
            System.out.println(Arrays.toString(vector.dup().data().asFloat()));
        }
    }
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) RandomWalkIterator(org.deeplearning4j.graph.iterator.RandomWalkIterator) ClassPathResource(org.nd4j.linalg.io.ClassPathResource) Test(org.junit.Test)

Aggregations

RandomWalkIterator (org.deeplearning4j.graph.iterator.RandomWalkIterator)3 Test (org.junit.Test)3 INDArray (org.nd4j.linalg.api.ndarray.INDArray)3 ClassPathResource (org.nd4j.linalg.io.ClassPathResource)3 InMemoryGraphLookupTable (org.deeplearning4j.graph.models.embeddings.InMemoryGraphLookupTable)2