ndlib
ndlib copied to clipboard
Seed value error in multi_runs
When using multi_runs
, some of my classmates got the following error:
Traceback (most recent call last):
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\multiprocessing\pool.py", line 125, in worker
result = (True, func(*args, **kwds))
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\ndlib\utils.py", line 71, in __execute
np.random.seed(seed)
File "mtrand.pyx", line 246, in numpy.random.mtrand.RandomState.seed
File "_mt19937.pyx", line 166, in numpy.random._mt19937.MT19937._legacy_seeding
File "_mt19937.pyx", line 180, in numpy.random._mt19937.MT19937._legacy_seeding
ValueError: Seed must be between 0 and 2**32 - 1
"""
The above exception was the direct cause of the following exception:
ValueError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_12920/4199588637.py in <module>
18
19 # Simulation multiple execution
---> 20 trends = multi_runs(model1, execution_number=10, iteration_number=100, infection_sets=None, nprocesses=4)
~\AppData\Local\Programs\Python\Python39\lib\site-packages\ndlib\utils.py in multi_runs(model, execution_number, iteration_number, infection_sets, nprocesses)
56
57 for result in results:
---> 58 executions.append(result.get())
59
60 return executions
~\AppData\Local\Programs\Python\Python39\lib\multiprocessing\pool.py in get(self, timeout)
769 return self._value
770 else:
--> 771 raise self._value
772
773 def _set(self, i, obj):
ValueError: Seed must be between 0 and 2**32 - 1
Numpy added this check in https://github.com/numpy/numpy/commit/6b1a1205eac6fe5d162f16155d500765e8bca53c, because seeds larget than 32 bits get truncated back to 32 bit, leading to the same stream of random numbers for two different seeds. A quick and dirty fix I did was changing line 71 in ndlib/utils.py
as follows:
- np.random.seed(seed)
+ np.random.seed(seed % 2**32)
This will results in the multiple seeds leading to the same stream of random numbers, so you may want to change the way you generate seeds to fall between 0 and 2**32-1 instead.