1

1.我用IntelliJ IDEA搭建了一个maven项目,代码如下:</p>

    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);

这段代码参考了deeplearning4j中的word2vec,但结果不稳定。每次实验的结果差别很大。比如'day'和'night'相似度的余弦值,有时结果高达0.98,有时低至 0.4?

这是两个实验的结果

Evaluate model....
Similarity between day and night: 0.706292986869812

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

为什么会出现这样的结果。因为刚开始学习word2vec,有很多知识没看懂,希望前辈能帮帮我,谢谢!

4

1 回答 1

1

您已设置以下行:

.minLearningRate(1e-2) // learning rate decays wrt # words. floor learning

但这是一个极高的学习率。高学习率导致模型不会“稳定”在任何状态下,而是一些更新显着改变了学习的表示。在最初的几次更新中这不是问题,但不利于收敛。

解决方案:允许学习率衰减。您可以完全忽略此行,或者如果必须,您可以使用更合适的值,例如 1e-15

于 2015-12-24T13:12:01.687 回答