当前位置: 首页 > 文档资料 > Edward 中文文档 >

ed.MAP

优质
小牛编辑
135浏览
2023-12-01

ed.MAP

Class MAP

Inherits From: VariationalInference

Aliases:

  • Class ed.MAP
  • Class ed.inferences.MAP

Defined in edward/inferences/map.py.

Maximum a posteriori.

This class implements gradient-based optimization to solve the optimization problem,

$(\min_{z} - p(z \mid x).)$

This is equivalent to using a PointMass variational distribution and minimizing the unnormalized objective,

$(- \mathbb{E}_{q(z; \lambda)} [ \log p(x, z) ].)$

Notes

This class is currently restricted to optimization over differentiable latent variables. For example, it does not solve discrete optimization.

This class also minimizes the loss with respect to any model parameters $(p(z \mid x; \theta))$.

In conditional inference, we infer $(z)$ in $(p(z, \beta \mid x))$ while fixing inference over $(\beta)$ using another distribution $(q(\beta))$. MAP optimizes $(\mathbb{E}_{q(\beta)} [ \log p(x, z, \beta) ])$, leveraging a single Monte Carlo sample, $(\log p(x, z, \beta^*))$, where $(\beta^* \sim q(\beta))$. This is a lower bound to the marginal density $(\log p(x, z))$, and it is exact if $(q(\beta) = p(\beta \mid x))$ (up to stochasticity).

Examples

Most explicitly, MAP is specified via a dictionary:

qpi = PointMass(params=ed.to_simplex(tf.Variable(tf.zeros(K-1))))
qmu = PointMass(params=tf.Variable(tf.zeros(K*D)))
qsigma = PointMass(params=tf.nn.softplus(tf.Variable(tf.zeros(K*D))))
ed.MAP({pi: qpi, mu: qmu, sigma: qsigma}, data)

We also automate the specification of PointMass distributions, so one can pass in a list of latent variables instead:

ed.MAP([beta], data)
ed.MAP([pi, mu, sigma], data)

Note that for MAP to optimize over latent variables with constrained continuous support, the point mass must be constrained to have the same support while its free parameters are unconstrained; see, e.g., qsigma above. This is different than performing MAP on the unconstrained space: in general, the MAP of the transform is not the transform of the MAP.

The objective function also adds to itself a summation over all tensors in the REGULARIZATION_LOSSES collection.

Methods

init

__init__(
    latent_vars=None,
    data=None
)

Create an inference algorithm.

Args:

  • latent_vars: list of RandomVariable or dict of RandomVariable to RandomVariable. Collection of random variables to perform inference on. If list, each random variable will be implictly optimized using a PointMass random variable that is defined internally with constrained support, has unconstrained free parameters, and is initialized using standard normal draws. If dictionary, each value in the dictionary must be a PointMass random variable with the same support as the key.

build_loss_and_gradients

build_loss_and_gradients(var_list)

Build loss function. Its automatic differentiation is the gradient of

$(- \log p(x,z).)$

finalize

finalize()

Function to call after convergence.

initialize

initialize(
    optimizer=None,
    var_list=None,
    use_prettytensor=False,
    global_step=None,
    *args,
    **kwargs
)

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

Args:

  • optimizer: str or tf.train.Optimizer. A TensorFlow optimizer, to use for optimizing the variational objective. Alternatively, one can pass in the name of a TensorFlow optimizer, and default parameters for the optimizer will be used.
  • var_list: list of tf.Variable. List of TensorFlow variables to optimize over. Default is all trainable variables that latent_vars and data depend on, excluding those that are only used in conditionals in data.
  • use_prettytensor: bool. True if aim to use PrettyTensor optimizer (when using PrettyTensor) or False if aim to use TensorFlow optimizer. Defaults to TensorFlow.
  • global_step: tf.Variable. A TensorFlow variable to hold the global step.

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.

  1. Initialize algorithm via initialize.
  2. (Optional) Build a TensorFlow summary writer for TensorBoard.
  3. (Optional) Initialize TensorFlow variables.
  4. (Optional) Start queue runners.
  5. Run update for self.n_iter iterations.
  6. While running, print_progress.
  7. Finalize algorithm via finalize.
  8. (Optional) Stop queue runners.

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

Args:

  • variables: list. 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.
  • use_coordinator: bool. 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 initialize.

update

update(feed_dict=None)

Run one iteration of optimization.

Args:

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

Returns:

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