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:
- dim
int The dimensionality of the Gaussian mixture model.
- n_comp
int The number of components in the Gaussian mixture.
- means_prior_name
str, {"softball"} The name of the prior over the means of the components of the Gaussian mixture.
- weights_prior_name
str, {"dirichlet"} The name of the prior over the weights of the components of the Gaussian mixture.
- log_var_prior_name
str, {"gaussian"} The name of the prior over the negative log-variance of the components of the Gaussian mixture.
- means_prior_options
dict 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_nameis"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_options
dict 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_nameis"dirichlet", the options that must be provided are:"alpha", namely the alpha of the Dirichlet distribution.
- log_var_prior_options
dict 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_nameis"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_type
str, {"fixed","isotropic","diagonal"},"diagonal" The type of covariance matrix used.
- dim
- forward(x)
Forward pass - compute the absolute log-probability density for a set of data points.
- Parameters:
- x
torch.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.
- x
- Returns:
- y
torch.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.
- y
- get_mixture_probs()
Convert the weights into mixture probabilities using the softmax function.
- Returns:
- mixture_probs
torch.Tensor The mixture probabilities.
This is a 1D tensor whose size equals the number of components in the Gaussian mixture model.
- mixture_probs
- get_prior_log_prob()
Calculate the log-probability of the prior over the means, log-variance, and mixture coefficients.
- Returns:
- p
float The log-probability of the prior.
- p
- log_prob(x)
Get the log-probability density of a set of samples drawn from the Gaussian mixture model.
- Parameters:
- x
torch.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.
- x
- Returns:
- log_prob
torch.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.
- log_prob
- 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_points
int The number of data points for which samples should be drawn.
- n_samples_per_comp
int,1 The number of samples to draw per data point per component of the Gaussian mixture.
- sampling_method
str, {"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 eachn_samples_per_compsample taken for each data point.
- n_points
- Returns:
- new_points
torch.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.
- new_points
- sample_probs(x)
Get the probability density per sample per component.
- Parameters:
- x
torch.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.
- x
- Returns:
- probs
torch.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.
- probs
- 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:
- values
torch.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.- dist
str, {"normal"}, default:"normal" The name of the distribution used to sample the representations, if no
valuesare passed.Available options are:
"normal": sample the representations from a normal distribution.
- dist_options
dict, optional A dictionary containing the parameters to sample the representations from the distribution, if no
valuesare 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
distis"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.
- values
- 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:
- ixs
int, optional The indexes of the samples whose representations should be returned. If not passed, all representations will be returned.
- ixs
- Returns:
- reps
torch.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.
- reps
- 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_rescaled
torch.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.
- reps_rescaled
- 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.