Base Classes for MCMC Methods

This module provides abstract base classes for our SGMCMC sampling methods. All subclasses inheriting from any of these base classes automatically conform to the iterator protocol.

This means that extracting the next sample with corresponding costs from any of our samplers is as simple as:

sample, cost = next(sampler)
class mcmc_base_classes.MCMCSampler(params, seed=None, batch_generator=None, dtype=<Mock name='tensorflow.float64' id='139670266623648'>, session=<Mock name='tensorflow.get_default_session()' id='139670266625552'>)[source]

Generic base class for all MCMC samplers.

__init__(params, seed=None, batch_generator=None, dtype=<Mock name='tensorflow.float64' id='139670266623648'>, session=<Mock name='tensorflow.get_default_session()' id='139670266625552'>)[source]

Initialize the sampler base class. Sets up member variables and initializes uninitialized target parameters in the current tensorflow.Graph.

Parameters:

params : list of tensorflow.Variable objects

Target parameters for which we want to sample new values.

seed : int, optional

Random seed to use. Defaults to None.

batch_generator : BatchGenerator, optional

Iterable which returns dictionaries to feed into tensorflow.Session.run() calls to evaluate the cost function. Defaults to None which indicates that no batches shall be fed.

dtype : tensorflow.DType, optional

Type of elements of tensorflow.Tensor objects used in this sampler. Defaults to tensorflow.float64.

session : tensorflow.Session, optional

Session object which knows about the external part of the graph (which defines Cost, and possibly batches). Used internally to evaluate (burn-in/sample) the sampler.

See also

tensorflow_mcmc.sampling.BurnInMCMCSampler
Abstract base class for samplers that perform a burn-in phase to tune their own hyperparameters. Inherits from sampling.MCMCSampler.
__iter__()[source]

Allows using samplers as iterators.

Examples

Extract the first three thousand samples (with costs) from a sampler:

>>> import tensorflow as tf
>>> import numpy as np
>>> from itertools import islice
>>> from tensorflow_mcmc.sampling.sghmc import SGHMCSampler
>>> session = tf.Session()
>>> x = tf.Variable(1.0)
>>> dist = tf.contrib.distributions.Normal(loc=0., scale=1.)
>>> n_burn_in, n_samples = 1000, 2000
>>> sampler = SGHMCSampler(params=[x], burn_in_steps=n_burn_in, cost_fun=lambda x: -dist.log_prob(x), session=session, dtype=tf.float32)
>>> session.run(tf.global_variables_initializer())
>>> burn_in_samples = list(islice(sampler, n_burn_in))  # perform all burn_in steps
>>> samples = list(islice(sampler, n_samples))
>>> len(burn_in_samples), len(samples)
(1000, 2000)
>>> session.close()
>>> tf.reset_default_graph()  # to avoid polluting test environment
__metaclass__

alias of ABCMeta

__next__(feed_vals={})[source]
Compute and return the next sample and
next cost values for this sampler.
Returns:

sample: list of numpy.ndarray objects

Sampled values are a numpy.ndarray for each target parameter.

cost: numpy.ndarray (1,)

Current cost value of the last evaluated target parameter values.

Examples

Extract the next sample (with costs) from a sampler:

>>> import tensorflow as tf
>>> import numpy as np
>>> from itertools import islice
>>> from tensorflow_mcmc.sampling.sghmc import SGHMCSampler
>>> session = tf.Session()
>>> x = tf.Variable(1.0)
>>> dist = tf.contrib.distributions.Normal(loc=0., scale=1.)
>>> n_burn_in = 1000
>>> sampler = SGHMCSampler(params=[x], burn_in_steps=n_burn_in, cost_fun=lambda x:-dist.log_prob(x), session=session, dtype=tf.float32)
>>> session.run(tf.global_variables_initializer())
>>> sample, cost = next(sampler)
>>> session.close()
>>> tf.reset_default_graph()  # to avoid polluting test environment
__weakref__

list of weak references to the object (if defined)

_draw_noise_sample(Sigma, Shape)[source]
Generate a single random normal sample with shape Shape and
standard deviation Sigma.
Parameters:

Sigma : tensorflow.Tensor

Standard deviation of the noise.

Shape : tensorflow.Tensor

Shape that the noise sample should have.

Returns:

noise_sample: tensorflow.Tensor

Random normal sample with shape Shape and standard deviation Sigma.

_next_batch()[source]
Get a dictionary mapping tensorflow.Placeholder onto

their corresponding feedable minibatch data. Each dictionary can directly be fed into tensorflow.Session.

Returns an empty dictionary if self.batch_generator is None, i.e. if no batches are needed to compute the cost function. (e.g. the cost function depends only on the target parameters).

Returns:

batch:

Dictionary that maps tensorflow.Placeholder objects onto ndarray objects that can be fed for them. Returns an empty dict if self.batch_generator is None, i.e. if no batches are needed to compute the cost function (e.g. the cost function depends only on the target parameters).

Examples

