问题描述
我正在尝试使用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)
'''
解决方法
TFP发行版支持一个称为“批量形状”的概念。在这里,通过给probs=phi
加上phi.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
干杯!