core.latent

This module contains the classes implementing the components of the latent space of the core.model.BulkDGDModel, namely the Gaussian mixture model (core.latent.GaussianMixtureModel) and the representation layer (core.latent.RepresentationLayer).

class core.latent.GaussianMixtureModel(dim, n_comp, means_prior_name, weights_prior_name, log_var_prior_name, means_prior_options, weights_prior_options, log_var_prior_options, cm_type='diagonal')

A class implementing a mixture of multivariate Gaussian distributions (Gaussian mixture model or GMM).

__init__(dim, n_comp, means_prior_name, weights_prior_name, log_var_prior_name, means_prior_options, weights_prior_options, log_var_prior_options, cm_type='diagonal')

Initialize an instance of the GMM.

Parameters:
dimint

The dimensionality of the Gaussian mixture model.

n_compint

The number of components in the Gaussian mixture.

means_prior_namestr, {"softball"}

The name of the prior over the means of the components of the Gaussian mixture.

weights_prior_namestr, {"dirichlet"}

The name of the prior over the weights of the components of the Gaussian mixture.

log_var_prior_namestr, {"gaussian"}

The name of the prior over the negative log-variance of the components of the Gaussian mixture.

means_prior_optionsdict

A dictionary containing the options needed to set up the prior over the means of the components of the Gaussian mixture model.

It varies according to the selected prior.

If means_prior_name is "softball", the options that must be provided are:

  • "radius", namely the radius of the multi- dimensional soft ball.

  • "sharpness", namely the sharpness of the soft boundary of the ball.

weights_prior_optionsdict

A dictionary containing the options needed to set up the prior over the weights of the components of the Gaussian mixture model.

It varies according to the selected prior.

If weights_prior_name is "dirichlet", the options that must be provided are:

  • "alpha", namely the alpha of the Dirichlet distribution.

log_var_prior_optionsdict

A dictionary containing the options needed to set up the prior over the negative log-variance of the Gaussian mixture model.

It varies according to the selected prior.

If log_var_prior_name is "gaussian", the options that must be provided are:

  • "mean", namely the mean of the Gaussian distribution.

  • "stddev", namely the standard deviation of the Gaussian distribution.

cm_typestr, {"fixed", "isotropic", "diagonal"}, "diagonal"

The type of covariance matrix used.

forward(x)

Forward pass - compute the absolute log-probability density for a set of data points.

Parameters:
xtorch.Tensor

The input data points. This is a 2D tensor where:

  • The first dimension has a length equal to the number of data points.

  • The second dimension has a length equal to the dimensionality of the data points.

Returns:
ytorch.Tensor

The result of the forward pass.

This is a 1D tensor whose size is equal to the number of input data points.

Each element of the tensor is the absolute log-probability density of a data point.

get_mixture_probs()

Convert the weights into mixture probabilities using the softmax function.

Returns:
mixture_probstorch.Tensor

The mixture probabilities.

This is a 1D tensor whose size equals the number of components in the Gaussian mixture model.

get_prior_log_prob()

Calculate the log-probability of the prior over the means, log-variance, and mixture coefficients.

Returns:
pfloat

The log-probability of the prior.

log_prob(x)

Get the log-probability density of a set of samples drawn from the Gaussian mixture model.

Parameters:
xtorch.Tensor

The input data points. This is a 2D tensor where:

  • The first dimension has a length equal to the number of data points.

  • The second dimension has a length equal to the dimensionality of the data points.

Returns:
log_probtorch.Tensor

A 1D tensor storing the log-probability density of each input data point to be drawn from the Gaussian mixture model.

The tensor has a size equal to the number of data points passed.

sample_new_points(n_points, n_samples_per_comp=1, sampling_method='mean')

Draw samples for new data points from each component of the Gaussian mixture model.

Parameters:
n_pointsint

The number of data points for which samples should be drawn.

n_samples_per_compint, 1

The number of samples to draw per data point per component of the Gaussian mixture.

sampling_methodstr, {"mean"}, "mean"

How to draw the samples for the given data points.

Available options are:

  • "mean" means taking the mean of each component as the value of each n_samples_per_comp sample taken for each data point.

