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)