Example usage for org.deeplearning4j.models.embeddings.inmemory InMemoryLookupTable getSyn0

List of usage examples for org.deeplearning4j.models.embeddings.inmemory InMemoryLookupTable getSyn0

Introduction

In this page you can find the example usage for org.deeplearning4j.models.embeddings.inmemory InMemoryLookupTable getSyn0.

Prototype

public INDArray getSyn0() 

Source Link

Usage

From source file:de.mpii.docsimilarity.mr.utils.io.WordVectorSerializer.java

License:Apache License

/**
 * Writes the word vectors to the given path. Note that this assumes an in memory cache
 *
 * @param lookupTable// w  ww .  j  a va  2  s .  co  m
 * @param cache
 *
 * @param path
 *            the path to write
 * @throws IOException
 */
public static void writeWordVectors(InMemoryLookupTable lookupTable, InMemoryLookupCache cache, String path)
        throws IOException {
    BufferedWriter write = new BufferedWriter(new FileWriter(new File(path), false));
    for (int i = 0; i < lookupTable.getSyn0().rows(); i++) {
        String word = cache.wordAtIndex(i);
        if (word == null) {
            continue;
        }
        StringBuilder sb = new StringBuilder();
        sb.append(word.replaceAll(" ", "_"));
        sb.append(" ");
        INDArray wordVector = lookupTable.vector(word);
        for (int j = 0; j < wordVector.length(); j++) {
            sb.append(wordVector.getDouble(j));
            if (j < wordVector.length() - 1) {
                sb.append(" ");
            }
        }
        sb.append("\n");
        write.write(sb.toString());

    }

    write.flush();
    write.close();

}