📉 Learning Without Labels: The Architecture of Compression and Reconstruction
In many real-world enterprise environments, acquiring massive labeled datasets for supervised machine learning is prohibitively expensive or impossible. Autoencoders solve this through unsupervised learning by training neural networks to perform a seemingly simple task: copy their input to their output ($x \approx \hat{x}$). By forcing data through a constrained, low-dimensional bottleneck layer known as the Latent Space, the network is compelled to discover non-linear correlations, discard noise, and learn compact, information-dense feature representations that power downstream anomaly detection, dimensionality reduction, and generative modeling.
import torch
import torch.nn as nn
import torch.optim as optim
class FraudAutoencoder(nn.Module):
def __init__(self, input_dim: int = 30):
super(FraudAutoencoder, self).__init__()
# Encoder: Compresses 30 financial features down to 8-dim latent space
self.encoder = nn.Sequential(
nn.Linear(input_dim, 16),
nn.ReLU(),
nn.Linear(16, 8)
)
# Decoder: Reconstructs 30 features from 8-dim latent bottleneck
self.decoder = nn.Sequential(
nn.Linear(8, 16),
nn.ReLU(),
nn.Linear(16, input_dim)
)
def forward(self, x):
latent = self.encoder(x)
reconstruction = self.decoder(latent)
return reconstruction
# Training on normal transactions only
model = FraudAutoencoder()
criterion = nn.MSELoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
# Anomaly Scoring: Transactions with reconstruction error > threshold are flagged as fraudulent
def detect_fraud(model, transaction_vector, threshold=0.05):
model.eval()
with torch.no_grad():
recon = model(transaction_vector)
loss = criterion(recon, transaction_vector).item()
return loss > threshold, loss
class ConvDenoisingAutoencoder(nn.Module):
def __init__(self):
super().__init__()
# Encoder
self.enc = nn.Sequential(
nn.Conv2d(1, 32, 3, stride=2, padding=1),
nn.ReLU(),
nn.Conv2d(32, 64, 3, stride=2, padding=1),
nn.ReLU()
)
# Decoder
self.dec = nn.Sequential(
nn.ConvTranspose2d(64, 32, 3, stride=2, padding=1, output_padding=1),
nn.ReLU(),
nn.ConvTranspose2d(32, 1, 3, stride=2, padding=1, output_padding=1),
nn.Sigmoid()
)
def forward(self, x):
return self.dec(self.enc(x))
class LSTMAutoencoder(nn.Module):
def __init__(self, seq_len: int, n_features: int, embedding_dim: int = 16):
super().__init__()
self.encoder = nn.LSTM(n_features, embedding_dim, batch_first=True)
self.decoder = nn.LSTM(embedding_dim, n_features, batch_first=True)
def forward(self, x):
_, (hidden, _) = self.encoder(x)
# Repeat hidden state across sequence length for decoding
decoded_input = hidden.repeat(x.size(1), 1, 1).transpose(0, 1)
out, _ = self.decoder(decoded_input)
return out
# Encoder architecture: 20000 -> 1024 -> 128 -> 2 (Latent 2D coordinates)
# Unlike PCA which only captures linear variance, deep autoencoders capture complex non-linear biological manifolds.
class BinaryHashingAutoencoder(nn.Module):
def __init__(self, input_dim: int, hash_bits: int = 64):
super().__init__()
self.encoder = nn.Sequential(nn.Linear(input_dim, hash_bits), nn.Sigmoid())
self.decoder = nn.Linear(hash_bits, input_dim)
def forward(self, x):
latent_bits = self.encoder(x)
reconstruction = self.decoder(latent_bits)
return reconstruction, latent_bits
class VAE(nn.Module):
def __init__(self, input_dim, latent_dim):
super().__init__()
self.fc_mu = nn.Linear(input_dim, latent_dim)
self.fc_var = nn.Linear(input_dim, latent_dim)
# Decoder maps latent sample back to input space
def reparameterize(self, mu, log_var):
std = torch.exp(0.5 * log_var)
eps = torch.randn_like(std)
return mu + eps * std # Enables backpropagation through stochastic nodes
# Continuously ingests 5-tuple packet flows (src_ip, dst_ip, port, protocol, byte_count).
# Flags packets exceeding dynamic reconstruction loss thresholds as active botnet C2 beacons.
# Input: 512-dim ArcFace embedding -> Bottleneck: 64-dim compressed vector -> Output: Reconstructed 512-dim embedding.
# Reduces enterprise biometric database storage overhead by 87%.
# Ingests noisy microphone audio frames -> Latent bottleneck preserves vocal frequencies -> Decoder outputs crystal clear speech.
# Tracks centroid shifts in the autoencoder latent space over successive training epochs to alert engineers of dataset skew.