在使用Python中的random.Random(0)保持模拟确定性方面存在问题 [英] Having problems keeping a simulation deterministic with random.Random(0) in python

查看:268
本文介绍了在使用Python中的random.Random(0)保持模拟确定性方面存在问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在python中进行了非常大的模拟,其中包含许多模块.我调用了许多随机函数.为了保持相同的随机结果,我有一个变量keep_seed_random.

I have a very large simulation in python with lots of modules. I call a lot of random functions. To keep the same random results I have a variable keep_seed_random.

如此:

import random

keep_seed_random = True

if keep_seed_random is False:
    fixed_seed = random.Random(0)
else:
    fixed_seed = random

然后,我在整个程序中都使用了fixed_seed,例如

Then I use fixed_seed all over the program, such as

fixed_seed.choice(['male', 'female'])
fixed_seed.randint()
fixed_seed.gammavariate(3, 3)
fixed_seed.random()
fixed_seed.randrange(20, 40)

以此类推...

它曾经工作得很好. 但是现在,该程序太大了,即使在我选择keep_seed_random = False

It used to work well. But now, that the programme is too large, there is something else interfering and the results are no longer identical, even when I choose keep_seed_random = False

P.S.我只随机导入一次.

P.S. I import random just once.

我们一直在试图查明程序从完全相同的结果变为不同结果的确切时间.似乎是在我们引入大量与随机模块无关的数据库读取的时候.

We have been trying to pinpoint the exact moment when the program turned from exact same results to different results. It seemed to be when we introduced a lot of reading of databases with no connection to random modules.

现在,结果在两个相似的结果之间交替显示. 也就是说,我运行main.py一旦获得了GDP的8148.78的结果 我再次运行我得到7851.49 再次8148.78回来 再次7851.49

The results now ALTERNATE among two similar results. That is, I run main.py once get a result of 8148.78 for GDP I run again I get 7851.49 Again 8148.78 back Again 7851.49

对于工作版本,在更改之前,第一个结果(当我们创建实例并对其进行腌制时)我得到了一个结果.然后,从第二个开始,结果是相同的.因此,我想这与泡菜读取/加载有关.

Also for the working version, before the change, the first result (when we create instances and pickle save them) I get one result. Then, from the second onwards the results are the same. So, I am guessing it is related to pickle reading/loading.

问题仍然存在!

我们部分发现了问题. 问题是当我们创建实例并先进行腌制转储,然后进行腌制加载时.

We partially found the problem. The problem is when we create instances and pickle dump and then pickle load.

我们仍然无法获得与创建和加载完全相同的结果. 但是,当重复加载时,结果是准确的.

We still cannot have the exact same results for creating and just loading. However, when loading repeatedly the results are exact.

因此,问题出在PICKLE 转储和加载时可能会发生一些随机化(我想).

Thus, the problem is in PICKLE Some randomization may occur when dumping and loading (I guess).

谢谢

推荐答案

如@ mart0903所述,如果没有好的复制案例,这很难诊断.但是,一般而言,可能会出现多种随机性来源.我想到了几件事:

This is difficult to diagnose without a good reproduce case as @mart0903 mentions. However, in general, there are several sources of randomness that can occur. A few things come to mind:

例如,如果您使用multiprocessing和/或subprocess程序包产生多个并行进程,则可能是在遇到竞争状况.也就是说,每次您运行该程序时,不同的进程将在不同的时间完成.也许您正在以某种方式组合结果,这取决于以特定顺序执行的这些线程.

If for example you are using the multiprocessing and/or subprocess packages to spawn several parallel processes, you may be experiencing a race condition. That is, different processes finishing at different times each time you run the program. Perhaps you are combining the result in some way that is dependent on these threads executing in a particular order.

也许您只是在字典上循环,并期望键是按一定顺序排列的,而实际上,字典是不按顺序排列的.例如,连续几次运行以下命令(如果有必要,我将使用Python 3.5),您会注意到键值对每次都以不同的顺序打印:

Perhaps you are simply looping over a dictionary and expecting the keys to be in a certain order, when in fact, dictionaries are not ordered. For example run the following a couple times in a row (I'm using Python 3.5 in case it matters) and you'll notice that the key-value pairs print out in a different order each time:

if __name__=='__main__':
    data = dict()
    data['a'] = 6
    data['b'] = 7
    data['c'] = 42
    for key in data:
        print(key + ' : ' + str(data[key]))

您甚至可能正在查看时间戳或设置一些值,或者可能在计算中使用的某个地方生成uuid.

You might even be looking at time-stamps or set some value, or perhaps generating a uuid somewhere that you are using in a calculation.

可能性还在继续.但是同样,如果没有简单的复制盒,就很难确定.可能只需要一些好点的断点和大量的代码步即可.

The possibilities could go on. But again, difficult to nail down without a simple reproduce case. It may just take some good-ol breakpoints and a lot of stepping through code.

祝你好运!

这篇关于在使用Python中的random.Random(0)保持模拟确定性方面存在问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