Returns:
new_pointstorch.Tensor

The samples drawn.

This is a 2D tensor where:

  • The first dimension has a length equal to n_points * n_reps_per_mix_comp * n_comp.

  • The second dimension has a length equal to the dimensionality of the Gaussian mixture model.

sample_probs(x)

Get the probability density per sample per component.

Parameters:
xtorch.Tensor

The input data points. This is a 2D tensor where:

  • The first dimension has a length equal to the number of data points.

  • The second dimension has a length equal to the dimensionality of the data points.

Returns:
probstorch.Tensor

The probability densities.

This is a 2D tensor where:

  • The first dimension has a length equal to the number of input data points.

  • The second dimension has a length equal to the number of components in the Gaussian mixture.

Each element of the tensor stores a per-data point, per-component probability density.

property cm_type

The type of the covariance matrix.

property dim

The dimensionality of the Gaussian mixture model.

property log_var

The log-variance of the components of the Gaussian mixture model.

property log_var_prior

A dictionary containing the name of the prior over the log-variance of the components of the Gaussian mixture model and the options used to set it up.

property means

The means of the components of the Gaussian mixture model.

property means_prior

A dictionary containing the name of the prior over the means of the components of the Gaussian mixture model and the options used to set it up.

property n_comp

The number of components in the Gaussian mixture.

property weights

The weights of the components of the Gaussian mixture model.

property weights_prior

A dictionary containing the name of the prior over the weights of the components of the Gaussian mixture model and the options used to set it up.

class core.latent.RepresentationLayer(values=None, dist='normal', dist_options=None)

Class implementing a representation layer accumulating gradients.

__init__(values=None, dist='normal', dist_options=None)

Initialize a representation layer.

Parameters:
valuestorch.Tensor, optional

A tensor used to initialize the representations in the layer.

This is a 2D tensor where:

  • The first dimension has a length equal to the number of representations in the tensor.

  • The second dimension has a length equal to the dimensionality of the representations.

If the tensor is not passed, the representations will be initialized by sampling the distribution specified by dist.

diststr, {"normal"}, default: "normal"

The name of the distribution used to sample the representations, if no values are passed.

Available options are:

  • "normal" : sample the representations from a normal distribution.

dist_optionsdict, optional

A dictionary containing the parameters to sample the representations from the distribution, if no values are passed.

For any distribution the following keys and associated parameters must be provided:

  • "n_samples" : the number of samples to draw from the distribution.

  • "dim" : the dimensionality of the representations to sample from the distribution.

If dist is "normal", the dictionary must contain these additional key/value pairs:

  • "mean" : the mean of the normal distribution used to generate the representations.

  • "stddev" : the standard deviation of the normal distribution used to generate the representations.

forward(ixs=None)

Forward pass - it returns the values of the representations.

You can select a subset of representations to be returned using their numerical indexes.

Parameters:
ixsint, optional

The indexes of the samples whose representations should be returned. If not passed, all representations will be returned.

Returns:
repstorch.Tensor

A tensor containing the values of the representations for the samples of interest.

This is a 2D tensor where:

  • The first dimension has a length equal to the number of representations.

  • The second dimension has a length equal to the dimensionality of the representations.

rescale()

Rescale the representations by subtracting the mean of the representations’ values from each of them and dividing each of them by the standard deviation of all representations.

Given \(N\) samples, we can indicate with \(z^{n}\) the value of the representation of sample \(x^{n}\).

Therefore, the rescaled value of the representation \(z^{n}_{rescaled}\) will be:

\[z^{n}_{rescaled} = \frac{z^{n} - \bar{z}}{s}\]

Where \(\bar{z}\) is the mean of the representations’ values and \(s\) is the standard deviation.

Returns:
reps_rescaledtorch.Tensor

The rescaled values of the representations.

This is a 2D tensor where:

  • The first dimension has a length equal to the number of representations.

  • The second dimension has a length equal to the dimensionality of the representations.

property dim

The dimensionality of the representations.

property n_rep

The number of representations in the layer.

property options

The dictionary ot options used to generate the representations, if no values were passed when initializing the layer.

property z

The values of the representations.