java深度学习库deeplearning4j使用之word2vec与INDAarry

女生的网名这么多〃 提交于 2019-12-16 09:18:04

import org.deeplearning4j.models.embeddings.loader.WordVectorSerializer;
import org.deeplearning4j.models.word2vec.Word2Vec;
import org.nd4j.linalg.api.ndarray.INDArray;

/**

*必须的maven,核心组件、nlp模块、platform

*INDArray相当于python的numpy

<dependency>
    <groupId>org.deeplearning4j</groupId>
    <artifactId>deeplearning4j-core</artifactId>
    <version>1.0.0-beta4</version>
</dependency>

<dependency>
    <groupId>org.nd4j</groupId>
    <artifactId>nd4j-native-platform</artifactId>
    <version>1.0.0-beta4</version>
</dependency>

<dependency>
    <groupId>org.deeplearning4j</groupId>
    <artifactId>deeplearning4j-nlp</artifactId>
    <version>1.0.0-beta4</version>
</dependency>

*/

    public static void main(String[] args) {
        String categoryVecPath = "D:\\.TencentEmbeddingBinary.w2v";
        String allWordVecPath = "D:\\.TencentEmbeddingBinary.w2v";
        Word2Vec vecModel = WordVectorSerializer.readWord2VecModel(allWordVecPath);
        INDArray word = vecModel.getWordVectorMatrix("谢安琪");
        System.out.println(word);
        //两个词向量相加,INDArray才可以进行向量操作
        INDArray addedWord = word.add(vecModel.getWordVectorMatrix("催眠"))

        System.out.println(addedWord);

        //获取某个词的向量
        double[] w = categoryModel.getWordVector("催眠");  //oov会返回null
        List<String> cate = (List) categoryModel.wordsNearest(word, 10);
        System.out.println("ok");
        System.out.println(cate.get(0));
        }

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!