简体   繁体   English

在评估 model 时如何处理随机性?

[英]How do you deal with randomness when evaluating a model?

I'm recently training and comparing the performance of two deep learning models.我最近正在训练和比较两种深度学习模型的性能。 For now, I use a specific seed only when doing train-test split.目前,我仅在进行训练测试拆分时使用特定的种子。 However, due to the randomness of the model, even with the same seed the loss of the same model differs everytime.然而,由于 model 的随机性,即使使用相同的种子,相同 model 的损失每次都不同。 Is it better to set a seed everywhere to perfectly control the result or to keep the randomness?是在各处设置种子以完美控制结果还是保持随机性更好? If it's the latter, then should I test the same seed several times more and average the losses or should I choose the best/worst performance of the seed?如果是后者,那么我应该多次测试同一种子并平均损失还是应该选择种子的最佳/最差表现?

Also, I've read some conference papers and they usually evaluate a model by using some number of random seeds and averaging the results, and I wonder how the seeds were chosen.此外,我阅读了一些会议论文,他们通常通过使用一些随机种子并对结果进行平均来评估 model,我想知道这些种子是如何选择的。 If I want to compare two models, should I test them with the same seeds (eg. seed 0, 1, 2 for both model A and B) or different ones (eg. seed 0, 1, 2 for model A and seed 5, 6, 7 for model B) depending on the results?如果我想比较两个模型,我应该使用相同的种子(例如 model A 和 B 的种子 0、1、2)还是不同的种子(例如 model A 和种子 5 的种子 0、1、2 来测试它们, 6, 7 for model B) 取决于结果? That means, always choose the best one no matter what?这意味着,无论如何总是选择最好的? Thank you in advance.先感谢您。

For splitting the data, you should use the same seed for each model so that each model is trained and tested on identical data.为了拆分数据,您应该为每个 model 使用相同的种子,以便每个 model 都在相同的数据上进行训练和测试。

For training the models, it doesn't matter except when you are training two models with identical architecture: if these two models share the same seed, they will give the same results.对于模型的训练,除了训练具有相同架构的两个模型之外,这无关紧要:如果这两个模型共享相同的种子,它们将给出相同的结果。 So, when training, you should use distinct seeds for models of the same architecture.因此,在训练时,您应该为相同架构的模型使用不同的种子。

You can use numpy.random.SeedSequence to help with getting seeds, as using it you can keep track of a single seed but spawn unique random seeds:您可以使用numpy.random.SeedSequence来帮助获取种子,因为使用它可以跟踪单个种子,但会产生唯一的随机种子:

import numpy as np
entropy = 100
seed_sequence = np.random.SeedSequence(entropy, pool_size=5)
spawns = seed_sequence.spawn(2)
split_seeds = spawns[0].pool # for splitting train-test
# [179453401, 3816112049, 3806930416, 1196834953, 1391596624]
model_seeds = spawns[1].pool # for training models
# [2353154363,  511151844,    6211548, 1188290456, 3368787154]

Notice you need only keep track of entropy .请注意,您只需要跟踪entropy Set pool_size to however many times you want to repeat the experiment.pool_size设置为您想要重复实验的次数。

As noted in the documentation:如文档中所述:

Best practice for achieving reproducible bit streams is to use the default None for the initial entropy, and then use SeedSequence.entropy to log/pickle the entropy for reproducibility实现可重现比特流的最佳实践是使用默认的 None 作为初始熵,然后使用 SeedSequence.entropy 记录/腌制熵以实现重现性

So rather than specifying entropy = 100 on your first run, don't specify it at all and simply save seed_sequence.entropy after your first run.因此,与其在第一次运行时指定entropy = 100 ,不如在第一次运行后完全不指定它并简单地保存seed_sequence.entropy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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