gfa.model#
GFAModel class for Group Factor Analysis.
- class GFAModel(
- config: GFAFitConfig | None = None,
- obs_hyperprior: ObsParamsHyperPrior | None = None,
High-level interface for Group Factor Analysis.
- Parameters:
- Attributes:
- config
GFAFitConfig Fitting configuration (immutable after construction).
- obs_posterior
ObsParamsPosteriororNone Posterior over observation model parameters. None until fit.
- latents_posterior
LatentsPosteriorStaticorNone Posterior over latent variables. None until fit.
- tracker
GFAFitTrackerorNone Fitting progress tracker. None until fit.
- flags
GFAFitFlagsorNone Fitting status flags. None until fit.
- config
Examples
Basic usage: fit and infer
>>> from latents.gfa import GFAModel, GFAFitConfig >>> from latents.callbacks import ProgressCallback >>> config = GFAFitConfig(x_dim_init=10) >>> model = GFAModel(config=config) >>> model.fit(Y, callbacks=[ProgressCallback()]) >>> X_new = model.infer_latents(Y_new)
Persistence: save and load
>>> model.save("fitted_model.safetensors") >>> loaded = GFAModel.load("fitted_model.safetensors")
Methods:
Fit model to data via variational inference.
Resume an interrupted fit.
Clear fit results for fresh initialization on next fit().
Infer latent posterior for new data given fitted parameters.
Recompute latents from data.
Recompute loading posterior from data.
Save model to a safetensors file.
Load model from a safetensors file.
- property obs_hyperprior: ObsParamsHyperPrior#
Observation model hyperprior parameters.
- property obs_prior: ObsParamsPrior#
Observation model prior.
- property latents_prior: LatentsPriorStatic#
Latent variable prior.
- fit( ) Self[source]#
Fit model to data via variational inference.
Resets tracker and flags. Warm-starts from posteriors if present, otherwise initializes from scratch.
- Parameters:
- Returns:
SelfThe fitted model (for method chaining).
Examples
>>> from latents.gfa import GFAModel, GFAFitConfig >>> from latents.callbacks import ProgressCallback >>> config = GFAFitConfig(x_dim_init=10, max_iter=100) >>> model = GFAModel(config=config) >>> model.fit(Y, callbacks=[ProgressCallback()])
- resume_fit( ) Self[source]#
Resume an interrupted fit.
Appends to tracker, preserves convergence baseline.
- Parameters:
- Returns:
SelfThe model (for method chaining).
- Raises:
ValueErrorIf no fit to resume (posteriors not initialized).
- clear_fit() Self[source]#
Clear fit results for fresh initialization on next fit().
- Returns:
SelfThe model (for method chaining).
- infer_latents(
- Y: ObsStatic,
Infer latent posterior for new data given fitted parameters.
Does not modify the model’s stored latents_posterior.
- Parameters:
- Y
ObsStatic Observed data.
- Y
- Returns:
LatentsPosteriorStaticPosterior over latent variables for the given data.
- Raises:
ValueErrorIf model has not been fitted.
Examples
>>> model.fit(Y_train) >>> X_test = model.infer_latents(Y_test) >>> X_test.mean.shape (5, 50)
- recompute_latents(Y: ObsStatic) Self[source]#
Recompute latents from data. Updates self.latents_posterior.
Use this to restore latents after loading a model saved with save_x=False.
- Parameters:
- Y
ObsStatic Observed data (typically the training data).
- Y
- Returns:
SelfThe model (for method chaining).
- Raises:
ValueErrorIf model has not been fitted.
- recompute_loadings(Y: ObsStatic) Self[source]#
Recompute loading posterior from data. Updates self.obs_posterior.C.
Use this to restore C.cov after loading a model saved with save_c_cov=False.
Note: At convergence, the recomputed values are essentially identical to the original fitted values. Pre-convergence, there may be non-negligible differences because the reconstruction uses the final latents posterior rather than the latents posterior from the previous iteration.
- Parameters:
- Y
ObsStatic Observed data (typically the training data).
- Y
- Returns:
SelfThe model (for method chaining).
- Raises:
ValueErrorIf model has not been fitted, or if latents are not available.
- save(path: str | PathLike[str]) None[source]#
Save model to a safetensors file.
Uses safetensors format for secure serialization (no arbitrary code execution on load). Arrays are stored as tensors; scalars and config are stored as JSON in metadata.
- Parameters:
- path
strorPathLike Output file path (conventionally ends in .safetensors).
- path