[Numpy-discussion] reseed random generator (1.19)
Evgeni Burovski
evgeny.burovskiy at gmail.com
Mon Jun 29 11:00:54 EDT 2020
Thanks Kevin!
A possibly dumb follow-up question: in your example,
> entropy = 382193877439745928479635728
is it relevant that `entropy` is a long integer? I.e., what are the
constraints on its value, can one use
entropy = 1234 or
entropy = 0 or
entropy = 1
instead?
On Mon, Jun 29, 2020 at 5:37 PM Kevin Sheppard
<kevin.k.sheppard at gmail.com> wrote:
>
> The best practice is to use a SeedSequence to spawn child SeedSequences, and then to use these children to initialize your generators or bit generators.
>
>
>
>
>
> from numpy.random import SeedSequence, Generator, PCG64, default_rng
>
>
>
> entropy = 382193877439745928479635728
>
>
>
> seed_seq = SeedSequence(entropy)
>
> NUM_STREAMS = 2**15
>
> children = seed_seq.spawn(NUM_STREAMS)
>
> # if you want the current best bit generator, which may change
>
> rngs = [default_rng(child) for child in children]
>
> # If you want the most control across version, set the bit generator
>
> # this uses PCG64, which is the current default. Each bit generator needs to be wrapped in a generator
>
> rngs = [Generator(PCG64(child)) for child in children]
>
>
>
> Kevin
>
>
>
>
>
> From: Evgeni Burovski
> Sent: Monday, June 29, 2020 2:21 PM
> To: Discussion of Numerical Python
> Subject: Re: [Numpy-discussion] reseed random generator (1.19)
>
>
>
> (apologies for jumping into a conversation)
>
> So what is the recommendation for instantiating a number of generators
>
> with manually controlled seeds?
>
>
>
> The use case is running a series of MC simulations with reproducible
>
> streams. The runs are independent and are run in parallel in separate
>
> OS processes, where I do not control the time each process starts
>
> (jobs are submitted to the batch queue), so default seeding seems
>
> dubious?
>
>
>
> Previously, I would just do roughly
>
>
>
> seeds = [1234, 1235, 1236, ...]
>
> rngs = [np.random.RandomState(seed) for seed in seeds]
>
> ...
>
> and each process operates with its own `rng`.
>
> What would be the recommended way with the new `Generator` framework?
>
> A human-friendly way would be preferable if possible.
>
>
>
> Thanks,
>
>
>
> Evgeni
>
>
>
>
>
> On Mon, Jun 29, 2020 at 3:20 PM Kevin Sheppard
>
> <kevin.k.sheppard at gmail.com> wrote:
>
> >
>
> > If you want to use the same entropy-initialized generator for temporarily-reproducible experiments, then you can use
>
> >
>
> >
>
> >
>
> > gen = np.random.default_rng()
>
> >
>
> > state = gen.bit_generator.state
>
> >
>
> > gen.standard_normal()
>
> >
>
> > # 0.5644742559549797, will vary across runs
>
> >
>
> > gen.bit_generator.state = state
>
> >
>
> > gen.standard_normal()
>
> >
>
> > # Always the same as before 0.5644742559549797
>
> >
>
> >
>
> >
>
> > The equivalent to the old way of calling seed to reseed is:
>
> >
>
> >
>
> >
>
> > SEED = 918273645
>
> >
>
> > gen = np.random.default_rng(SEED)
>
> >
>
> > gen.standard_normal()
>
> >
>
> > # 0.12345677
>
> >
>
> > gen = np.random.default_rng(SEED)
>
> >
>
> > gen.standard_normal()
>
> >
>
> > # Identical value
>
> >
>
> >
>
> >
>
> > Rather than reseeding the same object, you just create a new object. At some point in the development of Generator both methods were timed and there was no performance to reusing the same object by reseeding.
>
> >
>
> >
>
> >
>
> > Kevin
>
> >
>
> >
>
> >
>
> >
>
> >
>
> >
>
> >
>
> > From: Neal Becker
>
> > Sent: Monday, June 29, 2020 1:01 PM
>
> > To: Discussion of Numerical Python
>
> > Subject: Re: [Numpy-discussion] reseed random generator (1.19)
>
> >
>
> >
>
> >
>
> > I was using this to reset the generator, in order to repeat the same sequence again for testing purposes.
>
> >
>
> >
>
> >
>
> > On Wed, Jun 24, 2020 at 6:40 PM Robert Kern <robert.kern at gmail.com> wrote:
>
> >
>
> > On Wed, Jun 24, 2020 at 3:31 PM Neal Becker <ndbecker2 at gmail.com> wrote:
>
> >
>
> > Consider the following:
>
> >
>
> >
>
> >
>
> > from numpy.random import default_rng
>
> > rs = default_rng()
>
> >
>
> >
>
> >
>
> > Now how do I re-seed the generator?
>
> >
>
> > I thought perhaps rs.bit_generator.seed(), but there is no such attribute.
>
> >
>
> >
>
> >
>
> > In general, reseeding an existing generator instance is not a good practice. What effect are you trying to accomplish? I assume that you are asking this because you are currently using `RandomState.seed()`. In what circumstances?
>
> >
>
> >
>
> >
>
> > The raw `bit_generator.state` property *can* be assigned to, in order to support some advanced use cases (mostly involving de/serialization and similar kinds of meta-programming tasks). It's also been helpful for me to construct worst-case scenarios for testing parallel streams. But it quite deliberately bypasses the notion of deriving the state from a human-friendly seed number.
>
> >
>
> >
>
> >
>
> > --
>
> >
>
> > Robert Kern
>
> >
>
> > _______________________________________________
>
> > NumPy-Discussion mailing list
>
> > NumPy-Discussion at python.org
>
> > https://mail.python.org/mailman/listinfo/numpy-discussion
>
> >
>
> >
>
> >
>
> >
>
> > --
>
> >
>
> > Those who don't understand recursion are doomed to repeat it
>
> >
>
> >
>
> >
>
> > _______________________________________________
>
> > NumPy-Discussion mailing list
>
> > NumPy-Discussion at python.org
>
> > https://mail.python.org/mailman/listinfo/numpy-discussion
>
> _______________________________________________
>
> NumPy-Discussion mailing list
>
> NumPy-Discussion at python.org
>
> https://mail.python.org/mailman/listinfo/numpy-discussion
>
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at python.org
> https://mail.python.org/mailman/listinfo/numpy-discussion
More information about the NumPy-Discussion
mailing list