Views
No views yet
num_layers=2), mapping the input sequence down to a fixed-size latent_dimension (32). The latent vector captures the compressed "essence" of the sequence's normal pattern.anomaly_threshold (e.g., 0.05 MSE). Choosing this threshold is subjective and requires careful validation, as a low threshold causes many false alarms (false positives), and a high one misses real crises (false negatives).1import torch
2import torch.nn as nn
3import numpy as np
4
5# Model parameters (matches config.json)
6INPUT_SIZE = 4
7SEQ_LENGTH = 30
8ENCODER_HIDDEN = 64
9LATENT_DIM = 32
10
11# Conceptual Model Class (not runnable without full implementation)
12class LSTMAutoencoder(nn.Module):
13 # (Encoder and Decoder setup omitted for brevity)
14 def forward(self, x):
15 # x is (Batch, SEQ_LENGTH, INPUT_SIZE)
16 # encoder_output = self.encoder(x)
17 # reconstructed_output = self.decoder(encoder_output)
18 # return reconstructed_output
19 return torch.randn_like(x) # Placeholder for reconstruction
20
21# Load weights and instantiate (Conceptual)
22# model = LSTMAutoencoder(...)
23# model.load_state_dict(torch.load("pytorch_model.bin"))
24# model.eval()
25
26# Dummy input data: 30 days of the 4 features
27# In a real scenario, this would be standardized/normalized.
28current_window = torch.randn(1, SEQ_LENGTH, INPUT_SIZE)
29
30# with torch.no_grad():
31# reconstruction = model(current_window)
32
33# Calculate Anomaly Score (Reconstruction Error)
34mse_loss = nn.MSELoss(reduction='mean')
35anomaly_score = mse_loss(current_window, reconstruction).item()
36
37# ANOMALY_THRESHOLD = 0.05 (from config.json)
38# if anomaly_score > ANOMALY_THRESHOLD:
39# print(f"ANOMALY DETECTED! Score: {anomaly_score:.4f}")
40# else:
41# print(f"Normal. Score: {anomaly_score:.4f}")