【问题标题】:Using seed to sample in tensorflow-probability使用种子对张量流概率进行采样
【发布时间】:2019-02-11 13:47:23
【问题描述】:

我正在尝试使用 tensorflow-probability 并从一些非常简单的东西开始:

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)>

我以为重复使用相同的种子会得到相同的结果,但不知何故,这不是真的。

我也尝试过不执行eager,但结果仍然无法重现。如果我添加 tf.set_random_seed(12) 之类的内容,也是同样的情况。

我想我缺少一些基本的东西?

对于那些感兴趣的人,我在 Ubuntu 16.04 上运行 Python 3.5.2

张量流概率==0.5.0
张量流==1.12.0

【问题讨论】:

    标签: tensorflow random tensorflow-probability


    【解决方案1】:

    对于图形模式下的确定性输出,您需要同时设置图形随机种子 (tf.set_random_seed) 和操作随机种子(示例调用中的 seed=)。

    TFv2 中随机采样器的工作原理仍然是sorted out。目前,我最好的理解是您可以在每个调用采样器之前调用tf.set_random_seed,如果您想要确定性输出,将采样器传递给seed=急于求成。

    【讨论】:

    • 感谢您的提示和有用的链接。所以确实对于eager,我必须在每个调用采样器之前调用tf.set_random_seed(我是否将种子传递给采样器并不重要,那么就再现性而言)。
    • 至于图形模式,如果我留在会话中,我需要继续调用tf.set_random_seed。然后,我的后续问题是传递给操作种子的特定整数是否有任何副作用? IE。如果我将相同的种子传递给多个操作,这有关系吗?也很高兴获得更广泛文档的链接。
    【解决方案2】:

    现在更简洁了,我们支持 TFP 中的完全确定性随机性。您可以传递一个包含两个整数的元组作为种子,或者传递一个形状为 (2,) 的张量来触发确定性行为。 tfp.random.split_seed 在这里也很重要。

    【讨论】:

      【解决方案3】:

      除了在 mcmc 中为 sample 或 sample_chain 设置种子之外,您可能还需要设置以下内容:

      seed = 24
      os.environ['TF_DETERMINISTIC_OPS'] = 'true'
      os.environ['PYTHONHASHSEED'] = f'{seed}'
      np.random.seed(seed)
      random.seed(seed)
      tf.random.set_seed(seed)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-07-26
        • 1970-01-01
        • 1970-01-01
        • 2021-06-16
        • 1970-01-01
        相关资源
        最近更新 更多