Tensorflow 概率采样耗时较长
Tensorflow Probability Sampling Take Long Time
我正在尝试使用 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
干杯!
我正在尝试使用 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
干杯!