tfp.experimental.vi.build_asvi_surrogate_posterior_stateless
Stay organized with collections
Save and categorize content based on your preferences.
Builds a structured surrogate posterior inspired by conjugate updating.
tfp.experimental.vi.build_asvi_surrogate_posterior_stateless(
prior,
mean_field=False,
initial_prior_weight=0.5,
prior_substitution_rules=tfp.experimental.vi.ASVI_DEFAULT_PRIOR_SUBSTITUTION_RULES
,
surrogate_rules=tfp.experimental.vi.ASVI_DEFAULT_SURROGATE_RULES
,
name=None
)
ASVI, or Automatic Structured Variational Inference, was proposed by
Ambrogioni et al. (2020) [1] as a method of automatically constructing a
surrogate posterior with the same structure as the prior. It does this by
reparameterizing the variational family of the surrogate posterior by
structuring each parameter according to the equation
prior_weight * prior_parameter + (1 - prior_weight) * mean_field_parameter
In this equation, prior_parameter
is a vector of prior parameters and
mean_field_parameter
is a vector of trainable parameters with the same
domain as prior_parameter
. prior_weight
is a vector of learnable
parameters where 0. <= prior_weight <= 1.
. When prior_weight =
0
, the surrogate posterior will be a mean-field surrogate, and when
prior_weight = 1.
, the surrogate posterior will be the prior. This convex
combination equation, inspired by conjugacy in exponential families, thus
allows the surrogate posterior to balance between the structure of the prior
and the structure of a mean-field approximation.
Args |
prior
|
tfd.JointDistribution instance of the prior.
|
mean_field
|
Optional Python boolean. If True , creates a degenerate
surrogate distribution in which all variables are independent,
ignoring the prior dependence structure. Default value: False .
|
initial_prior_weight
|
Optional float value (either static or tensor value)
on the interval [0, 1]. A larger value creates an initial surrogate
distribution with more dependence on the prior structure. Default value:
0.5 .
|
prior_substitution_rules
|
Iterable of substitution rules applied to the
prior before constructing a surrogate. Each rule is a (condition,
substitution_fn) tuple; these are checked in order and all applicable
substitutions are made. The condition may be either a class or a
callable returning a boolean (for example, tfd.Normal or, equivalently,
lambda dist: isinstance(dist, tfd.Normal) ). The substitution_fn should
have signature new_dist = substitution_fn(dist) .
|
surrogate_rules
|
Iterable of special-purpose rules to create surrogates
for specific distribution types. Each rule is a (condition,
surrogate_fn) tuple; these are checked in order and the first applicable
surrogate_fn is used. The condition may be either a class or a
callable returning a boolean (for example, tfd.Normal or, equivalently,
lambda dist: isinstance(dist, tfd.Normal) ). The surrogate_fn should
have signature init_fn, apply_fn = surrogate_fn(dist,
build_nested_surrogate_fn, sample_shape=None) .
|
name
|
Optional string. Default value: build_asvi_surrogate_posterior .
|
Returns |
init_fn
|
Python callable with signature initial_parameters = init_fn(seed) .
|
apply_fn
|
Python callable with signature instance = apply_fn(*parameters) .
|
Examples
Consider a Brownian motion model expressed as a JointDistribution:
prior_loc = 0.
innovation_noise = .1
def model_fn():
new = yield tfd.Normal(loc=prior_loc, scale=innovation_noise)
for i in range(4):
new = yield tfd.Normal(loc=new, scale=innovation_noise)
prior = tfd.JointDistributionCoroutineAutoBatched(model_fn)
Let's use variational inference to approximate the posterior. We'll build a
surrogate posterior distribution by feeding in the prior distribution.
surrogate_posterior =
tfp.experimental.vi.build_asvi_surrogate_posterior(prior)
This creates a trainable joint distribution, defined by variables in
surrogate_posterior.trainable_variables
. We use fit_surrogate_posterior
to fit this distribution by minimizing a divergence to the true posterior.
losses = tfp.vi.fit_surrogate_posterior(
target_log_prob_fn,
surrogate_posterior=surrogate_posterior,
num_steps=100,
optimizer=tf.optimizers.Adam(0.1),
sample_size=10)
# After optimization, samples from the surrogate will approximate
# samples from the true posterior.
samples = surrogate_posterior.sample(100)
posterior_mean = [tf.reduce_mean(x) for x in samples]
posterior_std = [tf.math.reduce_std(x) for x in samples]
References
[1]: Luca Ambrogioni, Kate Lin, Emily Fertig, Sharad Vikram, Max Hinne,
Dave Moore, Marcel van Gerven. Automatic structured variational
inference. arXiv preprint arXiv:2002.00643, 2020
https://arxiv.org/abs/2002.00643
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2023-11-21 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2023-11-21 UTC."],[],[],null,["# tfp.experimental.vi.build_asvi_surrogate_posterior_stateless\n\n\u003cbr /\u003e\n\n|--------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/probability/blob/v0.23.0/tensorflow_probability/python/experimental/vi/automatic_structured_vi.py#L407-L527) |\n\nBuilds a structured surrogate posterior inspired by conjugate updating. \n\n tfp.experimental.vi.build_asvi_surrogate_posterior_stateless(\n prior,\n mean_field=False,\n initial_prior_weight=0.5,\n prior_substitution_rules=../../../tfp/experimental/vi#ASVI_DEFAULT_PRIOR_SUBSTITUTION_RULES,\n surrogate_rules=../../../tfp/experimental/vi#ASVI_DEFAULT_SURROGATE_RULES,\n name=None\n )\n\nASVI, or Automatic Structured Variational Inference, was proposed by\nAmbrogioni et al. (2020) \\[1\\] as a method of automatically constructing a\nsurrogate posterior with the same structure as the prior. It does this by\nreparameterizing the variational family of the surrogate posterior by\nstructuring each parameter according to the equation \n\n prior_weight * prior_parameter + (1 - prior_weight) * mean_field_parameter\n\nIn this equation, `prior_parameter` is a vector of prior parameters and\n`mean_field_parameter` is a vector of trainable parameters with the same\ndomain as `prior_parameter`. `prior_weight` is a vector of learnable\nparameters where `0. \u003c= prior_weight \u003c= 1.`. When `prior_weight =\n0`, the surrogate posterior will be a mean-field surrogate, and when\n`prior_weight = 1.`, the surrogate posterior will be the prior. This convex\ncombination equation, inspired by conjugacy in exponential families, thus\nallows the surrogate posterior to balance between the structure of the prior\nand the structure of a mean-field approximation.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|----------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `prior` | tfd.JointDistribution instance of the prior. |\n| `mean_field` | Optional Python boolean. If `True`, creates a degenerate surrogate distribution in which all variables are independent, ignoring the prior dependence structure. Default value: `False`. |\n| `initial_prior_weight` | Optional float value (either static or tensor value) on the interval \\[0, 1\\]. A larger value creates an initial surrogate distribution with more dependence on the prior structure. Default value: `0.5`. |\n| `prior_substitution_rules` | Iterable of substitution rules applied to the prior before constructing a surrogate. Each rule is a `(condition, substitution_fn)` tuple; these are checked in order and *all* applicable substitutions are made. The `condition` may be either a class or a callable returning a boolean (for example, `tfd.Normal` or, equivalently, `lambda dist: isinstance(dist, tfd.Normal)`). The `substitution_fn` should have signature `new_dist = substitution_fn(dist)`. |\n| `surrogate_rules` | Iterable of special-purpose rules to create surrogates for specific distribution types. Each rule is a `(condition, surrogate_fn)` tuple; these are checked in order and the first applicable `surrogate_fn` is used. The `condition` may be either a class or a callable returning a boolean (for example, `tfd.Normal` or, equivalently, `lambda dist: isinstance(dist, tfd.Normal)`). The `surrogate_fn` should have signature `init_fn, apply_fn = surrogate_fn(dist, build_nested_surrogate_fn, sample_shape=None)`. |\n| `name` | Optional string. Default value: `build_asvi_surrogate_posterior`. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|------------|----------------------------------------------------------------------|\n| `init_fn` | Python callable with signature `initial_parameters = init_fn(seed)`. |\n| `apply_fn` | Python callable with signature `instance = apply_fn(*parameters)`. |\n\n\u003cbr /\u003e\n\n### Examples\n\nConsider a Brownian motion model expressed as a JointDistribution: \n\n prior_loc = 0.\n innovation_noise = .1\n\n def model_fn():\n new = yield tfd.Normal(loc=prior_loc, scale=innovation_noise)\n for i in range(4):\n new = yield tfd.Normal(loc=new, scale=innovation_noise)\n\n prior = tfd.JointDistributionCoroutineAutoBatched(model_fn)\n\nLet's use variational inference to approximate the posterior. We'll build a\nsurrogate posterior distribution by feeding in the prior distribution. \n\n surrogate_posterior =\n tfp.experimental.vi.build_asvi_surrogate_posterior(prior)\n\nThis creates a trainable joint distribution, defined by variables in\n`surrogate_posterior.trainable_variables`. We use `fit_surrogate_posterior`\nto fit this distribution by minimizing a divergence to the true posterior. \n\n losses = tfp.vi.fit_surrogate_posterior(\n target_log_prob_fn,\n surrogate_posterior=surrogate_posterior,\n num_steps=100,\n optimizer=tf.optimizers.Adam(0.1),\n sample_size=10)\n\n # After optimization, samples from the surrogate will approximate\n # samples from the true posterior.\n samples = surrogate_posterior.sample(100)\n posterior_mean = [tf.reduce_mean(x) for x in samples]\n posterior_std = [tf.math.reduce_std(x) for x in samples]\n\n#### References\n\n\\[1\\]: Luca Ambrogioni, Kate Lin, Emily Fertig, Sharad Vikram, Max Hinne,\nDave Moore, Marcel van Gerven. Automatic structured variational\ninference. *arXiv preprint arXiv:2002.00643* , 2020\n\u003chttps://arxiv.org/abs/2002.00643\u003e"]]