32
32
33
33
34
34
class Inference (object ):
35
- R"""
36
- Base class for Variational Inference
35
+ R"""**Base class for Variational Inference**
37
36
38
37
Communicates Operator, Approximation and Test Function to build Objective Function
39
38
@@ -87,8 +86,7 @@ def run_profiling(self, n=1000, score=None, **kwargs):
87
86
88
87
def fit (self , n = 10000 , score = None , callbacks = None , progressbar = True ,
89
88
** kwargs ):
90
- """
91
- Performs Operator Variational Inference
89
+ """Perform Operator Variational Inference
92
90
93
91
Parameters
94
92
----------
@@ -100,8 +98,29 @@ def fit(self, n=10000, score=None, callbacks=None, progressbar=True,
100
98
calls provided functions after each iteration step
101
99
progressbar : bool
102
100
whether to show progressbar or not
103
- kwargs : kwargs
104
- additional kwargs for :func:`ObjectiveFunction.step_function`
101
+
102
+ Other Parameters
103
+ ----------------
104
+ obj_n_mc : `int`
105
+ Number of monte carlo samples used for approximation of objective gradients
106
+ tf_n_mc : `int`
107
+ Number of monte carlo samples used for approximation of test function gradients
108
+ obj_optimizer : function (grads, params) -> updates
109
+ Optimizer that is used for objective params
110
+ test_optimizer : function (grads, params) -> updates
111
+ Optimizer that is used for test function params
112
+ more_obj_params : `list`
113
+ Add custom params for objective optimizer
114
+ more_tf_params : `list`
115
+ Add custom params for test function optimizer
116
+ more_updates : `dict`
117
+ Add custom updates to resulting updates
118
+ total_grad_norm_constraint : `float`
119
+ Bounds gradient norm, prevents exploding gradient problem
120
+ fn_kwargs : `dict`
121
+ Add kwargs to theano.function (e.g. `{'profile': True}`)
122
+ more_replacements : `dict`
123
+ Apply custom replacements before calculating gradients
105
124
106
125
Returns
107
126
-------
@@ -198,7 +217,8 @@ def _infmean(input_array):
198
217
score = True )
199
218
200
219
def refine (self , n , progressbar = True ):
201
- """Refine the solution using the last compiled step function"""
220
+ """Refine the solution using the last compiled step function
221
+ """
202
222
if self .state is None :
203
223
raise TypeError ('Need to call `.fit` first' )
204
224
i , step , callbacks , score = self .state
@@ -211,19 +231,22 @@ def refine(self, n, progressbar=True):
211
231
212
232
213
233
class KLqp (Inference ):
214
- """General approach to fit Approximations that define :math:`logq`
215
- by maximizing ELBO (Evidence Lower BOund).
234
+ """**Kullback Leibler Divergence Inference**
235
+
236
+ General approach to fit Approximations that define :math:`logq`
237
+ by maximizing ELBO (Evidence Lower Bound).
216
238
217
239
Parameters
218
240
----------
219
241
approx : :class:`Approximation`
242
+ Approximation to fit, it is required to have `logQ`
220
243
"""
221
244
def __init__ (self , approx ):
222
245
super (KLqp , self ).__init__ (KL , approx , None )
223
246
224
247
225
248
class ADVI (KLqp ):
226
- R"""Automatic Differentiation Variational Inference (ADVI)
249
+ R"""** Automatic Differentiation Variational Inference (ADVI)**
227
250
228
251
This class implements the meanfield ADVI, where the variational
229
252
posterior distribution is assumed to be spherical Gaussian without
@@ -341,7 +364,7 @@ class ADVI(KLqp):
341
364
Parameters
342
365
----------
343
366
local_rv : dict[var->tuple]
344
- mapping {model_variable -> local_variable (:math:`\mu`, :math:`\rho`) }
367
+ mapping {model_variable -> approx params }
345
368
Local Vars are used for Autoencoding Variational Bayes
346
369
See (AEVB; Kingma and Welling, 2014) for details
347
370
model : :class:`pymc3.Model`
@@ -371,12 +394,12 @@ def __init__(self, *args, **kwargs):
371
394
372
395
373
396
class FullRankADVI (KLqp ):
374
- R"""Full Rank Automatic Differentiation Variational Inference (ADVI)
397
+ R"""** Full Rank Automatic Differentiation Variational Inference (ADVI)**
375
398
376
399
Parameters
377
400
----------
378
401
local_rv : dict[var->tuple]
379
- mapping {model_variable -> local_variable (:math:`\mu`, :math:`\rho`) }
402
+ mapping {model_variable -> approx params }
380
403
Local Vars are used for Autoencoding Variational Bayes
381
404
See (AEVB; Kingma and Welling, 2014) for details
382
405
model : :class:`pymc3.Model`
@@ -406,7 +429,9 @@ def __init__(self, *args, **kwargs):
406
429
407
430
408
431
class ImplicitGradient (Inference ):
409
- """Implicit Gradient for Variational Inference
432
+ """**Implicit Gradient for Variational Inference**
433
+
434
+ **not suggested to use**
410
435
411
436
An approach to fit arbitrary approximation by computing kernel based gradient
412
437
By default RBF kernel is used for gradient estimation. Default estimator is
@@ -424,7 +449,7 @@ def __init__(self, approx, estimator=KSD, kernel=test_functions.rbf, **kwargs):
424
449
425
450
426
451
class SVGD (ImplicitGradient ):
427
- R"""Stein Variational Gradient Descent
452
+ R"""** Stein Variational Gradient Descent**
428
453
429
454
This inference is based on Kernelized Stein Discrepancy
430
455
it's main idea is to move initial noisy particles so that
@@ -433,9 +458,9 @@ class SVGD(ImplicitGradient):
433
458
Algorithm is outlined below
434
459
435
460
*Input:* A target distribution with density function :math:`p(x)`
436
- and a set of initial particles :math:`{x^0_i}^n_{i=1}`
461
+ and a set of initial particles :math:`\ {x^0_i\ }^n_{i=1}`
437
462
438
- *Output:* A set of particles :math:`{x_i }^n_{i=1}` that approximates the target distribution.
463
+ *Output:* A set of particles :math:`\{x^{*}_i\ }^n_{i=1}` that approximates the target distribution.
439
464
440
465
.. math::
441
466
@@ -490,7 +515,9 @@ def __init__(self, n_particles=100, jitter=1, model=None, start=None,
490
515
491
516
492
517
class ASVGD (ImplicitGradient ):
493
- R"""Amortized Stein Variational Gradient Descent
518
+ R"""**Amortized Stein Variational Gradient Descent**
519
+
520
+ **not suggested to use**
494
521
495
522
This inference is based on Kernelized Stein Discrepancy
496
523
it's main idea is to move initial noisy particles so that
@@ -585,7 +612,7 @@ def run_profiling(self, n=1000, score=None, obj_n_mc=500, **kwargs):
585
612
586
613
587
614
class NFVI (KLqp ):
588
- R"""Normalizing Flow based :class:`KLqp` inference
615
+ R"""** Normalizing Flow based :class:`KLqp` inference**
589
616
590
617
Normalizing flow is a series of invertible transformations on initial distribution.
591
618
@@ -648,15 +675,14 @@ def fit(n=10000, local_rv=None, method='advi', model=None,
648
675
n : `int`
649
676
number of iterations
650
677
local_rv : dict[var->tuple]
651
- mapping {model_variable -> local_variable (:math:`\mu`, :math:`\rho`) }
678
+ mapping {model_variable -> approx params }
652
679
Local Vars are used for Autoencoding Variational Bayes
653
680
See (AEVB; Kingma and Welling, 2014) for details
654
681
method : str or :class:`Inference`
655
682
string name is case insensitive in:
656
683
657
684
- 'advi' for ADVI
658
685
- 'fullrank_advi' for FullRankADVI
659
- - 'advi->fullrank_advi' for fitting ADVI first and then FullRankADVI
660
686
- 'svgd' for Stein Variational Gradient Descent
661
687
- 'asvgd' for Amortized Stein Variational Gradient Descent
662
688
- 'nfvi' for Normalizing Flow with default `scale-loc` flow
@@ -671,8 +697,35 @@ def fit(n=10000, local_rv=None, method='advi', model=None,
671
697
additional kwargs passed to :class:`Inference`
672
698
start : `Point`
673
699
starting point for inference
674
- kwargs : kwargs
675
- additional kwargs for :func:`Inference.fit`
700
+
701
+ Other Parameters
702
+ ----------------
703
+ score : bool
704
+ evaluate loss on each iteration or not
705
+ callbacks : list[function : (Approximation, losses, i) -> None]
706
+ calls provided functions after each iteration step
707
+ progressbar : bool
708
+ whether to show progressbar or not
709
+ obj_n_mc : `int`
710
+ Number of monte carlo samples used for approximation of objective gradients
711
+ tf_n_mc : `int`
712
+ Number of monte carlo samples used for approximation of test function gradients
713
+ obj_optimizer : function (grads, params) -> updates
714
+ Optimizer that is used for objective params
715
+ test_optimizer : function (grads, params) -> updates
716
+ Optimizer that is used for test function params
717
+ more_obj_params : `list`
718
+ Add custom params for objective optimizer
719
+ more_tf_params : `list`
720
+ Add custom params for test function optimizer
721
+ more_updates : `dict`
722
+ Add custom updates to resulting updates
723
+ total_grad_norm_constraint : `float`
724
+ Bounds gradient norm, prevents exploding gradient problem
725
+ fn_kwargs : `dict`
726
+ Add kwargs to theano.function (e.g. `{'profile': True}`)
727
+ more_replacements : `dict`
728
+ Apply custom replacements before calculating gradients
676
729
677
730
Returns
678
731
-------
0 commit comments