tfsnippet.bayes¶
-
class
tfsnippet.bayes.
BayesianNet
(observed=None)¶ Bases:
object
Bayesian networks.
BayesianNet
is a class which helps to construct Bayesian networks and to derive the variational lower-bounds. It is inspired byzhusuan.BayesianNet
.Due to the expressive limitations of TensorFlow, it is hard to build
BayesianNet
with the concept of random variables. Instead, we only collectStochasticTensor
objects, i.e., tensors sampled from the distributions of these random variables. ThusBayesianNet
is actually a collection of (multiple) ancestral samples from the random variables. Fortunately, we can approximate most interested statistics of the desired random variables with these samples, by using Monte Carlo methods. For example, obtaining the expectation of a random variable by averaging over multiple samples from it. TheStochasticTensor
objects are called stochastic nodes within the context ofBayesianNet
.To build a Bayesian network, first obtain a
BayesianNet
:net = tfsnippet.bayes.BayesianNet()
Then add stochastic nodes into the network:
A Bayesian Linear Regression example, as of
zhusuan.BayesianNet
:\[ \begin{align}\begin{aligned}w \sim N(0, \alpha^2 I)\\y \sim N(w^Tx, \beta^2)\end{aligned}\end{align} \]from tfsnippet.bayes import BayesianNet() from tfsnippet.distributions import Normal def bayesian_linear_regression(x, alpha, beta, observed=None): net = BayesianNet(observed) w = net.add('w', Normal(mean=0., logstd=tf.log(alpha))) y_mean = tf.reduce_sum(tf.expand_dims(w, 0) * x, 1) y = net.add('y', Normal(mean=y_mean, logstd=tf.log(beta))) return net
To observe any stochastic nodes in the network, pass a dictionary mapping of
(name, Tensor)
as observed when constructingBayesianNet
. For example:model = bayesian_linear_regression(..., observed={'w': w_obs})
After construction,
BayesianNet
supports queries on the network.# get samples of random variable y following generative process # in the network model.output('y') # because w is observed in this case, its observed value will be # returned model.output('w') # also multiple outputs can be fetched together model.outputs(['y', 'w']) # get local log probability values of w and y, which returns # log p(w) and log p(y|w, x) model.local_log_probs(['w', 'y']) # query many quantities at the same time model.query(['w', 'y'])
See also
zhusuan.BayesianNet
-
__contains__
(name)¶ Test whether or not a stochastic node with name exists.
-
__getitem__
(name)¶ Get
StochasticTensor
of a stochastic node.Parameters: name (str) – Name of the queried stochastic node. Returns: StochasticTensor
of the queried node.Return type: StochasticTensor Raises: KeyError
– If non-exist name is queried.
-
__init__
(observed=None)¶ Construct the
BayesianNet
.Parameters: observed – Dict of (str, tf.Tensor)
, the names of stochastic nodes and their observations.
-
__iter__
()¶ Get an iterator of the stochastic node names.
-
add
(name, distribution, n_samples=None, group_ndims=0, is_reparameterized=None, flow=None)¶ Add a stochastic node to the network.
A
StochasticTensor
will be created for this node. If name exists in observed dict, its value will be used as the observation of this node. Otherwise samples will be taken from distribution.Parameters: - name (str) – Name of the stochastic node.
- distribution (Distribution or zhusuan.distributions.Distribution) – Distribution where the samples should be taken from.
- n_samples (int or tf.Tensor) – Number of samples to take.
If specified, n_samples will be taken, with a dedicated
sampling dimension
[n_samples]
at the front. If not specified, just one sample will be taken, without the dedicated dimension. - group_ndims (int or tf.Tensor) – Number of dimensions at the end of
[n_samples] + batch_shape
to be considered as events group. (default 0) - is_reparameterized – Whether or not the re-parameterization trick
should be applied? (default
None
, following the setting of distribution) - flow (Flow) – If specified, transform distribution by the flow.
Returns: The sampled stochastic tensor.
Return type: Raises: TypeError
– If name is not a str, or distribution is aTransformedDistribution
.KeyError
– IfStochasticTensor
with name already exists.ValueError
– If transform cannot be applied.
-
chain
(model_builder, latent_names=None, latent_axis=None, observed=None, **kwargs)¶ Alias for
variational_chain()
.
-
get
(name)¶ Get
StochasticTensor
of a stochastic node.Parameters: name (str) – Name of the queried stochastic node. Returns: StochasticTensor
of the queried node,- or
None
if no node exists with name.
Return type: StochasticTensor
-
local_log_prob
(name)¶ Get the log-density of a stochastic node.
Parameters: name (str) – Name of the queried stochastic node. Returns: Log-density of the queried stochastic node. Return type: tf.Tensor Raises: KeyError
– If non-exist name is queried.
-
local_log_probs
(names)¶ Get the log-densities of stochastic nodes.
Parameters: names (Iterable[str]) – Names of the queried stochastic nodes. Returns: - Log-densities of the queried stochastic
- nodes.
Return type: list[tf.Tensor] Raises: KeyError
– If non-exist name is queried.
-
observed
¶ Get the read-only dict of observations.
Returns: - The read-only dict of
- observations.
Return type: collections.Mapping[str, tf.Tensor]
-
output
(name)¶ Get the output of a stochastic node. The output of a stochastic node is its
StochasticTensor.tensor
.Parameters: name (str) – Name of the queried stochastic node. Returns: Output of the queried stochastic node. Return type: tf.Tensor Raises: KeyError
– If non-exist name is queried.
-
outputs
(names)¶ Get the outputs of stochastic nodes. The output of a stochastic node is its
StochasticTensor.tensor
.Parameters: names (Iterable[str]) – Names of the queried stochastic nodes. Returns: Outputs of the queried stochastic nodes. Return type: list[tf.Tensor] Raises: KeyError
– If non-exist name is queried.
-
query
(names)¶ Get the outputs and log-densities of stochastic node(s).
Parameters: names (Iterable[str]) – Names of the queried stochastic nodes. Returns: - Tuples of (output, log-prob) of the
- queried stochastic nodes.
Return type: list[(tf.Tensor, tf.Tensor)] Raises: KeyError
– If non-exist name is queried.
-
variational_chain
(model_builder, latent_names=None, latent_axis=None, observed=None, **kwargs)¶ Treat this
BayesianNet
as variational, and build the model net chained after this variational net.Parameters: - model_builder – Function which receives the observed dict, and
produce the model
BayesianNet
or a tuple of the modelBayesianNet
and the log-joint of the model. - latent_names (Iterable[str]) – Names of the nodes to be considered
as latent variables in this
BayesianNet
. All these variables will be fed into model_builder as observed variables, overriding the observations in observed. (default all the variables in thisBayesianNet
) - latent_axis – The axis or axes to be considered as the sampling
dimensions of latent variables. The specified axes will be
summed up in the variational lower-bounds or training
objectives. (default
None
) - observed – Dict of
(name, observation)
fed into model_builder. (defaultNone
) - **kwargs – Additional named arguments passed to model_builder.
Returns: - The object that holds this
BayesianNet
as the variational net, the constructed model net, and theVariationalInference
object for obtaining the variational lower-bounds and training objectives.
Return type: - model_builder – Function which receives the observed dict, and
produce the model
-