funkelab / gunpowder

A library to facilitate machine learning on multi-dimensional images.
https://funkelab.github.io/gunpowder/
MIT License
78 stars 56 forks source link

set random seed at start of each worker process #77

Closed abred closed 4 years ago

abred commented 4 years ago

numpy's seed is set at import, when new processes are created the import is not repeated, thus all have the same seed/give the same random numbers (this behavior is different from python's random package) Related to issue #74

abred commented 4 years ago
import multiprocessing as mp
import numpy as np
def test():
    print(np.random.randint(100, size=10))

for i in range(5):
    p = mp.Process(target=test)
    p.start()

def test2():
    np.random.seed(None)
    print(np.random.randint(100, size=10))

for i in range(5):
    p = mp.Process(target=test2)
    p.start()

Output: [21 40 51 50 24 9 43 95 43 83] [21 40 51 50 24 9 43 95 43 83] [21 40 51 50 24 9 43 95 43 83] [21 40 51 50 24 9 43 95 43 83] [21 40 51 50 24 9 43 95 43 83]

[18 68 36 31 14 25 5 25 74 21] [60 61 65 42 26 34 27 58 30 82] [78 35 48 96 97 79 0 26 45 53] [35 11 48 1 73 19 96 34 0 55] [18 17 2 84 45 51 74 84 46 57]