简体   繁体   English

是否可以在deeplearning4j.word2vec中使用gensim word2vec模型?

[英]Is it possible to use gensim word2vec model in deeplearning4j.word2vec?

I'm new to deeplearning4j, i want to make sentence classifier using words vector as input for the classifier. 我是deeplearning4j的新手,我想使用单词vector作为分类器的输入来创建句子分类器。 I was using python before, where the vector model was generated using gensim, and i want to use that model for this new classifier. 之前我正在使用python,其中使用gensim生成矢量模型,我想将该模型用于这个新的分类器。 Is it possible to use gensim's word2vec model in deeplearning4j.word2vec and how i can do that? 是否可以在deeplearning4j.word2vec中使用gensim的word2vec模型以及我如何做到这一点?

Yes, it's possible since Word2Vec implementation defines a standard to structure its model. 是的,这可能是因为Word2Vec实现定义了一个标准来构建其模型。

To do this: 去做这个:

  1. Using gensim , save the model compatible with Word2Vec implementation : 使用gensim ,保存与Word2Vec实现兼容的模型:

     w2v_model.wv.save_word2vec_format("path/to/w2v_model.bin", binary=True) 
  2. From DL4J , load the same pre-trained model: DL4J开始 ,加载相同的预训练模型:

     Word2Vec w2vModel = WordVectorSerializer.readWord2VecModel("path/to/w2v_model.bin"); 

In fact, you could test the model in both codes and you should see the same results, for instance: 实际上,您可以在两个代码中测试模型,您应该看到相同的结果,例如:

With gensim: 使用gensim:

print(w2v_model.most_similar("love"))
print(w2v_model.n_similarity(["man"], ["king"]))

And with DL4J: 并使用DL4J:

System.out.println(w2vModel.wordsNearest("love", 10));
System.out.println(w2vModel.similarity("man", "king"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM