# Samplers¶

class pysgmcmc.samplers.SGHMCSampler(params, cost_fun, batch_generator=None, stepsize_schedule=<pysgmcmc.stepsize_schedules.ConstantStepsizeSchedule object>, burn_in_steps=3000, mdecay=0.05, scale_grad=1.0, session=None, dtype=tf.float64, seed=None)[source]

Stochastic Gradient Hamiltonian Monte-Carlo Sampler that uses a burn-in procedure to adapt its own hyperparameters during the initial stages of sampling.

See [1] for more details on this burn-in procedure.

See [2] for more details on Stochastic Gradient Hamiltonian Monte-Carlo.

[1] J. T. Springenberg, A. Klein, S. Falkner, F. Hutter

In Advances in Neural Information Processing Systems 29 (2016).

Bayesian Optimization with Robust Bayesian Neural Networks.

[2] T. Chen, E. B. Fox, C. Guestrin

In Proceedings of Machine Learning Research 32 (2014).

__init__(params, cost_fun, batch_generator=None, stepsize_schedule=<pysgmcmc.stepsize_schedules.ConstantStepsizeSchedule object>, burn_in_steps=3000, mdecay=0.05, scale_grad=1.0, session=None, dtype=tf.float64, seed=None)[source]
Initialize the sampler parameters and set up a tensorflow.Graph
for later queries.
Parameters: params (list of tensorflow.Variable objects) – Target parameters for which we want to sample new values. cost_fun (callable) – Function that takes params as input and returns a 1-d tensorflow.Tensor that contains the cost-value. Frequently denoted with U in literature. batch_generator (iterable, 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. stepsize_schedule (pysgmcmc.stepsize_schedules.StepsizeSchedule) – Iterator class that produces a stream of stepsize values that we can use in our samplers. See also: pysgmcmc.stepsize_schedules burn_in_steps (int, optional) – Number of burn-in steps to perform. In each burn-in step, this sampler will adapt its own internal parameters to decrease its error. Defaults to 3000. For reference see: Bayesian Optimization with Robust Bayesian Neural Networks. mdecay (float, optional) – (Constant) momentum decay per time-step. Defaults to 0.05. For reference see: Bayesian Optimization with Robust Bayesian Neural Networks. scale_grad (float, optional) – Value that is used to scale the magnitude of the noise used during sampling. In a typical batches-of-data setting this usually corresponds to the number of examples in the entire dataset. Defaults to 1.0 which corresponds to no scaling. 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. dtype (tensorflow.DType, optional) – Type of elements of tensorflow.Tensor objects used in this sampler. Defaults to tensorflow.float64. seed (int, optional) – Random seed to use. Defaults to None.

pysgmcmc.sampling.BurnInMCMCSampler()
Base class for SGHMCSampler that specifies how actual sampling is performed (using iterator protocol, e.g. next(sampler)).
class pysgmcmc.samplers.RelativisticSGHMCSampler(params, cost_fun, batch_generator=None, stepsize_schedule=<pysgmcmc.stepsize_schedules.ConstantStepsizeSchedule object>, mass=1.0, speed_of_light=1.0, D=1.0, Bhat=0.0, session=None, dtype=tf.float64, seed=None)[source]

Relativistic Stochastic Gradient Hamiltonian Monte-Carlo Sampler.

See [1] for more details on Relativistic SGHMC.

[1] X. Lu, V. Perrone, L. Hasenclever, Y. W. Teh, S. J. Vollmer

In Proceedings of the 20 th International Conference on Artificial Intelligence and Statistics (AISTATS) 2017

Relativistic Monte Carlo

__init__(params, cost_fun, batch_generator=None, stepsize_schedule=<pysgmcmc.stepsize_schedules.ConstantStepsizeSchedule object>, mass=1.0, speed_of_light=1.0, D=1.0, Bhat=0.0, session=None, dtype=tf.float64, seed=None)[source]
Initialize the sampler parameters and set up a tensorflow.Graph
for later queries.
Parameters: params (list of tensorflow.Variable objects) – Target parameters for which we want to sample new values. Cost (tensorflow.Tensor) – 1-d Cost tensor that depends on params. Frequently denoted as U(theta) in literature. 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. stepsize_schedule (pysgmcmc.stepsize_schedules.StepsizeSchedule) – Iterator class that produces a stream of stepsize values that we can use in our samplers. See also: pysgmcmc.stepsize_schedules mass (float, optional) – mass constant. Defaults to 1.0. speed_of_light (float, optional) – “Speed of light” constant. TODO EXTEND DOKU Defaults to 1.0. D (float, optional) – Diffusion constant. Defaults to 1.0. Bhat (float, optional) – TODO: Documentation 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. dtype (tensorflow.DType, optional) – Type of elements of tensorflow.Tensor objects used in this sampler. Defaults to tensorflow.float64. seed (int, optional) – Random seed to use. Defaults to None.

pysgmcmc.sampling.MCMCSampler()
Base class for RelativisticSGHMCSampler that specifies how actual sampling is performed (using iterator protocol, e.g. next(sampler)).
class pysgmcmc.samplers.SGLDSampler(params, cost_fun, batch_generator=None, stepsize_schedule=<pysgmcmc.stepsize_schedules.ConstantStepsizeSchedule object>, burn_in_steps=3000, A=1.0, scale_grad=1.0, session=None, dtype=tf.float64, seed=None)[source]

Stochastic Gradient Langevin Dynamics Sampler that uses a burn-in procedure to adapt its own hyperparameters during the initial stages of sampling.

See [1] for more details on this burn-in procedure. See [2] for more details on Stochastic Gradient Langevin Dynamics.

[1] J. T. Springenberg, A. Klein, S. Falkner, F. Hutter

In Advances in Neural Information Processing Systems 29 (2016).

Bayesian Optimization with Robust Bayesian Neural Networks.

[2] M.Welling, Y. W. Teh

In International Conference on Machine Learning (ICML) 28 (2011).

Bayesian Learning via Stochastic Gradient Langevin Dynamics.

__init__(params, cost_fun, batch_generator=None, stepsize_schedule=<pysgmcmc.stepsize_schedules.ConstantStepsizeSchedule object>, burn_in_steps=3000, A=1.0, scale_grad=1.0, session=None, dtype=tf.float64, seed=None)[source]
Initialize the sampler parameters and set up a tensorflow.Graph
for later queries.
Parameters: params (list of tensorflow.Variable objects) – Target parameters for which we want to sample new values. cost_fun (callable) – Function that takes params as input and returns a 1-d tensorflow.Tensor that contains the cost-value. Frequently denoted with U in literature. 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. stepsize_schedule (pysgmcmc.stepsize_schedules.StepsizeSchedule) – Iterator class that produces a stream of stepsize values that we can use in our samplers. See also: pysgmcmc.stepsize_schedules burn_in_steps (int, optional) – Number of burn-in steps to perform. In each burn-in step, this sampler will adapt its own internal parameters to decrease its error. Defaults to 3000. For reference see: Bayesian Optimization with Robust Bayesian Neural Networks. A (float, optional) – TODO Doku Defaults to 1.0. scale_grad (float, optional) – Value that is used to scale the magnitude of the noise used during sampling. In a typical batches-of-data setting this usually corresponds to the number of examples in the entire dataset. 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. dtype (tensorflow.DType, optional) – Type of elements of tensorflow.Tensor objects used in this sampler. Defaults to tensorflow.float64. seed (int, optional) – Random seed to use. Defaults to None.

tensorflow_mcmc.sampling.mcmc_base_classes.BurnInMCMCSampler()
Base class for SGLDSampler that specifies how actual sampling is performed (using iterator protocol, e.g. next(sampler)).
class pysgmcmc.samplers.SVGDSampler(particles, cost_fun, batch_generator=None, stepsize_schedule=<pysgmcmc.stepsize_schedules.ConstantStepsizeSchedule object>, alpha=0.9, fudge_factor=1e-06, session=None, dtype=tf.float64, seed=None)[source]

See [1] for more details on stein variational gradient descent.

[1] Q. Liu, D. Wang

In Advances in Neural Information Processing Systems 29 (2016).

Stein Variational Gradient Descent: A General Purpose Bayesian Inference Algorithm.

__init__(particles, cost_fun, batch_generator=None, stepsize_schedule=<pysgmcmc.stepsize_schedules.ConstantStepsizeSchedule object>, alpha=0.9, fudge_factor=1e-06, session=None, dtype=tf.float64, seed=None)[source]
Initialize the sampler parameters and set up a tensorflow.Graph
for later queries.
Parameters: particles (List[tensorflow.Variable]) – List of particles each representing a (different) guess of the target parameters of this sampler. cost_fun (callable) – Function that takes params of one particle as input and returns a 1-d tensorflow.Tensor that contains the cost-value. Frequently denoted with U in literature. batch_generator (iterable, 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. stepsize_schedule (pysgmcmc.stepsize_schedules.StepsizeSchedule) – Iterator class that produces a stream of stepsize values that we can use in our samplers. See also: pysgmcmc.stepsize_schedules alpha (float, optional) – TODO DOKU Defaults to 0.9. fudge_factor (float, optional) – TODO DOKU Defaults to 1e-6. 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. dtype (tensorflow.DType, optional) – Type of elements of tensorflow.Tensor objects used in this sampler. Defaults to tensorflow.float64. seed (int, optional) – Random seed to use. Defaults to None.

pysgmcmc.sampling.MCMCSampler()
svgd_kernel(particles)[source]