【问题标题】:Selecting one normal from a tensor based on another random variable in TensorFlow Probability基于TensorFlow概率中的另一个随机变量从张量中选择一个法线
【发布时间】:2020-12-07 12:02:05
【问题描述】:

我正在尝试根据分类分布的输出从一系列正态分布中选择一个样本,但似乎无法找到完全正确的方法。使用类似的东西:

tfp.distributions.JointDistributionSequential([
        tfp.distributions.Categorical(probs=[0, 0, 1/2, 1/2]),
        lambda c: tfp.distributions.Normal([0, 1, -10, 30], 1)[..., c]
    ])

完全返回我想要的单个案例,但是如果我想要一次多个样本,这会中断(因为 c 变成了一个 numpy 数组而不是一个整数。这可能吗?如果可以,我应该怎么做?

(我也尝试使用 OneHotCategorical 和乘法,但根本不起作用!)

【问题讨论】:

    标签: python tensorflow data-science probability tensorflow-probability


    【解决方案1】:

    如果您不想像 Brian 建议的那样使用 MixtureSameFamily,您可以这样做:

    tfp.distributions.JointDistributionSequential([
            tfp.distributions.Categorical(probs=[0, 0, 1/2, 1/2]),
            lambda c: tfp.distributions.Normal(tf.gather([0., 1, -10, 30], c), 1)
        ])
    

    请注意,我需要将 . 添加到聚集中的 locs 以避免 dtype 错误。

    在这里,我们最终要做的是

    1. Categorical 中抽取n 样本
    2. 构造一批nNormals,其locs是通过将n次索引到locs的4-vector中得到的
    3. n-batch of Normals 中采样。

    之前的方法行不通,因为Distribution slicing 不支持这种"fancy indexing" 如果我们支持那就太酷了! TF 一般不支持,对于reasons

    【讨论】:

    • 谢谢,这正是我最终的目标,因为我也想知道选择了哪个分类,我无法用@Brian-patton 解决的(也很有帮助的)答案。
    【解决方案2】:

    还有另一个distribution

    tfd.MixtureSameFamily(
      mixture_distribution=tfd.Categorical(probs=[0, 0, .5, .5]),
      components_distribution=tfd.Normal([0, 1, -10, 30], 1))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多