Example usage for org.deeplearning4j.models.embeddings.loader WordVectorSerializer writeWord2VecModel

List of usage examples for org.deeplearning4j.models.embeddings.loader WordVectorSerializer writeWord2VecModel

Introduction

In this page you can find the example usage for org.deeplearning4j.models.embeddings.loader WordVectorSerializer writeWord2VecModel.

Prototype

public static void writeWord2VecModel(Word2Vec vectors, OutputStream stream) throws IOException 

Source Link

Document

This method saves Word2Vec model into compressed zip file and sends it to output stream PLEASE NOTE: This method saves FULL model, including syn0 AND syn1

Usage

From source file:net.liaocy.ml4j.nlp.word2vec.Train.java

private void saveModel(String modelName, Word2Vec vec) throws IOException {
    MongoDatabase db = Mongo.getDB();//from  w w w  .  ja  v a  2  s .c o  m
    GridFSBucket gridFSBucket = GridFSBuckets.create(db, "word2vecmodels");

    GridFSFile gfsfi = gridFSBucket.find(new Document("filename", modelName)).first();
    if (gfsfi != null) {
        ObjectId id = gfsfi.getObjectId();
        gridFSBucket.delete(id);
    }

    try (GridFSUploadStream uploadStream = gridFSBucket.openUploadStream(modelName)) {
        WordVectorSerializer.writeWord2VecModel(vec, uploadStream);
        System.out.println("Save Model Successed!");
    }
}

From source file:org.knime.ext.textprocessing.dl4j.util.WordVectorPortObjectUtils.java

License:Open Source License

/**
 * Writes {@link WordVectors} to specified {@link ZipInputStream}. Calling this method will close the stream (closed
 * by WordVectorSerializer)./*from  w w  w .  ja  va  2  s. co  m*/
 *
 * @param wordVectors {@link WordVectors} to write
 * @param out stream to write to
 * @throws IOException
 */
public static void writeWordVectors(final WordVectors wordVectors, final ZipOutputStream out)
        throws IOException {
    final ZipEntry entry = new ZipEntry("word_vectors");
    out.putNextEntry(entry);
    //first check if ParagraphVectors because it extends Word2Vec
    if (wordVectors instanceof ParagraphVectors) {
        ParagraphVectors d2v = (ParagraphVectors) wordVectors;
        WordVectorSerializer.writeParagraphVectors(d2v, out);
    } else if (wordVectors instanceof Word2Vec) {
        Word2Vec w2v = (Word2Vec) wordVectors;
        WordVectorSerializer.writeWord2VecModel(w2v, out);
    } else {
        throw new IllegalStateException("No serialization method defined for WordVectors of type: "
                + wordVectors.getClass().getSimpleName());
    }
}