0

I'm using Google Colab, and I'm using numpy.random.seed() and tensorflow.set_seed() with Keras v2 and Tensorflow v2.x.

However, it still gives me different results in different runs. Is there anything else that I might need to set?

I specify an argument in both of them, I don't leave them without arguments.

np.random.seed(1) # this is in a notebook

... # this is a function called in the notebook

tf.random.set_seed(3)

self.model.fit_generator(
        random_cropper(datagen(X, Y)),
        steps_per_epoch=STEPS_PER_EPOCH,
        epochs=EPOCHS,
        verbose=2,
        callbacks=callbacks
)

The output, which are the results for each epoch printed by fit_generator are not identical

NoImaginationGuy
  • 1,669
  • 10
  • 22
  • Can you provide some example code of what you're trying and the output you're seeing? – Tyler Jul 16 '20 at 00:46
  • @Tyler done, hope I gave you enough information – NoImaginationGuy Jul 16 '20 at 00:50
  • @NoImaginationGuy I don't know Colab, but if it is like in Jupyter, are the `numpy.random.seed()` in the same cell than the line you generate the numbers? – Ben.T Jul 16 '20 at 00:52
  • 1
    @Ben.T Yes, I call np.random.seed and then a function contained in a python file that contains the other lines, all in the same cell of the notebook – NoImaginationGuy Jul 16 '20 at 00:53
  • @NoImaginationGuy are the random number generated in the function from the python file? – Ben.T Jul 16 '20 at 01:00
  • @Ben.T yes, both randomness from numpy and tensorflow is extracted and used in the python file. Notebooks just call functions. Also let me point out that I don't explicitly extract randomness from tensorflow, I just call fit_generator which I'm assuming it is using tensorflow's random generator. – NoImaginationGuy Jul 16 '20 at 01:02

1 Answers1

0

Are you setting a specific number with the call to numpy.random.seed()? With no input, you are just reinitializing, but not specifying the seed (source from Numpy). As TensorFlow says:

If neither the global seed nor the operation seed is set: A randomly picked seed is used for this op

Try passing an integer into the function like numpy.random.seed(42) or similar tensroflow.random.set_seed(42). (by the way, tf.random.set_seed, not tf.set_seed)

That will set the global seed. You will also want to set an operation seed for specific operations like tensorflow.random.uniform([1], seed=1).

For more info, this question may be a duplicate of this other stackoverflow question: Reproducible results in Tensorflow with tf.set_random_seed

Tyler
  • 947
  • 9
  • 19
  • sorry, i will edit the question to specify this. Yes, I give it a number – NoImaginationGuy Jul 16 '20 at 00:42
  • the per-operation seed is interesting and useful, thanks. But the point is that I need to fix the seed for a fit_generator in Keras – NoImaginationGuy Jul 16 '20 at 00:51
  • This is not a simple problem. This link https://stackoverflow.com/questions/42022950/which-seeds-have-to-be-set-where-to-realize-100-reproducibility-of-training-res has some explanation. There are all kinds of things that can lead to a degree of randomness in the process. – Gerry P Jul 16 '20 at 03:19