use of org.deeplearning4j.graph.models.embeddings.InMemoryGraphLookupTable in project deeplearning4j by deeplearning4j.
the class DeepWalk method initialize.
/** Initialize the DeepWalk model with a list of vertex degrees for a graph.<br>
* Specifically, graphVertexDegrees[i] represents the vertex degree of the ith vertex<br>
* vertex degrees are used to construct a binary (Huffman) tree, which is in turn used in
* the hierarchical softmax implementation
* @param graphVertexDegrees degrees of each vertex
*/
public void initialize(int[] graphVertexDegrees) {
log.info("Initializing: Creating Huffman tree and lookup table...");
GraphHuffman gh = new GraphHuffman(graphVertexDegrees.length);
gh.buildTree(graphVertexDegrees);
lookupTable = new InMemoryGraphLookupTable(graphVertexDegrees.length, vectorSize, gh, learningRate);
initCalled = true;
log.info("Initialization complete");
}
use of org.deeplearning4j.graph.models.embeddings.InMemoryGraphLookupTable in project deeplearning4j by deeplearning4j.
the class GraphVectorSerializer method loadTxtVectors.
public static GraphVectors loadTxtVectors(File file) throws IOException {
List<double[]> vectorList = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
LineIterator iter = IOUtils.lineIterator(reader);
while (iter.hasNext()) {
String line = iter.next();
String[] split = line.split(DELIM);
double[] vec = new double[split.length - 1];
for (int i = 1; i < split.length; i++) {
vec[i - 1] = Double.parseDouble(split[i]);
}
vectorList.add(vec);
}
}
int vecSize = vectorList.get(0).length;
int nVertices = vectorList.size();
INDArray vectors = Nd4j.create(nVertices, vecSize);
for (int i = 0; i < vectorList.size(); i++) {
double[] vec = vectorList.get(i);
for (int j = 0; j < vec.length; j++) {
vectors.put(i, j, vec[j]);
}
}
InMemoryGraphLookupTable table = new InMemoryGraphLookupTable(nVertices, vecSize, null, 0.01);
table.setVertexVectors(vectors);
return new GraphVectorsImpl<>(null, table);
}
use of org.deeplearning4j.graph.models.embeddings.InMemoryGraphLookupTable 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();
}
}
}
use of org.deeplearning4j.graph.models.embeddings.InMemoryGraphLookupTable 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();
}
}
}
Aggregations