Example usage for org.deeplearning4j.models.word2vec.wordstore.inmemory InMemoryLookupCache wordFor

List of usage examples for org.deeplearning4j.models.word2vec.wordstore.inmemory InMemoryLookupCache wordFor

Introduction

In this page you can find the example usage for org.deeplearning4j.models.word2vec.wordstore.inmemory InMemoryLookupCache wordFor.

Prototype

@Override
    public VocabWord wordFor(long id) 

Source Link

Usage

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

License:Apache License

/**
 * Write the tsne format/* w  ww .  ja va2  s  .  com*/
 *
 * @param vec
 *            the word vectors to use for labeling
 * @param tsne
 *            the tsne array to write
 * @param csv
 *            the file to use
 * @throws Exception
 */
public static void writeTsneFormat(Glove vec, INDArray tsne, File csv) throws Exception {
    BufferedWriter write = new BufferedWriter(new FileWriter(csv));
    int words = 0;
    InMemoryLookupCache l = (InMemoryLookupCache) vec.vocab();
    for (String word : vec.vocab().words()) {
        if (word == null) {
            continue;
        }
        StringBuilder sb = new StringBuilder();
        INDArray wordVector = tsne.getRow(l.wordFor(word).getIndex());
        for (int j = 0; j < wordVector.length(); j++) {
            sb.append(wordVector.getDouble(j));
            if (j < wordVector.length() - 1) {
                sb.append(",");
            }
        }
        sb.append(",");
        sb.append(word);
        sb.append(" ");

        sb.append("\n");
        write.write(sb.toString());

    }

    log.info("Wrote " + words + " with size of " + vec.lookupTable().getVectorLength());
    write.flush();
    write.close();

}

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

License:Apache License

/**
 * Write the tsne format/*  w w  w .  j a  va 2 s  .  c  o m*/
 *
 * @param vec
 *            the word vectors to use for labeling
 * @param tsne
 *            the tsne array to write
 * @param csv
 *            the file to use
 * @throws Exception
 */
public static void writeTsneFormat(Word2Vec vec, INDArray tsne, File csv) throws Exception {
    BufferedWriter write = new BufferedWriter(new FileWriter(csv));
    int words = 0;
    InMemoryLookupCache l = (InMemoryLookupCache) vec.vocab();
    for (String word : vec.vocab().words()) {
        if (word == null) {
            continue;
        }
        StringBuilder sb = new StringBuilder();
        INDArray wordVector = tsne.getRow(l.wordFor(word).getIndex());
        for (int j = 0; j < wordVector.length(); j++) {
            sb.append(wordVector.getDouble(j));
            if (j < wordVector.length() - 1) {
                sb.append(",");
            }
        }
        sb.append(",");
        sb.append(word);
        sb.append(" ");

        sb.append("\n");
        write.write(sb.toString());

    }

    log.info("Wrote " + words + " with size of " + vec.lookupTable().layerSize());
    write.flush();
    write.close();

}