Extracting batches without any batch_generator function simply returns an empty dict: >>> import tensorflow as tf >>> import numpy as np >>> from itertools import islice >>> from tensorflow_mcmc.sampling.sghmc import SGHMCSampler >>> session = tf.Session() >>> x = tf.Variable(1.0) >>> dist = tf.contrib.distributions.Normal(loc=0., scale=1.) >>> sampler = SGHMCSampler(params=[x], cost_fun=lambda x: -dist.log_prob(x), session=session, dtype=tf.float32) >>> session.close() >>> sampler._next_batch() {}

A simple case with batches would look like this: >>> import tensorflow as tf >>> from tensorflow_mcmc.bayesian_neural_network import generate_batches >>> from tensorflow_mcmc.sampling.sghmc import SGHMCSampler >>> session = tf.Session() >>> N, D = 100, 3 # 100 datapoints with 3 features each >>> X = np.asarray([np.random.uniform(-10, 10, D) for _ in range(N)]) >>> y = np.asarray([np.random.choice([0., 1.]) for _ in range(N)]) >>> x_placeholder, y_placeholder = tf.placeholder(dtype=tf.float64), tf.placeholder(dtype=tf.float64) >>> batch_size = 10 >>> batch_generator = generate_batches(X=X, y=y, x_placeholder=x_placeholder, y_placeholder=y_placeholder, batch_size=batch_size) >>> sampler = SGHMCSampler(params=[x], cost_fun=lambda x: x, session=session, dtype=tf.float32, batch_generator=batch_generator) # cost function is just a dummy >>> batch_dict = sampler._next_batch() >>> session.close() >>> set(batch_dict.keys()) == set((x_placeholder, y_placeholder)) True >>> batch_dict[x_placeholder].shape, batch_dict[y_placeholder].shape ((10, 3), (10, 1))

_uninitialized_params(params)[source]
Determine a list of tensorflow.Variable objects in iterable params
that are not yet initialized.
Parameters:

params : list of tensorflow.Variable objects

List of target parameters that we want to sample values for.

Returns:

params_uninitialized: list of tensorflow.Variable objects

All target parameters in params that were not initialized yet in the current graph.

For some applications (e.g. Bayesian Optimization), it is important that samplers come with as few design choices as possible. To reduce the number of such design choices, a recent contribution in the literature proposes an on-line burn-in procedure.

class mcmc_base_classes.BurnInMCMCSampler(params, burn_in_steps, seed=None, batch_generator=None, dtype=<Mock name='tensorflow.float64' id='139670266623648'>, session=<Mock name='tensorflow.get_default_session()' id='139670266625552'>)[source]

Bases: mcmc_base_classes.MCMCSampler

Base class for MCMC samplers that use a burn-in procedure to estimate their mass matrix. Details of how this burn-in is performed are left to be specified in the individual samplers that inherit from this class.

__init__(params, burn_in_steps, seed=None, batch_generator=None, dtype=<Mock name='tensorflow.float64' id='139670266623648'>, session=<Mock name='tensorflow.get_default_session()' id='139670266625552'>)[source]

Initializes the corresponding MCMCSampler super object and sets member variables.

Parameters:

params : list of tensorflow.Variable objects

Target parameters for which we want to sample new values.

burn_in_steps: int

Number of burn-in steps to perform. In each burn-in step, this sampler will adapt its own internal parameters to decrease its error. For reference see: TODO ADD PAPER REFERENCE HERE

seed : int, optional

Random seed to use. Defaults to None.

batch_generator : BatchGenerator, optional

Iterable which returns dictionaries to feed into tensorflow.Session.run() calls to evaluate the cost function. Defaults to None which indicates that no batches shall be fed.

dtype : tensorflow.DType, optional

Type of elements of tensorflow.Tensor objects used in this sampler. Defaults to tensorflow.float64.

session : tensorflow.Session, optional

Session object which knows about the external part of the graph (which defines Cost, and possibly batches). Used internally to evaluate (burn-in/sample) the sampler.

See also

tensorflow_mcmc.sampling.mcmc_base_classes.MCMCSampler
Super class of this class. Has generic methods shared by all MCMC samplers implemented as part of this framework.
tensorflow_mcmc.sampling.sghmc.SGHMCSampler
Instantiation of this class. Uses SGHMC to sample from the target distribution after burn-in.
tensorflow_mcmc.sampling.sgld.SGLDSampler
Instantiation of this class. Uses SGLD to sample from the target distribution after burn-in.
__metaclass__

alias of ABCMeta

__next__()[source]
Perform a sampler step:

Compute and return the next sample and next cost values for this sampler.

While self.is_burning_in returns True (while the sampler has not yet performed self.burn_in_steps steps) this will also adapt the samplers mass matrix in a sampler-specific way to improve performance.

Returns:

sample: list of numpy.ndarray objects

Sampled values are a numpy.ndarray for each target parameter.

cost: numpy.ndarray (1,)

Current cost value of the last evaluated target parameter values.

is_burning_in
Check if this sampler is still in burn-in phase.
Used during graph construction to insert conditionals into the graph that will make the sampler skip all burn-in operations after the burn-in phase is over.
Returns:

is_burning_in: boolean

True if self.n_iterations <= self.burn_in_steps, otherwise False.