Search in sources :

Example 1 with GraphVectorsImpl

use of org.deeplearning4j.graph.models.embeddings.GraphVectorsImpl 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);
}
Also used : GraphVectorsImpl(org.deeplearning4j.graph.models.embeddings.GraphVectorsImpl) INDArray(org.nd4j.linalg.api.ndarray.INDArray) InMemoryGraphLookupTable(org.deeplearning4j.graph.models.embeddings.InMemoryGraphLookupTable) ArrayList(java.util.ArrayList) LineIterator(org.apache.commons.io.LineIterator)

Aggregations

ArrayList (java.util.ArrayList)1 LineIterator (org.apache.commons.io.LineIterator)1 GraphVectorsImpl (org.deeplearning4j.graph.models.embeddings.GraphVectorsImpl)1 InMemoryGraphLookupTable (org.deeplearning4j.graph.models.embeddings.InMemoryGraphLookupTable)1 INDArray (org.nd4j.linalg.api.ndarray.INDArray)1