是否可以在DeepLearning4J.Word2Vec中使用Gensim Word2Vec模型



我是DeepLearning4J的新手,我想使用单词向量作为分类器的输入来制作句子分类器。我以前在使用Python,在该Python使用Gensim生成了向量模型,我想将该模型用于该新分类器。是否可以在DeepLearning4J.Word2Vec中使用Gensim的Word2Vec模型,以及我如何做?

是的,由于Word2Vec实现定义了构建其模型的标准。

这样做:

  1. 使用 Gensim ,保存模型与Word2Vec实现兼容

    w2v_model.wv.save_word2vec_format("path/to/w2v_model.bin", binary=True)
    
  2. 来自 dl4j ,加载相同的预训练模型:

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

实际上,您可以在两个代码中测试模型,例如,您应该看到相同的结果,例如:

使用Gensim:

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

和dl4j:

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

最新更新