Tensorflow概率采样需要很长时间



我正在尝试使用tfp进行采样过程。从贝塔分布中提取样本,并将结果作为概率输入以从二元分布中提取采样。跑步花了很长时间。

我应该这样做吗?还是有一种最佳的方式?

''

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)

''

TFP分发支持一个我们称之为";批次形状";。在这里,通过将probs=phiphi.shape = [100000]结合使用,您有效地创建了一个";批次";10万Binomials。然后你要从中采样10万次,试图创建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

干杯!

最新更新