Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Seeding breaks random variate generation via copy #501

Closed
chmp opened this issue Mar 4, 2017 · 2 comments · Fixed by #617
Closed

Seeding breaks random variate generation via copy #501

chmp opened this issue Mar 4, 2017 · 2 comments · Fixed by #617

Comments

@chmp
Copy link
Contributor

chmp commented Mar 4, 2017

Edward relies on edward.copy to generate new variates of random variables (to quote klqp.py: Copy q(z) to obtain new set of posterior samples). Calling edward.set_seed does seem to prevent this mechanism from working properly. Minimal example:

import tensorflow as tf
import edward as ed

do_seed = True
if do_seed:
    ed.set_seed(42)

op = tf.random_normal([3])
copy_op = ed.copy(op)
print(ed.get_session().run([op, copy_op]))

# do_seed == False: [
#   array([-1.33050728, -0.36147749,  0.19219629], dtype=float32), 
#   array([ 0.32038462, -0.05204332, -1.20637977], dtype=float32)
# ]

# do_seed == True: [
#   array([ 0.80214548,  0.50567901,  1.81201267], dtype=float32), 
#   array([ 0.80214548,  0.50567901,  1.81201267], dtype=float32)
# ]

A similar behavior can be observed when replacing op with ed.models.Normal(0.0, 1.0).value(). With seed it generates both two equal numbers (for example: 0.1395757 and 0.1395757). Without seed two different variates are generated (for example: -0.64106858, 0.47237226).

Versions:

python: 3.5.1
tensorflow: 1.0.0
edward: 1.2.2
@dustinvtran
Copy link
Member

Thanks for this catch.

This doesn't break when explicitly copying random variables (and not their tensors). This is whatKLqp and related algorithms do, so fortunately they work as intended.

import tensorflow as tf
import edward as ed
from edward.models import Normal

do_seed = True
if do_seed:
    ed.set_seed(42)

op = Normal(tf.zeros(3), tf.ones(3))
copy_op = ed.copy(op)
print(ed.get_session().run([op.value(), copy_op.value()]))

# do_seed == True: [
#  array([ 0.1395757 ,  1.83572245, -0.27154389], dtype=float32), 
#  array([-1.425722  , -1.01437879,  0.39387721], dtype=float32)
# ]

The consequence of random tf.Tensors + seed setting is mysterious. For all use cases I imagine for copy, I think it always makes sense for the copied tensor to output a different random value. So we should fix that.

@chmp
Copy link
Contributor Author

chmp commented Mar 4, 2017

Interesting. I didn't see this, since I was experimenting with bypassing edward and setting the value of the random variable directly (via value=...). In this case, the variates end up the same also - I guess unsurprising given the implementation :).

My theory of why this happens: I assume the seed for the value is copied as well, when performing the copy. Whereas, copying a random variable without a fixed value at construction only copies the parameters, since its value was never added to the _kwargs attribute.

For reference: I'm trying something akin to ed.models.Normal(0.0, 1.0, value=tf.random_normal([])).

dustinvtran added a commit that referenced this issue Apr 19, 2017
* rename ret to new_op

* rename python variables in copy; update docstrings

* change op-level seed during copy; fix #501
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants