Cyber Whale

How to write trained Word2Vec model to CSV with DeepLearning4j

I used DeepLearning4j to train word2vec model. Then I had to save the dictionary to CSV so I can run some clustering algorithms on it.

deeplearning4jjavamachine learningtutorialword2vecDevelopment tips

How to write trained Word2Vec model to CSV with DeepLearning4j

I used DeepLearning4j to train word2vec model. Then I had to save the dictionary to CSV so I can run some clustering algorithms on it.

Sounded like a simple task, but it took a while, and here is the code to do this:

private void writeIndexToCsv(String csvFileName, Word2Vec model) { CSVWriter writer = null; try { writer = new CSVWriter(new FileWriter(csvFileName)); } catch (IOException e) { e.printStackTrace(); }

VocabCache vocCache = model.vocab(); Collection wrds = vocCache.vocabWords();

for (VocabWord w : wrds) { String s = w.getWord(); System.out.println(“Looking into the word:”); System.out.println(s); StringBuilder sb = new StringBuilder(); sb.append(s).append(”,”); double[] wordVector = model.getWordVector(s); for (int i = 0; i < wordVector.length; i++) { sb.append(wordVector[i]).append(”,”); }

writer.writeNext(sb.toString().split(”,”), false); }

try { writer.close(); } catch (IOException e) { e.printStackTrace(); } }private void writeIndexToCsv(String csvFileName, Word2Vec model) { CSVWriter writer = null; try { writer = new CSVWriter(new FileWriter(csvFileName)); } catch (IOException e) { e.printStackTrace(); }

VocabCache vocCache = model.vocab(); Collection wrds = vocCache.vocabWords();

for (VocabWord w : wrds) { String s = w.getWord(); System.out.println(“Looking into the word:”); System.out.println(s); StringBuilder sb = new StringBuilder(); sb.append(s).append(”,”); double[] wordVector = model.getWordVector(s); for (int i = 0; i < wordVector.length; i++) { sb.append(wordVector[i]).append(”,”); }

writer.writeNext(sb.toString().split(”,”), false); }

try { writer.close(); } catch (IOException e) { e.printStackTrace(); } }

By admindeeplearning4jjavamachine learningtutorialword2vec

Recent Posts

Archive

Tags

#CloudTechnologies #Code #CyberWhale #GenerativeAI Analytics bi Big Data Business Business growth business intelligence Cambrian Explosion Cloud data Data Capture Data Management Data Science Data Stack Data Synchronization Data Systems Data Warehouse DBLog deeplearning4j development dev tools DoorDash ELT ETL Hacks java jobs libs machine learning parsing Production python request Retail SaaS scala Strategy tip tutorial Uber word2vec xml

AI Strategy & Consulting

Build a future-ready AI strategy with trusted guidance