![](/img/trans.png)
[英]word not in vocabulary after training gensim word2vec model, why?
[英]how to update vocabulary after training Word2vec model using deeplearning4java
我使用 deeplearning4j 庫使用 word2vec 執行矢量化任務。 在使用特定語料庫訓練 model 后,我需要對新詞進行矢量化。 那么如何添加這個新詞並更新訓練以獲得新詞的新權重向量呢? 我的代碼如下:
Test word2Vec = new Test();
word2Vec.train();
//test the generated trained file
Word2Vec word2VecModel = WordVectorSerializer.readWord2VecModel(new File(word2Vec.modelFilePath));
double cosi=word2VecModel.similarity("httpdbpediaorgresourcethe_terminator", "httpdbpediaorgresourceterminator_salvation");
System.out.println(cosi);
}
public void train() throws IOException {
SentenceIterator sentenceIterator = new FileSentenceIterator(new File(inputFilePath));
TokenizerFactory tokenizerFactory = new DefaultTokenizerFactory();
tokenizerFactory.setTokenPreProcessor(new CommonPreprocessor());
Word2Vec vec = new Word2Vec.Builder()
.layerSize(100)
.windowSize(5)
.epochs(5) //3-50 https://arxiv.org/pdf/1301.3781.pdf
.elementsLearningAlgorithm(new SkipGram<VocabWord>())
.iterate(sentenceIterator)
.tokenizerFactory(tokenizerFactory)
.build();
vec.fit();
WordVectorSerializer.writeWordVectors(vec, modelFilePath);
}
Deeplearning4j 能夠更新權重,但不會添加新詞。 一般不建議這樣做。 只添加單數句子實際上並不能很好地訓練好的詞向量。
無論如何,培訓通常不會花費太長時間。 我建議只用新詞訓練權重。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.