core.latents#
This module contains the classes implementing the components of the latent space of the core.model.BulkDGD, namely the Gaussian mixture model (core.latent.GaussianMixtureModel) and the representation layer (core.latent.RepresentationLayer).
- class bulkdgd.core.latents.GaussianMixtureModelLegacy(dim: int, n_components: int, means_prior_name: str, weights_prior_name: str, log_var_prior_name: str, means_prior_options: dict[str, object], weights_prior_options: dict[str, object], log_var_prior_options: dict[str, object], covariance_type: str = 'diagonal')#
A class implementing the legacy mixture of multivariate Gaussian distributions (Gaussian mixture model or GMM).
- forward(x: Tensor) Tensor#
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() Tensor#
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() float#
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: Tensor) Tensor#
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: int, n_samples_per_comp: int = 1, sampling_method: str = 'mean') Tensor#
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_components.The second dimension has a length equal to the dimensionality of the Gaussian mixture model.
- new_points
- sample_probs(x: Tensor) Tensor#
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
- save(file: str) None#
Save the legacy GMM parameters to a .pth file.
- Parameters:
- file
str The .pth file where to save the model parameters.
- file
- set_log_var(log_var: Tensor) None#
Set the log-variance of the components of the Gaussian mixture model.
- Parameters:
- log_var
torch.Tensor The log-variance of the components of the Gaussian mixture model.
This is a 2D tensor where:
The first dimension has a length equal to the number of components in the Gaussian mixture.
The second dimension has a length equal to the dimensionality of the Gaussian mixture model.
- log_var
- set_means(means: Tensor) None#
Set the means of the components of the Gaussian mixture model.
- Parameters:
- means
torch.Tensor The means of the components of the Gaussian mixture model.
This is a 2D tensor where:
The first dimension has a length equal to the number of components in the Gaussian mixture.
The second dimension has a length equal to the dimensionality of the Gaussian mixture model.
- means
- set_weights(weights: Tensor) None#
Set the weights of the components of the Gaussian mixture model.
- Parameters:
- weights
torch.Tensor The weights of the components of the Gaussian mixture model.
This is a 1D tensor whose size equals the number of components in the Gaussian mixture model.
- weights
- property covariance_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_components#
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 bulkdgd.core.latents.GaussianMixtureModelTGMM(n_components: int = 1, n_features: int = None, covariance_type: str = 'full', max_iter: int = 1000, tol: float = 0.0001, reg_covar: float = 1e-06, n_init: int = 1, init_means='kmeans', init_weights='uniform', init_covariances='empirical', random_state: int = None, warm_start: bool = False, cem: bool = False, weight_concentration_prior: Tensor = None, mean_prior: Tensor = None, mean_precision_prior: float = None, covariance_prior: Tensor = None, degrees_of_freedom_prior: float = None, verbose: bool = False, verbose_interval: int = 10, device: str = None, **kwargs)#
Compatibility wrapper for
tgmm.GaussianMixture.- forward(x: Tensor) Tensor#
Return negative log-density.
- 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 A 1D tensor whose size is equal to the number of input data points.
Each element of the tensor is the negative log-probability density of a data point.
- y
- get_mixture_probs() Tensor#
Get the mixture probabilities.
- Returns:
- mixture_probs
torch.Tensor The mixture probabilities.
- mixture_probs
- load_state_dict(state_dict, strict=False, *args, **kwargs)#
Load the state dictionary.
- log_prob(x: Tensor) Tensor#
Return the log-density.
- 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 whose size is equal to the number of input data points.
- log_prob
- sample_new_points(n_points: int, n_samples_per_comp: int = 1, sampling_method: str = 'mean') Tensor#
Draw samples from each component.
- 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.
- new_points
- sample_probs(x: Tensor) Tensor#
Sample probabilities.
- 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
- state_dict(*args, **kwargs)#
Get the state dictionary containing the parameters of the model.
- to(*args, **kwargs)#
Move the model parameters and internal tensors to the target device.
- property dim#
Alias for TGMM
n_features.
- property means#
Alias for TGMM
means_.
- property weights#
Alias for TGMM
weights_.
- class bulkdgd.core.latents.RepresentationLayer(values: Tensor | None = None, dist: str = 'normal', dist_options: Dict[str, int | float] | None = None, device: str | device | None = 'cpu')#
Class implementing a representation layer accumulating gradients.
This layer stores learned embeddings for data samples that can be optimized during training. It supports various initialization methods and utility functions for representation manipulation and analysis.
- classmethod load(file: str, device: str | device | None = None) RepresentationLayer#
Load representations from a file.
- Parameters:
- path
str The file from which to load the representations.
- device
strortorch.device, optional The device where to load the representations.
- path
- Returns:
- rep_layer
core.latent.RepresentationLayer The representation layer.
- rep_layer
- forward(ixs: list[int] | Tensor | None = None, index_map: dict[int, int] | None = None, batch_size: int | None = None) Tensor#
Forward pass that returns the values of the representations.
- Parameters:
- ixs
intortorch.Tensor, optional The indexes of the samples whose representations should be returned. If not passed, all representations will be returned.
- index_map
dict, optional A mapping from dataset indices to representation indices.
- batch_size
int, optional Process representations in batches of size
batch_sizefor memory efficiency.
- 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() None#
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.
- save(file: str) None#
Save the representations to a .pth file.
- Parameters:
- file
str The .pth file where to save the representations.
- file
- property device#
The device where the model is.
- 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.