Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using seed to sample in tensorflow-probability

I am trying to use tensorflow-probability and started off with something really simple:

import tensorflow as tf
import tensorflow_probability as tfp

tf.enable_eager_execution()

tfd = tfp.distributions
poiss = tfd.Poisson(0.8)

poiss.sample(2, seed=1)
#> Out: <tf.Tensor: id=3569, shape=(2,), dtype=float32, numpy=array([0., 0.], dtype=float32)>

poiss.sample(2, seed=1)
#> Out: <tf.Tensor: id=3695, shape=(2,), dtype=float32, numpy=array([1., 0.], dtype=float32)>

poiss.sample(2, seed=1)
#> Out: <tf.Tensor: id=3824, shape=(2,), dtype=float32, numpy=array([2., 2.], dtype=float32)>

poiss.sample(2, seed=1)
#> Out: <tf.Tensor: id=3956, shape=(2,), dtype=float32, numpy=array([0., 1.], dtype=float32)>

I was thinking I would get the same results when re-using the same seed, but somehow that's not true.

I also tried without eager execution, but the results still weren't reproducible. Same story if I add something like tf.set_random_seed(12).

I suppose there is something basic I am missing?

For those interested, I am running Python 3.5.2 on Ubuntu 16.04 with

tensorflow-probability==0.5.0
tensorflow==1.12.0

like image 432
RolandASc Avatar asked Mar 05 '23 13:03

RolandASc


1 Answers

For deterministic output in graph mode, you need to set both the graph random seed (tf.set_random_seed) and the op random seed (seed= in your sample call).

The workings of random samplers in TFv2 are still being sorted out. For now, my best understanding is that you can call tf.set_random_seed prior to each call to a sampler, and pass the sampler a seed=, if you want deterministic output in eager.

like image 136
Brian Patton Avatar answered Mar 12 '23 21:03

Brian Patton