home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (GraphVectorSerializer.java)

This example Java source code file (GraphVectorSerializer.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

arraylist, bufferedwriter, delim, file, filereader, graphvectorserializer, indarray, inmemorygraphlookuptable, ioexception, lineiterator, list, string, stringbuilder, util, wrote

The GraphVectorSerializer.java Java example source code

package org.deeplearning4j.graph.models.loader;

import org.apache.commons.io.IOUtils;
import org.apache.commons.io.LineIterator;
import org.deeplearning4j.graph.models.GraphVectors;
import org.deeplearning4j.graph.models.deepwalk.DeepWalk;
import org.deeplearning4j.graph.models.embeddings.GraphVectorsImpl;
import org.deeplearning4j.graph.models.embeddings.InMemoryGraphLookupTable;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.factory.Nd4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**GraphVectorSerializer: Provide static methods to save and load DeepWalk/Graph vectors
public class GraphVectorSerializer {
    private static final Logger log = LoggerFactory.getLogger(GraphVectorSerializer.class);
    private static final String DELIM = "\t";

    public static void writeGraphVectors(DeepWalk deepWalk, String path) throws IOException {

        int nVertices = deepWalk.numVertices();
        int vectorSize = deepWalk.getVectorSize();

        try( BufferedWriter write = new BufferedWriter(new FileWriter(new File(path), false)) ){
            for( int i=0; i<nVertices; i++ ){
                StringBuilder sb = new StringBuilder();
                INDArray vec = deepWalk.getVertexVector(i);
                for( int j=0; j<vectorSize; j++ ){
                    double d = vec.getDouble(j);

        log.info("Wrote {} vectors of length {} to: {}",nVertices,vectorSize,path);

    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);

                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]);

        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++ ){

        InMemoryGraphLookupTable table = new InMemoryGraphLookupTable(nVertices,vecSize,null,0.01);

        return new GraphVectorsImpl<Object,Object>(null,table);


Other Java examples (source code examples)

Here is a short list of links related to this Java GraphVectorSerializer.java source code file:

my book on functional programming


new blog posts


Copyright 1998-2019 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.