Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 

Repository files navigation

AutoEncoder for Anomaly Detection

Autoencoder-based example in PyTorch for detecting anomalies (seizures) from tabular heart rate features in CSVs. The included notebook (main.ipynb) demonstrates data preparation, training an autoencoder, saving a checkpoint, and evaluating detection performance with reconstruction error and ROC/AUC.

Notebook

Open main.ipynb and run the cells. The notebook covers:

  • loading and concatenating CSV feature files
  • feature standardization and conversion to PyTorch tensors
  • DataLoader setup and a simple PyTorch AutoEncoder model
  • training loop, evaluation, ROC curve and AUC calculation
  • saving a model checkpoint

Data format

CSV files should contain feature columns and a seizure column with integer labels:

  • 0 → normal (used for training)
  • 1 → seizure (anomaly)
  • 2 or 3 → artifact / disagreed (removed by the notebook)

Update the file-glob paths in the notebook before running (examples used in the notebook): path/train/*.csv, path/validation/*.csv, path/test/*.csv.

Model (as implemented)

  • Encoder: Linear(16 → 10) → Sigmoid → Linear(10 → 4) → Sigmoid → Dropout(0.05)
  • Decoder: Linear(4 → 10) → Sigmoid → Linear(10 → 16) → Sigmoid
  • Loss: MSE between input and reconstruction
  • Weight init: Xavier/Glorot for Linear layers

Adjust input_size in the notebook if your feature vector length differs from 16.

Quick run

  1. Install dependencies (minimal): pip install jupyter pandas numpy scikit-learn matplotlib torch
  2. Start Jupyter and open main.ipynb: jupyter lab

Or execute the notebook headless: jupyter nbconvert --to notebook --execute main.ipynb --output executed.ipynb

Training & evaluation notes

  • Batch size: 256
  • Optimizer: Adam (lr in notebook: 0.01)
  • Epochs: 10 (example)
  • Evaluation: per-sample MSE reconstruction error; ROC curve and AUC vs. true labels
  • Example AUC shown in the notebook: ~0.6078

Improvements & next steps

  • Tune architecture, activations, and learning schedule
  • Try threshold selection from training error distribution or use a VAE for probabilistic scores
  • Add preprocessing scripts, a requirements.txt, or a train.py to run the pipeline outside the notebook

Files

  • main.ipynb — full pipeline
  • README.md — this file

About

Anomaly detection using AutoEncoder

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages