简体   繁体   中英

I used the word2vec in deeplearning4j to train vectors, but results are unstable

1.I am using IntelliJ IDEA to build a maven project. The code is as follows:

public class Test1 {
public static void main(String[] args) throws IOException {

    System.out.println("Load data....");
    SentenceIterator iter = new LineSentenceIterator(new File("/home/zs/programs/deeplearning4j-master/dl4j-test-resources/src/main/resources/raw_sentences.txt"));
    iter.setPreProcessor(new SentencePreProcessor() {
        @Override

            return sentence.toLowerCase();
        }
    });



    System.out.println("Build model....");
    int batchSize = 1000;
    int iterations = 30;
    int layerSize = 300;
    com.sari.Word2Vec vec= new  com.sari.Word2Vec.Builder()
            .batchSize(batchSize) //# words per minibatch.
            .sampling(1e-5) // negative sampling. drops words out
            .minWordFrequency(5) //
            .useAdaGrad(false) //
            .layerSize(layerSize) // word feature vector size
            .iterations(iterations) // # iterations to train
            .learningRate(0.025) //
            .minLearningRate(1e-2) // learning rate decays wrt # words. floor learning
            .negativeSample(10) // sample size 10 words
            .iterate(iter) //
            .tokenizerFactory(tokenizer)
            .build();
    vec.fit();

    System.out.println("Evaluate model....");

    double cosSim = vec.similarity("day" , "night");
    System.out.println("Similarity between day and night: "+cosSim);

This code is referring the word2vec in deeplearning4j,but the result is unstable. The results of each experiment were very different. For example, with the cosine value of the similarity between 'day' and 'night', sometimes the result is as high as 0.98, sometimes as low as 0.5.

Here are the results of two experiments:

Evaluate model....
Similarity between day and night: 0.8252374529838562
Evaluate model....
Similarity between day and night: 0.5550910234451294

Why the result is like this? I have just started learning word2vec, there are a lot of things that are not understood, I hope that the seniors can help me.

Are you working with the 0.4 examples?

If not, please check out this one for W2V:

github.com/deeplearning4j/dl4j-0.4-examples

You can run examples with different randomly initialized weights, but different random initializations can lead to different results.

You can seed the model with the same random initial weights each time you run it by adding an additional parameter: .seed(x), where x is a number like 42.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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