@@ -24,7 +24,14 @@ class EvidenceLowerBoundObjective(VariationalObjective):
24
24
calling :func:`elbo`::
25
25
26
26
# lower_bound is an EvidenceLowerBoundObjective instance
27
- lower_bound = zs.variational.elbo(log_joint, observed, latent)
27
+ lower_bound = zs.variational.elbo(
28
+ meta_bn, observed, variational=variational, axis=0)
29
+
30
+ Here ``meta_bn`` is a :class:`~zhusuan.framework.meta_bn.MetaBayesianNet`
31
+ instance representing the model to be inferred. ``variational`` is
32
+ a :class:`~zhusuan.framework.bn.BayesianNet` instance that defines the
33
+ variational family. ``axis`` is the index of the sample dimension used
34
+ to estimate the expectation when computing the objective.
28
35
29
36
Instances of :class:`EvidenceLowerBoundObjective` are Tensor-like. They
30
37
can be automatically or manually cast into Tensors when fed into Tensorflow
@@ -60,8 +67,7 @@ class EvidenceLowerBoundObjective(VariationalObjective):
60
67
61
68
# optimize the surrogate cost wrt. variational parameters
62
69
optimizer = tf.train.AdamOptimizer(learning_rate)
63
- infer_op = optimizer.minimize(cost,
64
- var_list=variational_parameters)
70
+ infer_op = optimizer.minimize(cost, var_list=variational_parameters)
65
71
with tf.Session() as sess:
66
72
for _ in range(n_iters):
67
73
_, lb = sess.run([infer_op, lower_bound], feed_dict=...)
@@ -81,11 +87,9 @@ class EvidenceLowerBoundObjective(VariationalObjective):
81
87
optimize the class instance::
82
88
83
89
# optimize wrt. model parameters
84
- learn_op = optimizer.minimize(-lower_bound,
85
- var_list=model_parameters)
90
+ learn_op = optimizer.minimize(-lower_bound, var_list=model_parameters)
86
91
# or
87
- # learn_op = optimizer.minimize(cost,
88
- # var_list=model_parameters)
92
+ # learn_op = optimizer.minimize(cost, var_list=model_parameters)
89
93
# both ways are correct
90
94
91
95
Or we can do inference and learning jointly by optimize over both
@@ -95,30 +99,34 @@ class EvidenceLowerBoundObjective(VariationalObjective):
95
99
infer_and_learn_op = optimizer.minimize(
96
100
cost, var_list=model_and_variational_parameters)
97
101
98
- :param log_joint: A function that accepts a dictionary argument of
102
+ :param meta_bn: A :class:`~zhusuan.framework.meta_bn.MetaBayesianNet`
103
+ instance or a log joint probability function.
104
+ For the latter, it must accepts a dictionary argument of
99
105
``(string, Tensor)`` pairs, which are mappings from all
100
- `StochasticTensor` names in the model to their observed values. The
106
+ node names in the model to their observed values. The
101
107
function should return a Tensor, representing the log joint likelihood
102
108
of the model.
103
109
:param observed: A dictionary of ``(string, Tensor)`` pairs. Mapping from
104
- names of observed `StochasticTensor` s to their values.
110
+ names of observed stochastic nodes to their values.
105
111
:param latent: A dictionary of ``(string, (Tensor, Tensor))`` pairs.
106
- Mapping from names of latent `StochasticTensor` s to their samples and
107
- log probabilities.
112
+ Mapping from names of latent stochastic nodes to their samples and
113
+ log probabilities. `latent` and `variational` are mutually exclusive.
108
114
:param axis: The sample dimension(s) to reduce when computing the
109
115
outer expectation in the objective. If ``None``, no dimension is
110
116
reduced.
117
+ :param variational: A :class:`~zhusuan.framework.bn.BayesianNet` instance
118
+ that defines the variational family.
119
+ `variational` and `latent` are mutually exclusive.
111
120
"""
112
121
113
122
def __init__ (self , meta_bn , observed , latent = None , axis = None ,
114
- variational = None , allow_default = False ):
123
+ variational = None ):
115
124
self ._axis = axis
116
125
super (EvidenceLowerBoundObjective , self ).__init__ (
117
126
meta_bn ,
118
127
observed ,
119
128
latent = latent ,
120
- variational = variational ,
121
- allow_default = allow_default )
129
+ variational = variational )
122
130
123
131
def _objective (self ):
124
132
lower_bound = self ._log_joint_term ()
@@ -223,27 +231,31 @@ def reinforce(self,
223
231
return cost
224
232
225
233
226
- def elbo (meta_bn , observed , latent = None , axis = None , variational = None ,
227
- allow_default = False ):
234
+ def elbo (meta_bn , observed , latent = None , axis = None , variational = None ):
228
235
"""
229
236
The evidence lower bound (ELBO) objective for variational inference. The
230
237
returned value is a :class:`EvidenceLowerBoundObjective` instance.
231
238
232
239
See :class:`EvidenceLowerBoundObjective` for examples of usage.
233
240
234
- :param log_joint: A function that accepts a dictionary argument of
241
+ :param meta_bn: A :class:`~zhusuan.framework.meta_bn.MetaBayesianNet`
242
+ instance or a log joint probability function.
243
+ For the latter, it must accepts a dictionary argument of
235
244
``(string, Tensor)`` pairs, which are mappings from all
236
- `StochasticTensor` names in the model to their observed values. The
245
+ node names in the model to their observed values. The
237
246
function should return a Tensor, representing the log joint likelihood
238
247
of the model.
239
248
:param observed: A dictionary of ``(string, Tensor)`` pairs. Mapping from
240
- names of observed `StochasticTensor` s to their values.
249
+ names of observed stochastic nodes to their values.
241
250
:param latent: A dictionary of ``(string, (Tensor, Tensor))`` pairs.
242
- Mapping from names of latent `StochasticTensor` s to their samples and
243
- log probabilities.
251
+ Mapping from names of latent stochastic nodes to their samples and
252
+ log probabilities. `latent` and `variational` are mutually exclusive.
244
253
:param axis: The sample dimension(s) to reduce when computing the
245
254
outer expectation in the objective. If ``None``, no dimension is
246
255
reduced.
256
+ :param variational: A :class:`~zhusuan.framework.bn.BayesianNet` instance
257
+ that defines the variational family.
258
+ `variational` and `latent` are mutually exclusive.
247
259
248
260
:return: An :class:`EvidenceLowerBoundObjective` instance.
249
261
"""
@@ -252,5 +264,4 @@ def elbo(meta_bn, observed, latent=None, axis=None, variational=None,
252
264
observed ,
253
265
latent = latent ,
254
266
axis = axis ,
255
- variational = variational ,
256
- allow_default = allow_default )
267
+ variational = variational )
0 commit comments