【问题标题】:I used the word2vec in deeplearning4j to train vectors, but results are unstable我在deeplearning4j中使用word2vec训练向量,但结果不稳定
【发布时间】:2015-12-11 18:57:40
【问题描述】:

1.我正在使用 IntelliJ IDEA 构建一个 maven 项目。代码如下:

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

这段代码引用了deeplearning4j中的word2vec,但结果不稳定。每个实验的结果都大相径庭。例如,以'day'和'night'相似度的余弦值,有时结果高达0.98,有时低至0.5。

以下是两个实验的结果:

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

为什么结果是这样的?刚开始学习word2vec,有很多东西没看懂,希望前辈能帮帮我。

【问题讨论】:

标签: java word2vec


【解决方案1】:

你在处理 0.4 的例子吗?

如果没有,请查看这个 W2V:

github.com/deeplearning4j/dl4j-0.4-examples

您可以运行具有不同随机初始化权重的示例,但不同的随机初始化可能会导致不同的结果。

您可以通过添加一个附加参数:.seed(x),每次运行模型时使用相同的随机初始权重为模型播种,其中 x 是一个类似 42 的数字。

【讨论】:

    猜你喜欢
    • 2015-12-07
    • 1970-01-01
    • 2016-09-05
    • 1970-01-01
    • 1970-01-01
    • 2019-07-12
    • 2015-12-21
    • 1970-01-01
    • 2021-10-20
    相关资源
    最近更新 更多