【发布时间】: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