A privacy-by-design federated learning framework for anomaly detection in smart buildings using stacked Long Short-Term Memory (LSTM) networks. This repository implements the FSLSTM model that enables IoT sensors to collaboratively learn for anomaly detection while preserving data privacy through secure multi-party computation.
Keywords: federated learning, anomaly detection, smart buildings, IoT sensors, LSTM, privacy preservation, machine learning, deep learning
Our framework operates on comprehensive smart building infrastructures equipped with diverse IoT sensor networks including:
Our federated stacked LSTM approach achieves state-of-the-art performance compared to centralized and federated baselines:
| Model | Precision | Recall | F1-Score | Balanced Accuracy | MAE | MSE | RMSE |
|---|---|---|---|---|---|---|---|
| FSLSTM (Ours) | 0.89 | 0.79 | 0.87 | 0.90 | 0.162 | 0.19 | 0.435 |
| FGRU | 0.84 | 0.66 | 0.59 | 0.80 | 0.211 | 0.29 | 0.538 |
| FLR | 0.65 | 0.71 | 0.70 | 0.69 | 0.339 | 0.34 | 0.583 |
| LSTM | 0.66 | 0.61 | 0.58 | 0.71 | 0.243 | 0.33 | 0.574 |
| LR | 0.57 | 0.60 | 0.52 | 0.72 | 0.341 | 0.48 | 0.692 |
Our evaluation encompasses 180 IoT sensors across five critical building systems:
Key Performance Highlights:
| Method | Collective Anomalies | Contextual Anomalies | ย | ย |
|---|---|---|---|---|
| ย | Correct (%) | False (%) | Correct (%) | False (%) |
| FSLSTM | 88 | 9 | 90 | 4 |
| FGRU | 74 | 12 | 82 | 7 |
| FLR | 65 | 21 | 78 | 18 |
| LSTM | 66 | 33 | 74 | 29 |
| LR | 56 | 54 | 63 | 48 |
FSLSTM demonstrates remarkable training efficiency:
Scalability Performance Insights:
Privacy-by-Design Implementation:
Significant Communication Overhead Reduction:
Outstanding Regression Performance:
Advanced Anomaly Detection Capabilities:
Our comprehensive evaluation utilizes three real-world datasets from General Electric Current smart building IoT production systems:
Data Processing Pipeline:
Stacked LSTM Configuration:
Federated Learning Process:
Our federated approach significantly outperforms traditional centralized and federated baselines across all evaluation metrics:
๐ Classification Performance Improvements:
๐ Regression Performance Superiority:
Convergence Speed Comparison:
git clone https://github.com/your-username/FSLSTM.git
cd FSLSTM
pip install -e .
pip install fslstm
pip install torch>=1.7.0
pip install numpy>=1.19.0
pip install pandas>=1.2.0
pip install scikit-learn>=0.24.0
pip install matplotlib>=3.3.0
pip install seaborn>=0.11.0
pip install tqdm>=4.60.0
pip install pysyft>=0.5.0
pip install tensorboard>=2.4.0
from fslstm import FSLSTMTrainer, DataLoader
from fslstm.config import Config
# Load configuration for smart building anomaly detection
config = Config.from_file("configs/smart_building.yaml")
# Prepare IoT sensor data for federated learning
data_loader = DataLoader(config)
train_data, test_data = data_loader.load_sensor_data()
# Initialize federated learning trainer
trainer = FSLSTMTrainer(config)
# Train the FSLSTM model using federated approach
trainer.fit(train_data)
# Evaluate anomaly detection performance
results = trainer.evaluate(test_data)
print(f"Balanced Accuracy: {results['balanced_accuracy']:.4f}")
print(f"F1 Score: {results['f1_score']:.4f}")
# Train FSLSTM model for smart building anomaly detection
python scripts/train.py --config configs/smart_building.yaml
# Evaluate trained federated learning model
python scripts/evaluate.py --model_path checkpoints/fslstm_best.pth --data_path data/test/
# Run complete federated learning pipeline
python scripts/run_pipeline.py --config configs/smart_building.yaml
sensor_data/
โโโ sensor_events.csv
โโโ energy_usage.csv
โโโ weather_api.csv
Sensor Events (sensor_events.csv):
timestamp,sensor_id,sensor_type,value,status,zone_id
2019-05-01 08:00:00,S001,occupancy,1,normal,Zone_A
2019-05-01 08:01:00,S002,temperature,22.5,normal,Zone_B
Energy Usage (energy_usage.csv):
timestamp,sensor_id,energy_consumption,appliance_type
2019-05-01 08:00:00,S001,1.25,LED_light
2019-05-01 08:01:00,S002,2.8,HVAC
from fslstm.data import SensorDataProcessor
processor = SensorDataProcessor(
window_size=600, # 10 hours in minutes for IoT sensor data
stride=60, # 1 hour stride for time series analysis
normalize=True
)
# Process raw smart building sensor data
processed_data = processor.process_sensor_logs("data/sensor_events.csv")
configs/smart_building.yaml)# Model Configuration for Federated LSTM
model:
name: "FSLSTM"
lstm_layers: 3
hidden_size: 128
dropout: 0.2
fc_size: 100
# Federated Learning Configuration for IoT Sensors
federated:
num_clients: 180
clients_per_round: 36
num_rounds: 50
local_epochs: 5
batch_size: 1024
# Training Configuration for Smart Building Anomaly Detection
training:
learning_rate: 0.001
optimizer: "adam"
loss_function: "cross_entropy" # or "mse" for regression
device: "cuda"
# Data Configuration for IoT Sensor Networks
data:
window_size: 600
sequence_length: 60
train_split: 0.8
val_split: 0.1
test_split: 0.1
# Sensor Configuration for Smart Buildings
sensors:
categories: ["lights", "thermostat", "occupancy", "water_leakage", "building_access"]
num_sensors: 180
# Privacy Configuration for Federated Learning
privacy:
secure_aggregation: true
differential_privacy: false
from fslstm.config import Config
config = Config()
config.model.lstm_layers = 3
config.model.hidden_size = 256
config.federated.num_clients = 100
config.training.learning_rate = 0.0005
# Save configuration for smart building research
config.save("my_config.yaml")
from fslstm import FSLSTMTrainer, FederatedDataLoader
# Initialize federated data loader for IoT sensors
fed_loader = FederatedDataLoader(
data_path="data/sensor_events.csv",
num_clients=180,
client_split="sensor_type" # Split by sensor type for federated learning
)
# Create federated datasets for smart building sensors
client_datasets = fed_loader.create_client_datasets()
# Initialize federated learning trainer
trainer = FSLSTMTrainer(config)
# Federated training for anomaly detection
trainer.federated_fit(
client_datasets=client_datasets,
num_rounds=50,
clients_per_round=36
)
# For comparison with centralized machine learning approach
from fslstm.baselines import CentralizedLSTM
centralized_model = CentralizedLSTM(config)
centralized_model.fit(train_data)
results = centralized_model.evaluate(test_data)
# Enable logging and visualization for federated learning
from fslstm.utils import TrainingLogger
logger = TrainingLogger(log_dir="logs/fslstm_experiment")
trainer = FSLSTMTrainer(config, logger=logger)
# Training with monitoring for smart building anomaly detection
trainer.fit(train_data, validation_data=val_data)
# View federated learning training curves
logger.plot_training_curves()
logger.plot_convergence_comparison()
from fslstm.evaluation import Evaluator
evaluator = Evaluator(config)
# Load trained federated learning model
model = trainer.load_model("checkpoints/fslstm_best.pth")
# Evaluate on smart building test data
results = evaluator.evaluate(
model=model,
test_data=test_data,
metrics=["accuracy", "precision", "recall", "f1", "auc", "mae", "mse"]
)
print("Anomaly Detection Classification Results:")
print(f" Balanced Accuracy: {results['balanced_accuracy']:.4f}")
print(f" Precision: {results['precision']:.4f}")
print(f" Recall: {results['recall']:.4f}")
print(f" F1-Score: {results['f1_score']:.4f}")
print("Energy Prediction Regression Results:")
print(f" MAE: {results['mae']:.4f}")
print(f" MSE: {results['mse']:.4f}")
print(f" RMSE: {results['rmse']:.4f}")
from fslstm.evaluation import AnomalyDetector
detector = AnomalyDetector(model, threshold=0.5)
# Detect anomalies in real-time IoT sensor data
anomalies = detector.detect_anomalies(sensor_stream)
# Evaluate collective and contextual anomalies in smart buildings
collective_results = detector.evaluate_collective_anomalies(test_data)
contextual_results = detector.evaluate_contextual_anomalies(test_data)
from fslstm.baselines import run_baseline_comparison
# Compare with baseline machine learning methods
baseline_results = run_baseline_comparison(
data=test_data,
methods=["LR", "LSTM", "FLR", "FGRU", "FSLSTM"],
config=config
)
# Generate comparison plots for research evaluation
evaluator.plot_method_comparison(baseline_results)
evaluator.plot_roc_curves(baseline_results)
Our FSLSTM model achieves state-of-the-art performance on smart building anomaly detection:
| Model | Precision | Recall | F1-Score | Balanced Accuracy | MAE | MSE | RMSE |
|---|---|---|---|---|---|---|---|
| LR | 0.57 | 0.60 | 0.52 | 0.72 | 0.341 | 0.48 | 0.692 |
| LSTM | 0.66 | 0.61 | 0.58 | 0.71 | 0.243 | 0.33 | 0.574 |
| FLR | 0.65 | 0.71 | 0.70 | 0.69 | 0.339 | 0.34 | 0.583 |
| FGRU | 0.84 | 0.66 | 0.59 | 0.80 | 0.211 | 0.29 | 0.538 |
| FSLSTM | 0.89 | 0.79 | 0.87 | 0.90 | 0.162 | 0.19 | 0.435 |
from fslstm.visualization import ResultVisualizer
visualizer = ResultVisualizer()
# Plot federated learning training convergence
visualizer.plot_convergence_comparison(trainer.history)
# Plot ROC curves for anomaly detection
visualizer.plot_roc_curves(results)
# Plot smart building energy consumption prediction
visualizer.plot_energy_prediction(predictions, ground_truth)
# Plot real-time anomaly detection timeline
visualizer.plot_anomaly_timeline(anomalies, timestamps)
FSLSTM/
โโโ fslstm/
โ โโโ __init__.py
โ โโโ models/
โ โ โโโ __init__.py
โ โ โโโ fslstm.py # Main FSLSTM model
โ โ โโโ lstm_layers.py # LSTM layer implementations
โ โ โโโ federated_model.py # Federated learning wrapper
โ โโโ data/
โ โ โโโ __init__.py
โ โ โโโ data_loader.py # Data loading utilities
โ โ โโโ preprocessing.py # Data preprocessing
โ โ โโโ federated_data.py # Federated data distribution
โ โโโ training/
โ โ โโโ __init__.py
โ โ โโโ trainer.py # Main training logic
โ โ โโโ federated_trainer.py # Federated training
โ โ โโโ aggregation.py # Federated aggregation algorithms
โ โโโ evaluation/
โ โ โโโ __init__.py
โ โ โโโ evaluator.py # Model evaluation
โ โ โโโ metrics.py # Evaluation metrics
โ โ โโโ anomaly_detection.py # Anomaly detection evaluation
โ โโโ baselines/
โ โ โโโ __init__.py
โ โ โโโ centralized_lstm.py # Centralized LSTM baseline
โ โ โโโ federated_lr.py # Federated Logistic Regression
โ โ โโโ federated_gru.py # Federated GRU
โ โโโ utils/
โ โ โโโ __init__.py
โ โ โโโ config.py # Configuration management
โ โ โโโ logger.py # Logging utilities
โ โ โโโ privacy.py # Privacy mechanisms
โ โโโ visualization/
โ โโโ __init__.py
โ โโโ plots.py # Plotting functions
โ โโโ dashboard.py # Interactive dashboard
โโโ scripts/
โ โโโ train.py # Training script
โ โโโ evaluate.py # Evaluation script
โ โโโ run_pipeline.py # Complete pipeline
โ โโโ preprocess_data.py # Data preprocessing script
โโโ configs/
โ โโโ smart_building.yaml # Default configuration
โ โโโ ablation_study.yaml # Ablation study config
โ โโโ baseline_comparison.yaml # Baseline comparison config
โโโ data/
โ โโโ raw/ # Raw sensor data
โ โโโ processed/ # Processed datasets
โ โโโ examples/ # Example datasets
โโโ notebooks/
โ โโโ 01_data_exploration.ipynb # Data exploration
โ โโโ 02_model_training.ipynb # Model training tutorial
โ โโโ 03_evaluation.ipynb # Evaluation and results
โ โโโ 04_visualization.ipynb # Result visualization
โโโ tests/
โ โโโ test_models.py
โ โโโ test_data.py
โ โโโ test_training.py
โ โโโ test_evaluation.py
โโโ requirements.txt
โโโ setup.py
โโโ README.md
โโโ LICENSE
from fslstm.sensors import SensorInterface
class CustomSensor(SensorInterface):
def __init__(self, sensor_id, sensor_type):
super().__init__(sensor_id, sensor_type)
def read_data(self):
# Custom IoT sensor data reading logic
return sensor_data
def preprocess(self, data):
# Custom preprocessing for smart building data
return processed_data
# Register custom IoT sensor for federated learning
trainer.register_sensor_type("custom_sensor", CustomSensor)
# Configure different tasks for different IoT sensor types
config.tasks = {
"occupancy": {"type": "classification", "classes": 2},
"temperature": {"type": "regression", "target": "energy_consumption"},
"lighting": {"type": "classification", "classes": 2}
}
from fslstm.privacy import DifferentialPrivacy, SecureAggregation
# Enable differential privacy for federated learning
privacy_mechanism = DifferentialPrivacy(epsilon=1.0, delta=1e-5)
trainer.set_privacy_mechanism(privacy_mechanism)
# Enable secure aggregation for IoT sensor networks
secure_agg = SecureAggregation()
trainer.set_aggregation_method(secure_agg)
from fslstm.experiments import AblationStudy
# Run ablation study on number of LSTM layers for federated learning
ablation = AblationStudy(config)
results = ablation.run_layer_ablation(
layers=[1, 2, 3, 4],
dataset=train_data
)
# Analyze results for smart building anomaly detection
ablation.plot_layer_comparison(results)
from fslstm.experiments import ConvergenceAnalysis
# Analyze federated learning convergence with different number of IoT clients
convergence_study = ConvergenceAnalysis(config)
convergence_results = convergence_study.analyze_client_scaling(
client_counts=[20, 40, 80, 160, 200],
dataset=train_data
)
If you use this code in your research, please cite:
@article{fslstm2020,
title={A Federated Learning Approach to Anomaly Detection in Smart Buildings},
journal={ACM Transactions on Internet of Things},
volume={2},
number={4},
pages={1--23},
year={2021},
keywords={federated learning, anomaly detection, smart buildings, IoT sensors, LSTM, privacy preservation}
}
Related Research Publications:
This project is licensed under the MIT License - see the LICENSE.md file for details.
Note: This implementation is based on the federated learning framework for anomaly detection in smart buildings. The model supports both classification tasks (sensor fault detection) and regression tasks (energy consumption prediction) while preserving data privacy through federated learning.