tf.random.normal() doesn't work as expected

541 views Asked by At

I am new on Python and tensorflow. I am a little (or a lot) puzzled: tf.random.normal() doesn't seem to work as expected.

tf.random.normal() is used to generate some data. I want to see the data are generated as expected by printing them on the screen through "tf.keras.backend.eval()".

randndata = tf.random.normal([1, 5])
print('randndata = ', tf.keras.backend.eval(randndata))

So far, everything seems fine. However, it gives a totally different result if the data are printed for the second time.

randndata = tf.random.normal([1, 5])
print('randndata = ', tf.keras.backend.eval(randndata))
print('randndata = ', tf.keras.backend.eval(randndata))

The issue is not with the normal distribution only. Uniform distribution has the same issue.

The tensorflow version is 2.1.0.

Thank you!

1

There are 1 answers

3
Poe Dator On

Garry, I was able to reproduce your error by disabling eager execution. In that case tf.random.normal gets called every time you run eval. With eager execution on, random numbers are assigned to the variable immediately and then they are just passed to tf.keras.backend.eval() calls.

Find where you might have disabled eager execution - this will explain the puzzling situation. Post more code here, if in doubt.

I suggest to use TF2 style with eager execution and not to call eval at all (or call it once if you have some complex calculation graph). read more on eager execution here