1

Is there a way to read the "state" of the random number generator in a jupyter notebook?

For example, if I ran a cell where I specify a neural network architecture, and then train it on some data without specifying a seed, is there a way I can then read what seed was used to run this?

desertnaut
  • 46,107
  • 19
  • 109
  • 140
carefullynamed
  • 397
  • 3
  • 15
  • 2
    As far as I know you can't, the easiest solution is to set the seed before you train, so that you know the seed. [Here](https://stackoverflow.com/questions/32172054/how-can-i-retrieve-the-current-seed-of-numpys-random-number-generator) a similar question – gionni Jul 25 '17 at 14:35

1 Answers1

2

You can indeed read (and store) the current state of the RNG, but this changes every time it is used, i.e. you cannot do what you describe after you have run the cell.

Here is an example (since you have tagged the question withkeras, I assume you are actually interested in the Numpy RNG, which is the one used in Keras):

import numpy as np
current_state = np.random.get_state()

# produce some random numbers:
a = np.random.randn(3)
a
# array([-0.44270351, 1.42933504, 2.11385353])

# Now, restoring the RNG state and producing again 3 random numbers, you get the same result:

np.random.set_state(current_state)
b = np.random.randn(3)
b
# array([-0.44270351, 1.42933504, 2.11385353])
a == b
# array([ True, True, True], dtype=bool)
desertnaut
  • 46,107
  • 19
  • 109
  • 140