Parametric Autoencoder#
autoencoder
#
| CLASS | DESCRIPTION |
|---|---|
Autoencoder |
Autoencoder for unsupervised representation learning and reconstruction. |
Classes#
Autoencoder(*, model: dict[str, ModelType], loss_kwargs: dict | None = None, optimizer_fn: type[torch.optim.Optimizer] | str | None = None, optimizer_kwargs: dict | None = None, lr_scheduler_fn: Any | str | None = None, lr_scheduler_kwargs: dict | None = None, device: Literal['cuda', 'cpu'] = 'cuda')
#
Bases: Parametric
Autoencoder for unsupervised representation learning and reconstruction.
This class implements a standard autoencoder architecture with separate encoder and decoder networks. The encoder compresses input data into a lower-dimensional latent representation, while the decoder reconstructs the original input from this representation. The model is trained to minimize reconstruction error.
The autoencoder uses mean squared error (MSE) loss for reconstruction \(L = \frac{1}{N} \sum_{i=1}^{N} \| X_i - \hat{X}_i \|^2_2\), where \(X\) is the input data and \(\hat{X}\) is the reconstructed output.
| PARAMETER | DESCRIPTION |
|---|---|
model
|
Dictionary containing 'encoder' and 'decoder' model specifications. Both networks are required for autoencoder functionality:
Example:
TYPE:
|
loss_kwargs
|
Additional keyword arguments to pass to the MSELoss constructor.
TYPE:
|
optimizer_fn
|
Optimizer specification for training.
TYPE:
|
optimizer_kwargs
|
Keyword arguments for optimizer instantiation
(e.g.,
TYPE:
|
lr_scheduler_fn
|
Learning rate scheduler specification.
TYPE:
|
lr_scheduler_kwargs
|
Keyword arguments for scheduler instantiation.
TYPE:
|
device
|
Computation device. Automatically falls back to "cpu" if CUDA is unavailable.
TYPE:
|
| ATTRIBUTE | DESCRIPTION |
|---|---|
decoder |
Parsed decoder network on the specified device. Also available as
TYPE:
|
loss_fn |
Mean squared error loss function for reconstruction.
TYPE:
|
Examples:
>>> import torch
>>> from spectre.parametric import Autoencoder
>>> from spectre.core import Model
>>>
>>> # Create encoder (input -> latent)
>>> encoder = Model(in_features=784, out_features=64, multipliers=[0.5])
>>> # Create decoder (latent -> output)
>>> decoder = Model(in_features=64, out_features=784, multipliers=[2.0])
>>>
>>> # Initialize with model dictionary
>>> autoencoder = Autoencoder(
.. model={"encoder": encoder, "decoder": decoder}
.. )
>>>
>>> # Training data
>>> X = torch.randn(100, 784)
>>> batch = X[:32] # Single batch
>>> loss = autoencoder.training_step(batch, 0)
| METHOD | DESCRIPTION |
|---|---|
loss |
Compute autoencoder reconstruction loss between input and decoded output. |
forward_step |
Forward pass through the encoder network. |
score_step |
Compute reconstruction loss for evaluation without training status checks. |
training_step |
Compute autoencoder reconstruction loss for a single training batch. |
Source code in spectre/parametric/autoencoder.py
Functions#
loss(X: torch.Tensor, weights: torch.Tensor | None = None, target: torch.Tensor | None = None) -> torch.Tensor
#
Compute autoencoder reconstruction loss between input and decoded output.
Performs encoding and decoding operations, then computes mean squared error between the original input and the reconstructed output.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input data tensor to encode and reconstruct.
TYPE:
|
weights
|
Sample weights for weighted loss computation. If None, uniform weighting is applied.
TYPE:
|
target
|
Unused for autoencoders (reconstruction target is always the input).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Reconstruction loss value. |
Source code in spectre/parametric/autoencoder.py
forward_step(X: torch.Tensor) -> torch.Tensor
#
Forward pass through the encoder network.
Compresses input data into a lower-dimensional latent representation using the encoder network. This is the first step of the autoencoder pipeline, followed by decoding for reconstruction.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input data to encode into latent representation.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
torch.Tensor of shape (n_samples, latent_features)
|
Encoded latent representation of the input data. |
Source code in spectre/parametric/autoencoder.py
score_step(X: torch.Tensor, weights: torch.Tensor | None = None, target: torch.Tensor | None = None) -> torch.Tensor
#
Compute reconstruction loss for evaluation without training status checks.
This method provides direct access to the autoencoder's reconstruction
loss computation for evaluation purposes, bypassing the is_trained
requirement of the inherited score method.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input data to encode and reconstruct.
TYPE:
|
weights
|
Sample weights for weighted loss computation. If None, uniform weighting is applied.
TYPE:
|
target
|
Unused for autoencoders (reconstruction target is the input itself).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Reconstruction loss value. |
Source code in spectre/parametric/autoencoder.py
training_step(batch: DataBatch, batch_idx: int) -> torch.Tensor
#
Compute autoencoder reconstruction loss for a single training batch.
Performs forward pass through encoder and decoder networks, then computes reconstruction loss between input and decoded output. Automatically logs the loss for monitoring during training and validation.
| PARAMETER | DESCRIPTION |
|---|---|
batch
|
Training batch containing (input_data, sample_weights) where:
TYPE:
|
batch_idx
|
Batch index.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Reconstruction loss value. |