Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Seed value error in multi_runs #209

Open
soudy opened this issue Oct 22, 2021 · 0 comments
Open

Seed value error in multi_runs #209

soudy opened this issue Oct 22, 2021 · 0 comments
Assignees
Labels

Comments

@soudy
Copy link

soudy commented Oct 22, 2021

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 numpy/numpy@6b1a120, 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants