`KLpq`

Inherits From: `VariationalInference`

- Class
`ed.KLpq`

- Class
`ed.inferences.KLpq`

Defined in `edward/inferences/klpq.py`

.

Variational inference with the KL divergence

\(\text{KL}( p(z \mid x) \| q(z) ).\)

To perform the optimization, this class uses a technique from adaptive importance sampling (Cappe et al., 2008).

`KLpq`

also optimizes any model parameters \(p(z\mid x; \theta)\). It does this by variational EM, minimizing

\(\mathbb{E}_{p(z \mid x; \lambda)} [ \log p(x, z; \theta) ]\)

with respect to \(\theta\).

In conditional inference, we infer $z` in \(p(z, \beta \mid x)\) while fixing inference over \(\beta\) using another distribution \(q(\beta)\). During gradient calculation, instead of using the model's density

\(\log p(x, z^{(s)}), z^{(s)} \sim q(z; \lambda),\)

for each sample \(s=1,\ldots,S\), `KLpq`

uses

\(\log p(x, z^{(s)}, \beta^{(s)}),\)

where \(z^{(s)} \sim q(z; \lambda)\) and\(\beta^{(s)} \sim q(\beta)\).

**init**

```
__init__(
*args,
**kwargs
)
```

`build_loss_and_gradients`

`build_loss_and_gradients(var_list)`

Build loss function

\(\text{KL}( p(z \mid x) \| q(z) ) = \mathbb{E}_{p(z \mid x)} [ \log p(z \mid x) - \log q(z; \lambda) ]\)

and stochastic gradients based on importance sampling.

The loss function can be estimated as

\(\frac{1}{S} \sum_{s=1}^S [ w_{\text{norm}}(z^s; \lambda) (\log p(x, z^s) - \log q(z^s; \lambda) ],\)

where for \(z^s \sim q(z; \lambda)\),

\(w_{\text{norm}}(z^s; \lambda) = w(z^s; \lambda) / \sum_{s=1}^S w(z^s; \lambda)\)

normalizes the importance weights, \(w(z^s; \lambda) = p(x, z^s) / q(z^s; \lambda)\).

This provides a gradient,

\(- \frac{1}{S} \sum_{s=1}^S [ w_{\text{norm}}(z^s; \lambda) \nabla_{\lambda} \log q(z^s; \lambda) ].\)

`finalize`

`finalize()`

Function to call after convergence.

`initialize`

```
initialize(
n_samples=1,
*args,
**kwargs
)
```

Initialize inference algorithm. It initializes hyperparameters and builds ops for the algorithm's computation graph.

: int, optional. Number of samples from variational model for calculating stochastic gradients.`n_samples`

`print_progress`

`print_progress(info_dict)`

Print progress to output.

`run`

```
run(
variables=None,
use_coordinator=True,
*args,
**kwargs
)
```

A simple wrapper to run inference.

- Initialize algorithm via
`initialize`

. - (Optional) Build a TensorFlow summary writer for TensorBoard.
- (Optional) Initialize TensorFlow variables.
- (Optional) Start queue runners.
- Run
`update`

for`self.n_iter`

iterations. - While running,
`print_progress`

. - Finalize algorithm via
`finalize`

. - (Optional) Stop queue runners.

To customize the way inference is run, run these steps individually.

: list, optional. A list of TensorFlow variables to initialize during inference. Default is to initialize all variables (this includes reinitializing variables that were already initialized). To avoid initializing any variables, pass in an empty list.`variables`

: bool, optional. Whether to start and stop queue runners during inference using a TensorFlow coordinator. For example, queue runners are necessary for batch training with file readers. *args, **kwargs: Passed into`use_coordinator`

`initialize`

.

`update`

`update(feed_dict=None)`

Run one iteration of optimization.

: dict, optional. Feed dictionary for a TensorFlow session run. It is used to feed placeholders that are not fed during initialization.`feed_dict`

dict. Dictionary of algorithm-specific information. In this case, the loss function value after one iteration.