1

I have a code using tensorflow v1 and I'd like to migrate it toward native tensorflow 2. The code defines random objects (using numpy.randomor random, a neural network (keras weight initialization etc) and other tensorflow's random functions. At the end, it makes predictions on a random test set and outputs loss/accuracy of the model.

For this task, I'm having the original code and a copy of it and I'm changing the code of the copy part by part. I want to make sure that the behaviour is the same so I want to set the randomness so that I can monitor if the loss/accuracy change

However, even after setting the seeds of the various random modules in my original file, launching it multiple times still give different loss/accuracy

here are my libraries :

import time
import random
import my_file as mf // file in directory scope
import numpy as np
import copy
import os
from matplotlib import pyplot as plt
import tensorflow.compat.v1 as tf

and I'm setting the seeds at the beginning like that :

tf.set_random_seed(42)
random.seed(42)
np.random.seed(42)

My module my_file uses the random library and I'm also setting the seed there

I do understand from the docs that tf.set_random_seed only sets the global seed and that each random operation in tensorflow is also using its own seed, resulting in different behaviors for consecutive calls. For example if I call the training/testing cell 3 times I get the consecutive value of losses L1 -> L2 -> L3 However, this should still result in the same behavior if I restart the environment so why isn't it the case ? If I restart the kernel and execute 3 times I will get L1' =/= L1 -> L2' =/= L2 -> L3' =/= L3

What else should I verify to make sure the behaviour is the same everytime I restart the notebook kernel ?

Aydin Abiar
  • 334
  • 3
  • 11
  • Related to [earlier SO question #36288235](https://stackoverflow.com/questions/36288235/how-to-get-stable-results-with-tensorflow-setting-random-seed), with 8 answers. – jpmarinier Mar 07 '22 at 12:28
  • Thank you, the best answer worked. However, I just have a 10^-1 range error on the test losses and 10^-5 range errors on the test accuracy. As suggested in the best answer, this may be because of floating point arithmetic. Should I delete my post or should someone answer it by giving the original question ? – Aydin Abiar Mar 07 '22 at 21:32
  • @AydinAbiar ! I think even if you use tf.random_set seed , you are going to get different values because of usage of np.random or other tf.random . Can you please share a minimal reproducible code to investigate the issue though? –  Apr 06 '22 at 07:45

0 Answers0