Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -271,10 +271,14 @@ def lstm_anomaly_detection(X, feature_columns, num_anomalies=10, epochs=100, bat
|
|
| 271 |
|
| 272 |
X = torch.FloatTensor(X).to(device)
|
| 273 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 274 |
train_size = int(0.85 * len(X))
|
| 275 |
X_train, X_val = X[:train_size], X[train_size:]
|
| 276 |
|
| 277 |
-
model = LSTMAutoencoder(input_size=
|
| 278 |
criterion = nn.MSELoss()
|
| 279 |
optimizer = optim.Adam(model.parameters())
|
| 280 |
|
|
@@ -295,7 +299,7 @@ def lstm_anomaly_detection(X, feature_columns, num_anomalies=10, epochs=100, bat
|
|
| 295 |
with torch.no_grad():
|
| 296 |
reconstructed = model(X.unsqueeze(0)).squeeze(0).cpu().numpy()
|
| 297 |
|
| 298 |
-
# Compute anomalies for all features
|
| 299 |
mse_all = np.mean(np.power(X.cpu().numpy() - reconstructed, 2), axis=1)
|
| 300 |
top_indices_all = mse_all.argsort()[-num_anomalies:][::-1]
|
| 301 |
anomalies_all = np.zeros(len(mse_all), dtype=bool)
|
|
@@ -304,7 +308,12 @@ def lstm_anomaly_detection(X, feature_columns, num_anomalies=10, epochs=100, bat
|
|
| 304 |
# Compute anomalies for components only
|
| 305 |
component_columns = [col for col in feature_columns if col.startswith('Comp')]
|
| 306 |
component_indices = [feature_columns.index(col) for col in component_columns]
|
| 307 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 308 |
top_indices_comp = mse_comp.argsort()[-num_anomalies:][::-1]
|
| 309 |
anomalies_comp = np.zeros(len(mse_comp), dtype=bool)
|
| 310 |
anomalies_comp[top_indices_comp] = True
|
|
|
|
| 271 |
|
| 272 |
X = torch.FloatTensor(X).to(device)
|
| 273 |
|
| 274 |
+
# Ensure X is 2D
|
| 275 |
+
if X.dim() == 1:
|
| 276 |
+
X = X.unsqueeze(0)
|
| 277 |
+
|
| 278 |
train_size = int(0.85 * len(X))
|
| 279 |
X_train, X_val = X[:train_size], X[train_size:]
|
| 280 |
|
| 281 |
+
model = LSTMAutoencoder(input_size=X.shape[1]).to(device)
|
| 282 |
criterion = nn.MSELoss()
|
| 283 |
optimizer = optim.Adam(model.parameters())
|
| 284 |
|
|
|
|
| 299 |
with torch.no_grad():
|
| 300 |
reconstructed = model(X.unsqueeze(0)).squeeze(0).cpu().numpy()
|
| 301 |
|
| 302 |
+
# Compute anomalies for all features
|
| 303 |
mse_all = np.mean(np.power(X.cpu().numpy() - reconstructed, 2), axis=1)
|
| 304 |
top_indices_all = mse_all.argsort()[-num_anomalies:][::-1]
|
| 305 |
anomalies_all = np.zeros(len(mse_all), dtype=bool)
|
|
|
|
| 308 |
# Compute anomalies for components only
|
| 309 |
component_columns = [col for col in feature_columns if col.startswith('Comp')]
|
| 310 |
component_indices = [feature_columns.index(col) for col in component_columns]
|
| 311 |
+
|
| 312 |
+
if len(component_indices) > 0:
|
| 313 |
+
mse_comp = np.mean(np.power(X.cpu().numpy()[:, component_indices] - reconstructed[:, component_indices], 2), axis=1)
|
| 314 |
+
else:
|
| 315 |
+
mse_comp = mse_all # If no components, use all features
|
| 316 |
+
|
| 317 |
top_indices_comp = mse_comp.argsort()[-num_anomalies:][::-1]
|
| 318 |
anomalies_comp = np.zeros(len(mse_comp), dtype=bool)
|
| 319 |
anomalies_comp[top_indices_comp] = True
|