torch_concepts.data.preprocessing.autoencoder.AutoencoderTrainer

class AutoencoderTrainer(input_shape: int, noise: float = 0.0, latent_dim: int = 32, lr: float = 0.0005, epochs: int = 2000, batch_size: int = 512, patience: int = 50, device=None)[source]

Trainer class for autoencoder models with early stopping.

Provides training loop, early stopping, and latent representation extraction for autoencoder models.

model

The autoencoder model.

Type:

SimpleAutoencoder

criterion

Reconstruction loss function.

Type:

nn.MSELoss

optimizer

Optimizer for training.

Type:

optim.Adam

device

Device to train on (‘cpu’ or ‘cuda’).

Type:

str

Parameters:
  • input_shape – Number of input features.

  • noise – Noise level to add to latent representations (default: 0.5).

  • latent_dim – Dimension of latent space (default: 32).

  • lr – Learning rate (default: 0.0005).

  • epochs – Maximum training epochs (default: 2000).

  • batch_size – Batch size for training (default: 512).

  • patience – Early stopping patience in epochs (default: 50).

  • device – Device to use for training (default: ‘cpu’).

Example

>>> import torch
>>> from torch_concepts.data.preprocessing.autoencoder import AutoencoderTrainer
>>>
>>> # Create synthetic data
>>> data = torch.randn(1000, 100)
>>>
>>> # Create and train autoencoder
>>> trainer = AutoencoderTrainer(
...     input_shape=100,
...     latent_dim=16,
...     epochs=100,
...     batch_size=64,
...     device='cpu'
... )
>>>
>>> # Train
>>> trainer.train(data)
Autoencoder training started...
>>>
>>> # Extract latent representations
>>> latent = trainer.extract_latent()
>>> print(latent.shape)
torch.Size([1000, 16])
__init__(input_shape: int, noise: float = 0.0, latent_dim: int = 32, lr: float = 0.0005, epochs: int = 2000, batch_size: int = 512, patience: int = 50, device=None)[source]

Methods

__init__(input_shape[, noise, latent_dim, ...])

extract_latent()

Extract latent representations from the trained autoencoder.

train(dataset)

Train the autoencoder on the given dataset.