gpytorch.Module

class gpytorch.Module[source]
initialize(**kwargs)[source]

Set a value for a parameter

kwargs: (param_name, value) - parameter to initialize. Can also initialize recursively by passing in the full name of a parameter. For example if model has attribute model.likelihood, we can initialize the noise with either model.initialize(**{‘likelihood.noise’: 0.1}) or model.likelihood.initialize(noise=0.1). The former method would allow users to more easily store the initialization values as one object.

Value can take the form of a tensor, a float, or an int

local_load_samples(samples_dict, memo, prefix)[source]

Defines local behavior of this Module when loading parameters from a samples_dict generated by a Pyro sampling mechanism.

The default behavior here should almost always be called from any overriding class. However, a class may want to add additional functionality, such as reshaping things to account for the fact that parameters will acquire an extra batch dimension corresponding to the number of samples drawn.

named_added_loss_terms()[source]

Returns an iterator over module variational strategies, yielding both the name of the variational strategy as well as the strategy itself.

Yields:
(string, VariationalStrategy): Tuple containing the name of the
strategy and the strategy
named_priors(memo=None, prefix='')[source]

Returns an iterator over the module’s priors, yielding the name of the prior, the prior, the associated parameter names, and the transformation callable.

Yields:
(string, Prior, tuple((Parameter, callable)), callable): Tuple containing:
  • the name of the prior
  • the prior
  • a tuple of tuples (param, transform), one for each of the parameters associated with the prior
  • the prior’s transform to be called on the parameters
pyro_load_from_samples(samples_dict)[source]

Convert this Module in to a batch Module by loading parameters from the given samples_dict. samples_dict is typically produced by a Pyro sampling mechanism.

Note that the keys of the samples_dict should correspond to prior names (covar_module.outputscale_prior) rather than parameter names (covar_module.raw_outputscale), because we will use the setting_closure associated with the prior to properly set the unconstrained parameter.

Args:
samples_dict (dict): Dictionary mapping prior names to sample values.
pyro_sample_from_prior()[source]

For each parameter in this Module and submodule that have defined priors, sample a value for that parameter from its corresponding prior with a pyro.sample primitive and load the resulting value in to the parameter.

This method can be used in a Pyro model to conveniently define pyro sample sites for all parameters of the model that have GPyTorch priors registered to them.

register_parameter(name, parameter, prior=None)[source]

Adds a parameter to the module. The parameter can be accessed as an attribute using the given name.

Args:
name (str):
The name of the parameter
parameter (torch.nn.Parameter):
The parameter
register_prior(name, prior, param_or_closure, setting_closure=None)[source]

Adds a prior to the module. The prior can be accessed as an attribute using the given name.

Args:
name (str):
The name of the prior
prior (Prior):
The prior to be registered`
param_or_closure (string or callable):
Either the name of the parameter, or a closure (which upon calling evalutes a function on one or more parameters): single parameter without a transform: .register_prior(“foo_prior”, foo_prior, “foo_param”) transform a single parameter (e.g. put a log-Normal prior on it): .register_prior(“foo_prior”, NormalPrior(0, 1), lambda: torch.log(self.foo_param)) function of multiple parameters: .register_prior(“foo2_prior”, foo2_prior, lambda: f(self.param1, self.param2)))
setting_closure (callable, optional):
A function taking in a tensor in (transformed) parameter space and initializing the internal parameter representation to the proper value by applying the inverse transform. Enables setting parametres directly in the transformed space, as well as sampling parameter values from priors (see sample_from_prior)
sample_from_prior(prior_name)[source]

Sample parameter values from prior. Modifies the module’s parameters in-place.