Back to Blog
Machine Learning December 12, 2024 8 min read

Anomaly Detection with Autoencoders: Better Than Rules, Cheaper Than Labels

Using autoencoders for unsupervised anomaly detection — reconstruction error thresholding, LSTM autoencoders for time series, and production deployment.

Why Autoencoders for Anomaly Detection

You only need normal data to train. The autoencoder learns to reconstruct normal patterns well. Anomalies have high reconstruction error.

Architecture

class Autoencoder(nn.Module):
    def __init__(self, input_dim, latent_dim=32):
        super().__init__()
        self.encoder = nn.Sequential(
            nn.Linear(input_dim, 128), nn.ReLU(),
            nn.Linear(128, 64), nn.ReLU(),
            nn.Linear(64, latent_dim)
        )
        self.decoder = nn.Sequential(
            nn.Linear(latent_dim, 64), nn.ReLU(),
            nn.Linear(64, 128), nn.ReLU(),
            nn.Linear(128, input_dim)
        )
    
    def forward(self, x):
        z = self.encoder(x)
        return self.decoder(z)

Threshold Setting

# Set threshold at 99th percentile of train reconstruction error
recon_errors = ((X_train - model(X_train)) ** 2).mean(dim=1)
threshold = np.percentile(recon_errors.numpy(), 99)
Anomaly DetectionAutoencoderUnsupervisedPyTorchFraud
O

Ossama Elhakki

AI Engineer & ML Systems Builder — Morocco