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

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

Introduction

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

Prototype

public static ParagraphVectors readParagraphVectors(InputStream stream) throws IOException 

Source Link

Document

This method restores ParagraphVectors model previously saved with writeParagraphVectors()

Usage

From source file:dollar.learner.smart.ParagraphVectorsClassifierExample.java

License:Apache License

public void start() throws Exception {
    if (serializeFile().exists()) {
        try {//www . j av  a2  s . c  o  m
            log.info("Loading from " + serializeFile().getAbsolutePath());
            paragraphVectors = WordVectorSerializer.readParagraphVectors(serializeFile());
        } catch (Exception e) {
            log.debug(e.getMessage(), e);
            makeParagraphVectors();
        }
    } else {
        makeParagraphVectors();
    }
}

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

License:Open Source License

/**
 * Reads {@link WordVectors} from the specified {@link ZipInputStream}. The method expects the ZipInputStream to
 * contain an ZipEntry with name "word_vectors", which contains the WordVector model to load.
 *
 * @param in stream to read from// ww w  . jav a  2  s  .  c o m
 * @param mode the type of WordVector model to expect
 * @return {@link WordVectors} loaded from stream
 * @throws IOException
 */
public static WordVectors loadWordVectors(final ZipInputStream in, final WordVectorTrainingMode mode)
        throws IOException {
    ZipEntry entry;
    while ((entry = in.getNextEntry()) != null) {
        if (entry.getName().matches("word_vectors")) {
            switch (mode) {
            case DOC2VEC:
                return WordVectorSerializer.readParagraphVectors(in);
            case WORD2VEC:
                /* Need to copy stream to temp file because API does not support Word2VecModel reading
                 * with InputStreams. */
                Word2Vec model = null;
                File tmp = null;
                try {
                    tmp = copyInputStreamToTmpFile(in);
                    model = WordVectorSerializer.readWord2VecModel(tmp);
                } catch (Exception e) {
                    throw e;
                } finally {
                    if (tmp != null && tmp.exists()) {
                        tmp.delete();
                    }
                }

                return model;
            default:
                throw new IllegalStateException(
                        "No deserialization method defined for WordVectors of type: " + mode);
            }
        }
    }
    throw new IllegalArgumentException(
            "WordVectors entry not found. ZipInputStream seems not to contain ZipEntry "
                    + "with name 'word_vectors'!");
}

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

License:Open Source License

/**
 * Reads {@link WordVectors} from the specified {@link URL}.
 *
 * @param url the URL to read from//from   www. j a v  a  2 s. c om
 * @param mode the type of WordVector model to expect
 * @return {@link WordVectors} loaded from URL
 * @throws IOException
 * @throws URISyntaxException
 */
public static WordVectors loadWordVectors(final URL url, final WordVectorTrainingMode mode)
        throws IOException, URISyntaxException {
    switch (mode) {
    case DOC2VEC:
        return WordVectorSerializer.readParagraphVectors(url.openStream());
    case WORD2VEC:
        boolean isLocalFile = FileUtil.resolveToPath(url) != null;
        File wvFile = null;
        try {
            wvFile = isLocalFile ? FileUtil.getFileFromURL(url) : copyURLToTmpFile(url);
            return WordVectorSerializer.readWord2VecModel(wvFile);
        } catch (Exception e) {
            // error is handled outside
            throw e;
        } finally {
            // file has been temporarily downloaded
            if (!isLocalFile && wvFile != null) {
                wvFile.delete();
            }
        }

    default:
        throw new IllegalStateException("No deserialization method defined for WordVectors of type: " + mode);
    }
}