【问题标题】:Tensorflow Probability Sampling Take Long TimeTensorFlow 概率采样需要很长时间
【发布时间】:2020-12-31 15:05:07
【问题描述】:

我正在尝试使用 tfp 进行采样过程。从 beta 分布中抽取样本,并将结果作为概率输入从二项分布中抽取样本。它需要很长时间才能运行。

我应该以这种方式运行它还是有最佳方式?

'''

import tensorflow_probability as tfp
tfd = tfp.distributions

m = 100000 # sample size

### first sample from Beta distribution 
### and feed the result as probability to the binomial distribution sampling
s = tfd.Sample(
    tfd.Beta(2,2),
    sample_shape = m
)
phi = s.sample()

### Second sample from Binominal distribution 
### !!! it took forever to run...
s2 = tfd.Sample(
    tfd.Binomial(total_count=10, probs=phi),
    sample_shape = m
)

y = s2.sample() # not working well


### scipy code which works well:
from scipy import stats
m = 100000 # sample size
phi = stats.beta.rvs(2, 2, size = m)
y = stats.binom.rvs(10, phi, size = m)

'''

【问题讨论】:

    标签: tensorflow sampling montecarlo tensorflow-probability


    【解决方案1】:

    TFP 分布支持我们称为“批量形状”的概念。在这里,通过将probs=phiphi.shape = [100000] 一起提供,您实际上是在创建一个包含 100k 二项式的“批次”。然后你从那些样本中采样 100k 次,它试图创建 1e10 个样本,这需要一段时间!相反,试试这个:

    m = 100000
    s = tfd.Sample(
        tfd.Beta(2,2),
        sample_shape = m
    )
    phi = s.sample()
    
    ### Second sample from Binominal distribution 
    s2 = tfd.Binomial(total_count=10, probs=phi)
    
    y = s2.sample()
    

    或者,使用tfd.BetaBinomial

    bb = tfd.BetaBinomial(total_count=10, concentration1=2, concentration0=2)
    bb.sample(100000)
    

    但最重要的是,看一下通过 TFP 的形状语义讨论的示例笔记本:https://www.tensorflow.org/probability/examples/Understanding_TensorFlow_Distributions_Shapes

    干杯!

    【讨论】:

    • 哈哈,这很有帮助。非常感谢!
    猜你喜欢
    • 2021-02-28
    • 2013-09-07
    • 2020-08-26
    • 2014-10-09
    • 2012-11-26
    • 2019-12-27
    • 2017-10-22
    • 2020-11-15
    • 2016-05-19
    相关资源
    最近更新 更多