numpy.random and multiprocessing

2,246 views
Skip to first unread message

Achilleas

unread,
Jul 20, 2011, 7:33:19 AM7/20/11
to Brian
This is something that is probably well known, but I fell into this
pitfall recently while working with Brian and I think it would be
appropriate to mention here as well.

The discussion on the numpy mailing list on the subject can be found
here:
http://mail.scipy.org/pipermail/numpy-discussion/2008-December/039179.html

In brief, when using numpy.random to generate random numbers in an
individual process using the multiprocessing module, "the different
instances of the RNG are created by forking from the main process".
This causes all sub-processes to use the same seed and generate the
same sequence of numbers.

The output of the following program:
http://pastebin.com/QhfSzgLy
is:
n: 0 r: 0.985201182352
n: 1 r: 0.985201182352
n: 2 r: 0.985201182352
n: 3 r: 0.985201182352
n: 4 r: 0.985201182352
n: 5 r: 0.985201182352
n: 6 r: 0.985201182352
n: 7 r: 0.985201182352
n: 8 r: 0.985201182352
n: 9 r: 0.985201182352


In brian now, this causes problems when trying to run something like
the following:
http://pastebin.com/WrWsP3tL
The 4 neurons always behave exactly the same between them and fire at
the same times.

This can be remedied by importing numpy and adding
"numpy.random.seed()" before defining the neuron, e.g., before line 17
in the second example.

Again, sorry if I'm wasting space with common knowledge, but I spent
about a day trying to figure this out after stumbling on the numpy
discussion thread. I figured since brian uses numpy's RNG that people
using multiprocessing may fall into a similar trap.

Bertrand Fontaine

unread,
Jul 20, 2011, 7:48:03 AM7/20/11
to brians...@googlegroups.com
Hi,

thanks for the tip. We were indeed aware of this problem which is only present on Unix machine as the seed is passed when forked but not on windows.

To be entirely sure that the results are different, it is even better to seed your programs with a parameter that will be different for each process, like:

numpy.random.seed(int(some_parameter*1000))

where some_parameter take a different value for each sub process.


Thanks anyway,

Bertrand




--
You received this message because you are subscribed to the Google Groups "Brian" group.
To post to this group, send email to brians...@googlegroups.com.
To unsubscribe from this group, send email to briansupport...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/briansupport?hl=en.


Romain Brette

unread,
Jul 20, 2011, 7:51:08 AM7/20/11
to brians...@googlegroups.com
Hi Achilleas,

Thanks for your post! It will certainly be useful to a number of people.
It rings a bell, I think this is something we ran into with the model
fitting toolbox.

Romain

Le 20/07/2011 13:33, Achilleas a �crit :

Bertrand Fontaine

unread,
Jul 20, 2011, 7:51:37 AM7/20/11
to brians...@googlegroups.com

I forgot, if you want the results to be different between launches, the parameters given to the seed function needs to be different each time, so you can do:

from time import time

numpy.random.seed(int((time()+some_parameter*1000))

Note that you write codes that will be porter on other os, you can  make sure that this trick is only done for Unix system

import os
if os.name=='posix':
    numpy.random.seed(int((time()+some_parameter*1000))


Cheers,

Bertrand

achil...@gmail.com

unread,
Jul 20, 2011, 7:55:19 AM7/20/11
to brians...@googlegroups.com
Thanks for the clarifications and the extra tips Bertrand.

achil...@gmail.com

unread,
Jul 20, 2011, 7:56:57 AM7/20/11
to brians...@googlegroups.com
Hello Romain,

You're quite welcome.
I figured some people may not be lucky enough to stumble upon the numpy discussion.

Achilleas
Reply all
Reply to author
Forward
0 new messages