/** * Writes the word vectors to the given path. Note that this assumes an in memory cache * * @param vec the word2vec to write * @param path the path to write * @throws IOException */ public static void writeWordVectors(Word2Vec vec, String path) throws IOException { BufferedWriter write = new BufferedWriter(new FileWriter(new File(path), false)); int words = 0; for (String word : vec.vocab().words()) { if (word == null) { continue; } StringBuilder sb = new StringBuilder(); sb.append(word.replaceAll(" ", "_")); sb.append(" "); INDArray wordVector = vec.getWordVectorMatrix(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()); words++; } log.info("Wrote " + words + " with size of " + vec.lookupTable().layerSize()); write.flush(); write.close(); }
/** * Write the tsne format * * @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(); }
public Builder setFeatureVectors(Word2Vec vec) { vocabCache = vec.vocab(); return setFeatureVectors(vec.lookupTable()); }