Module phiml.latent
Dimensionality reduction and latent space models.
This module provides functionality for fitting and using latent models, such as UMAP, to reduce the dimensionality of data.
Functions
def fit(data: phiml.math._tensors.Tensor, model: str, latent_dim: Union[str, phiml.math._shape.Shape, int] = (vectorᶜ=l1,l2), feature_dim: Union[str, Sequence[+T_co], set, phiml.math._shape.Shape, Callable, None] = <function channel>, list_dim: Union[str, Sequence[+T_co], set, phiml.math._shape.Shape, Callable, None] = <function instance>, **model_kwargs) ‑> LatentModel
-
Fit a latent model to the data.
Args
data
- Tensor to fit the model to. Must contain
list_dim
. If nofeature_dim
is present, 1D data is assumed. model
- Model type to fit. Currently only 'UMAP' is supported.
latent_dim
- The dimension of the latent space to use. This determines the number of components in the latent representation.
The shape can be passed as a string spec, e.g. '(x,y)'.
Alternatively, the number of components can be passed as an
int
, in which case the shape will be channel of name 'vector'. feature_dim
- The dimension of the feature space used in distance computations.
list_dim
- Dimension along which data points belonging to one model are listed. Any dims not marked as list or feature are considered as batch dims.
Returns
LatentModel
- A LatentModel object containing the fitted model and its parameters.
Classes
class LatentModel (model_type: str, model: Any, feature_dim: phiml.math._shape.Shape, latent_dim: phiml.math._shape.Shape, can_reconstruct: bool)
-
LatentModel(model_type: str, model: Any, feature_dim: phiml.math._shape.Shape, latent_dim: phiml.math._shape.Shape, can_reconstruct: bool)
Expand source code
class LatentModel: model_type: str model: Any feature_dim: Shape latent_dim: Shape can_reconstruct: bool def embed(self, data: Tensor) -> Tensor: """ Embed the data using the fitted model or by training a new dimensionality reduction model. Args: data: Tensor to embed. Must contain `list_dim`. If no `feature_dim` is present, 1D data is assumed. Returns: The embedded data as a `Tensor`. """ list_dim = data.shape.without(self.feature_dim) if self.model_type == 'UMAP': def embed_single(x): embedded_np = self.model.transform(x.numpy([list_dim, self.feature_dim])) embedded_nat = data.backend.as_tensor(embedded_np) return wrap(embedded_nat, [list_dim, self.latent_dim]) return math.map(embed_single, data, dims=data.shape - self.feature_dim - list_dim) __call__ = embed def reconstruct(self, latent: Tensor) -> Tensor: """ Reconstruct the data from the latent representation if supported. Check `self.can_reconstruct` to see if reconstruction is supported. Args: latent: Tensor containing the latent representation. Must contain `self.latent_dim`. Returns: The reconstructed data as a `Tensor`. """ list_dim = latent.shape.without(self.latent_dim) if self.model_type == 'UMAP': def embed_single(x): reconstructed_np = self.model.inverse_transform(x.numpy([list_dim, self.latent_dim])) reconstructed_nat = latent.backend.as_tensor(reconstructed_np) return wrap(reconstructed_nat, [list_dim, self.feature_dim]) return math.map(embed_single, latent, dims=latent.shape - self.latent_dim - list_dim)
Class variables
var can_reconstruct : bool
var feature_dim : phiml.math._shape.Shape
var latent_dim : phiml.math._shape.Shape
var model : Any
var model_type : str
Methods
def embed(self, data: phiml.math._tensors.Tensor) ‑> phiml.math._tensors.Tensor
-
Embed the data using the fitted model or by training a new dimensionality reduction model.
Args
data
- Tensor to embed. Must contain
list_dim
. If nofeature_dim
is present, 1D data is assumed.
Returns
The embedded data as a
Tensor
. def reconstruct(self, latent: phiml.math._tensors.Tensor) ‑> phiml.math._tensors.Tensor
-
Reconstruct the data from the latent representation if supported. Check
self.can_reconstruct
to see if reconstruction is supported.Args
latent
- Tensor containing the latent representation. Must contain
self.latent_dim
.
Returns
The reconstructed data as a
Tensor
.