阅读(11.3k) 书签 (0)

TensorFlow Monte Carlo期望值计算

2018-08-30 15:57 更新

tf.contrib.bayesflow.monte_carlo.expectation

expectation( 
    f , 
    p , 
    z = none, 
    n = none, 
    seed = none, 
    name = 'expectation' 
)

定义在tensorflow/contrib/bayesflow/python/ops/monte_carlo_impl.py.

参见指南:贝叶斯蒙特卡罗(contrib)>行动

蒙特卡罗估计的期望:E_p[f(Z)] 与样本具有均值.

该操作返回:

n ^ { - 1 } sum_ {i= 1 } ^ n f( z_i ),where z_i 〜 p
\approx E_p[f(Z)]

ARGS:

  • f:从 p 到张量的可调用映射样本.
  • p:tf.contrib.distributions.Distribution.
  • z:p 样品的张量,由 p.sample 为一些 n 生成.
  • n:整数张量.如果未提供z生成的样本数.
  • seed:Python整数来寻找随机数程序.
  • name:为该操作提供一个名字.

返回:

与一个 Tensor 具有相同 dtype 的 p,举例:

N_samples =  10000
distribute = tf.contrib.distributions
dist =distributions.Uniform([ 0.0,0.0 ] ,[1.0,2.0]) 
elementwise_mean =  lambda x :x
mean_sum =  lambda x : tf.reduce_sum(x ,1)
estimate_elementwise_mean_tf = monte_carlo.expectation( elementwise_mean , 
                                                       dist , 
                                                       n = N_samples ) 
estimate_mean_sum_tf = monte_carlo.expectation( mean_sum , 
                                               dist , 
                                               n = N_samples )
with tf.Session()as sess : 
  estimate_elementwise_mean , estimate_mean_sum =  ( 
     sess.run([estimate_elementwise_mean_tf ,estimate_mean_sum_tf]))
print estimate_elementwise_mean
 >>> np.array([0.50018013  1.00097895 ] ,dtype=np. float32 )
print estimate_mean_sum
>>>  1.49571