Mixin

class pysgmcmc.samplers.mixin.SamplerMixin(negative_log_likelihood, params, *args, **kwargs)[source]

Mixin class that turns a torch.nn.optim.Optimizer into a MCMC sampler.

__init__(negative_log_likelihood, params, *args, **kwargs)[source]
Instantiate a sampler object.
(Initial) parameters are passed as iterable params, negative_log_likelihood is a function mapping parameters to a NLL value and *args and **kwargs allow specifying additional arguments to pass to a sampler, e.g. lr or mdecay.
Parameters:
  • negative_log_likelihood (typing.Callable[[typing.Iterable[torch.Tensor]], torch.Tensor]) – Callable mapping parameters to a NLL value.
  • params (iterable) – Iterable of parameters used to construct samples.

See also

pysgmcmc.samplers.sghmc.SGHMC()
SGHMC sampler that uses this mixin.
__next__()[source]
Perform a step of this sampler and return parameters with costs.
Together with __iter__, this allows using samplers as iterables.
Returns:
  • parameters (typing.Tuple[numpy.ndarray, …]) – Current parameters.
  • cost (torch.Tensor) – NLL value associated with parameters.
__weakref__

list of weak references to the object (if defined)

parameters

Return last sample as tuple of numpy arrays.

Returns:current_parameters – Tuple of numpy arrays containing last sampled values.
Return type:typing.Tuple[numpy.ndarray, ..]
sample_step()[source]

Perform a single step with the sampler.

Returns:
  • parameters (typing.Tuple[numpy.ndarray, …]) – Current parameters.
  • cost (torch.Tensor) – NLL value associated with parameters.
  • next_parameters (typing.Tuple[numpy.ndarray, …]) – Parameters to evaluate on a subsequent call.