TensorFlow Monte Carlo期望值計(jì)算

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)>行動(dòng)

蒙特卡羅估計(jì)的期望: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 到張量的可調(diào)用映射樣本.
  • p:tf.contrib.distributions.Distribution.
  • z:p 樣品的張量,由 p.sample 為一些 n 生成.
  • n:整數(shù)張量.如果未提供z生成的樣本數(shù).
  • seed:Python整數(shù)來尋找隨機(jī)數(shù)程序.
  • name:為該操作提供一個(gè)名字.

返回:

與一個(gè) 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


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)