Ensemble Embedding#
embedding
#
| CLASS | DESCRIPTION |
|---|---|
EmbeddingEnsemble |
Ensemble of multiple embeddings with configurable aggregation. |
Attributes#
Classes#
EmbeddingEnsemble(estimators: list[ModelType] | dict[str, ModelType], reduce: str = 'mean', estimator_weights: list[float] | dict[str, float] | None = None, alignment: str = 'procrustes', fit_manager: FitManager | None = None)
#
Bases: Ensemble
Ensemble of multiple embeddings with configurable aggregation.
Combines embeddings from multiple diffusion maps, PCAs, or other methods using various reduction strategies. Provides robust embeddings that are less sensitive to hyperparameter choices.
| PARAMETER | DESCRIPTION |
|---|---|
estimators
|
Models to construct ensemble. |
reduce
|
Reduction method for combining embeddings:
TYPE:
|
estimator_weights
|
Estimator weights for "weighted_mean" reduce. Must match estimators structure.
If None and
TYPE:
|
alignment
|
Whether to align embeddings before reduction.
TYPE:
|
fit_manager
|
Manager for unified training of both Estimator and Parametric models. Required for using Parametric (PyTorch Lightning) models in embedding ensemble. See FitManager for configuration options.
TYPE:
|
| ATTRIBUTE | DESCRIPTION |
|---|---|
estimators |
Dictionary of fitted models with names as keys.
TYPE:
|
estimator_weights |
Normalized estimator weights for reduction.
TYPE:
|
embeddings |
Individual embeddings from each model (stored during fit).
TYPE:
|
Examples:
Ensemble diffusion maps with different kernels:
>>> from spectre.decomposition import DiffusionMap
>>> from spectre.kernel import GaussianKernel, TKernel
>>> from spectre.pairwise_distance import PairwiseDistanceEuclidean
>>> from spectre.ensemble import EmbeddingEnsemble
>>>
>>> # Create estimators with different kernels
>>> dm_gaussian = DiffusionMap(
... kernel_fn=GaussianKernel(bw_method=torch.tensor(1.0)),
... distance_fn=PairwiseDistanceEuclidean(),
... out_features=5,
... )
>>> dm_t = DiffusionMap(
... kernel_fn=TKernel(
... alpha=torch.tensor(1.0),
... beta=torch.tensor(3.0),
... ),
... distance_fn=PairwiseDistanceEuclidean(),
... out_features=5,
... )
>>>
>>> # Create ensemble
>>> ensemble = EmbeddingEnsemble(
... estimators=[dm_gaussian, dm_t],
... reduce="mean",
... alignment="procrustes",
... )
>>>
>>> X = torch.randn(200, 10)
>>> ensemble.fit(X)
>>> X_embedded = ensemble.predict(X)
Named estimators with custom weights:
>>> ensemble = EmbeddingEnsemble(
... estimators={"gaussian": dm_gaussian, "t_kernel": dm_t},
... reduce="weighted_mean",
... estimator_weights={"gaussian": 0.7, "t_kernel": 0.3},
... )
>>> ensemble.fit(X)
Access individual estimator embeddings:
>>> ensemble.fit(X)
>>> for name, emb in zip(["gaussian", "t_kernel"], ensemble.embeddings):
... print(f"{name}: {emb.shape}")
| METHOD | DESCRIPTION |
|---|---|
fit |
Fit all estimators independently on the data. |
predict |
Combine predictions from all estimators. |
score |
Reduce scores from all estimators. |
get_stability_score |
Compute stability across ensemble members. |
Source code in spectre/ensemble/embedding.py
Attributes#
embeddings: list[torch.Tensor]
property
#
Return the embeddings of the fitted estimators.
estimator_weights: dict[str, float]
property
#
Return the weights of the fitted estimators.
Functions#
fit(X: torch.Tensor, weights: torch.Tensor | None = None, target: torch.Tensor | None = None) -> EmbeddingEnsemble
#
Fit all estimators independently on the data.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Training data of shape (n_samples, in_features).
TYPE:
|
weights
|
Sample weights of shape (n_samples, ).
TYPE:
|
target
|
Target values for supervised methods.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
EmbeddingEnsemble
|
Returns self for method chaining. |
Source code in spectre/ensemble/embedding.py
predict(X: torch.Tensor) -> torch.Tensor
#
Combine predictions from all estimators.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input data of shape (n_samples, in_features).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Reduced embedding of shape (n_samples, out_features). |
Source code in spectre/ensemble/embedding.py
score(X: torch.Tensor, weights: torch.Tensor | None = None, target: torch.Tensor | None = None) -> float
#
Reduce scores from all estimators.
Computes the mean score across all ensemble members.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input data of shape (n_samples, in_features).
TYPE:
|
weights
|
Sample weights.
TYPE:
|
target
|
Target values.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
float
|
Mean score across estimators. |
Source code in spectre/ensemble/embedding.py
get_stability_score(X: torch.Tensor) -> float
#
Compute stability across ensemble members.
Measures pairwise agreement between estimator embeddings using Procrustes error. Lower values indicate higher agreement.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input data of shape (n_samples, in_features).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
float
|
Mean Procrustes error between all pairs of embeddings. |