vi

bnet

Module for the Bayesian network.

class quinn.vi.bnet.BNet(nnmodel, pi=0.5, sigma1=1.0, sigma2=1.0, mu_init_lower=-0.2, mu_init_upper=0.2, rho_init_lower=-5.0, rho_init_upper=-4.0)[source]

Bases: Module

Bayesian NN class.

device

Device where the computations are done.

Type:

str

log_prior

Value of log-prior.

Type:

float

log_variational_posterior

Value of logarithm of variational posterior.

Type:

float

nnmodel

The underlying PyTorch NN module.

Type:

torch.nn.Module

nparams

Number of deterministic parameters.

Type:

int

param_names

List of parameter names.

Type:

list[str]

param_priors

List of parameter priors.

Type:

list[quinn.vi.rvs.RV]

params

Variational parameters.

Type:

torch.nn.ParameterList

rparams

List of variational PDFs.

Type:

list[quinn.rvar.rvs.RV]

__init__(nnmodel, pi=0.5, sigma1=1.0, sigma2=1.0, mu_init_lower=-0.2, mu_init_upper=0.2, rho_init_lower=-5.0, rho_init_upper=-4.0)[source]

Instantiate a Bayesian NN object given an underlying PyTorch NN module.

Parameters:
  • nnmodel (torch.nn.Module) – The original PyTorch NN module.

  • pi (float) – Weight of the first gaussian. The second weight is 1-pi.

  • sigma1 (float) – Standard deviation of the first gaussian. Can also be a scalar torch.Tensor.

  • sigma2 (float) – Standard deviation of the second gaussian. Can also be a scalar torch.Tensor.

  • mu_init_lower (float) – Initialization of mu lower value

  • mu_init_upper (float) – Initialization of mu upper value

  • rho_init_lower (float) – Initialization of rho lower value

  • rho_init_upper (float) – Initialization of rho upper value

del_attr(obj, names)[source]

Deletes attributes from a given object.

Parameters:
  • obj (any) – The object of interest.

  • names (list) – List that corresponds to the attribute to be deleted. If list is [‘A’, ‘B’, ‘C’], the attribute A.B.C is deleted recursively.

set_attr(obj, names, val)[source]

Sets attributes of a given object.

Parameters:
  • obj (any) – The object of interest.

  • names (list) – List that corresponds to the attribute of interest. If list is [‘A’, ‘B’, ‘C’], the attribute A.B.C is filled with value val.

  • val (torch.Tensor) – Value to be set.

forward(x, sample=False, par_samples=None)[source]

Forward function of Bayesian NN object.

Parameters:
  • x (torch.Tensor) – Input array of size (N,d).

  • sample (bool, optional) – Whether this is used in a sampling mode or not.

  • par_samples (None, optional) – Parameter samples. Default is None, in which cases the mean values of variational PDFs are used.

Returns:

Output array of size (N,o).

Return type:

torch.Tensor

sample_elbo(x, target, nsam, likparams=None)[source]

Sample from ELBO.

Parameters:
  • x (torch.Tensor) – A 2d input tensor.

  • target (torch.Tensor) – A 2d output tensor.

  • nsam (int) – Number of samples

  • likparams (tuple, optional) – Other parameters of the likelihood, e.g. data noise.

Returns:

(log_prior, log_variational_posterior, negative_log_likelihood)

Return type:

tuple

viloss(data, target)[source]

Variational loss function L(x,y).

Parameters:
  • data (torch.Tensor) – A 2d input tensor x.

  • target (torch.Tensor) – A 2d output tensor y.

Returns:

The value of loss function.

Return type:

float