Upload 15 files
Browse files- .gitignore +4 -0
- README.md +34 -0
- fine_tune_template.py +5 -0
- main.py +4 -0
- model_card.md +92 -0
- models/model.py +21 -0
- models/txn_anomaly_model.onnx +3 -0
- models/txn_anomaly_model.pt +3 -0
- notebooks/demo.ipynb +1 -0
- notebooks/demo.py +1 -0
- pipeline/main.py +8 -0
- requirements.txt +7 -0
- tests/test_pipeline.py +2 -0
- txn_anomaly_pipeline.py +3 -0
- utils/logger.py +3 -0
.gitignore
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
__pycache__/
|
2 |
+
*.pt
|
3 |
+
*.onnx
|
4 |
+
*.log
|
README.md
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# GRU Sequence Anomaly Detector
|
2 |
+
|
3 |
+
This project provides an open-source, bidirectional GRU-based deep learning model to detect anomalies in time-series transactional data. It is designed to be general-purpose and supports transfer learning.
|
4 |
+
|
5 |
+
## π Project Structure
|
6 |
+
|
7 |
+
- `models/` β Contains the model architecture and trained weights
|
8 |
+
- `pipeline/` β Core training, evaluation, and export logic
|
9 |
+
- `utils/` β Logging and utility functions
|
10 |
+
- `notebooks/` β Example usage and exploration
|
11 |
+
- `tests/` β Unit and integration tests
|
12 |
+
- `main.py` β Entry script to run training/evaluation
|
13 |
+
- `fine_tune_template.py` β Script for model fine-tuning on external datasets
|
14 |
+
- `model_card.md` β Model documentation and expected usage
|
15 |
+
- `requirements.txt` β All required dependencies
|
16 |
+
|
17 |
+
## π¦ Pretrained Models
|
18 |
+
|
19 |
+
- `models/txn_anomaly_model.pt` β PyTorch model file for fine-tuning or loading
|
20 |
+
- `models/txn_anomaly_model.onnx` β ONNX model file for deployment in other runtimes
|
21 |
+
|
22 |
+
## π Quick Start
|
23 |
+
|
24 |
+
```bash
|
25 |
+
pip install -r requirements.txt
|
26 |
+
python main.py
|
27 |
+
```
|
28 |
+
|
29 |
+
To fine-tune:
|
30 |
+
|
31 |
+
```bash
|
32 |
+
python fine_tune_template.py --data your_dataset.csv
|
33 |
+
```
|
34 |
+
|
fine_tune_template.py
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pipeline.main import TxnAnomalyPipeline
|
2 |
+
if __name__ == '__main__':
|
3 |
+
pipe = TxnAnomalyPipeline()
|
4 |
+
pipe.load_model()
|
5 |
+
pipe.fine_tune_on_external()
|
main.py
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pipeline.main import TxnAnomalyPipeline
|
2 |
+
if __name__ == '__main__':
|
3 |
+
pipe = TxnAnomalyPipeline()
|
4 |
+
pipe.run()
|
model_card.md
ADDED
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
license: mit
|
3 |
+
tags:
|
4 |
+
- pytorch
|
5 |
+
- anomaly-detection
|
6 |
+
- time-series
|
7 |
+
- gru
|
8 |
+
- sequence-model
|
9 |
+
- binary-classification
|
10 |
+
model-index:
|
11 |
+
- name: GRU Sequence Anomaly Detector
|
12 |
+
results: []
|
13 |
+
---
|
14 |
+
|
15 |
+
# GRU Sequence Anomaly Detector
|
16 |
+
|
17 |
+
This model uses a bidirectional GRU (Gated Recurrent Unit) architecture to detect anomalies in sequential tabular data β such as transaction records, log events, or sensor readings. It's designed for general-purpose anomaly detection and can be fine-tuned on domain-specific datasets.
|
18 |
+
|
19 |
+
---
|
20 |
+
|
21 |
+
## π§ Model Architecture
|
22 |
+
|
23 |
+
- **Type:** Bidirectional GRU
|
24 |
+
- **Input:** Sequence of numerical feature vectors `(batch_size, time_steps, input_dim)`
|
25 |
+
- **Output:** Binary classification (0 = normal, 1 = anomaly)
|
26 |
+
- **Layers:** 2-layer GRU β BatchNorm β Dense β Sigmoid
|
27 |
+
|
28 |
+
---
|
29 |
+
|
30 |
+
## π οΈ Intended Use
|
31 |
+
|
32 |
+
This model is ideal for:
|
33 |
+
- Transaction anomaly detection
|
34 |
+
- Time-series pattern disruption
|
35 |
+
- Sequential event log monitoring
|
36 |
+
|
37 |
+
It is **open for fine-tuning** using your labeled anomaly dataset via `fine_tune_template.py`.
|
38 |
+
|
39 |
+
---
|
40 |
+
|
41 |
+
## π How to Use
|
42 |
+
|
43 |
+
```python
|
44 |
+
import torch
|
45 |
+
from models.model import TxnAnomalyGRU
|
46 |
+
|
47 |
+
model = TxnAnomalyGRU(input_dim=32)
|
48 |
+
model.load_state_dict(torch.load("models/txn_anomaly_model.pt"))
|
49 |
+
model.eval()
|
50 |
+
```
|
51 |
+
|
52 |
+
Or use the ONNX version with ONNX Runtime:
|
53 |
+
|
54 |
+
```python
|
55 |
+
import onnxruntime
|
56 |
+
session = onnxruntime.InferenceSession("models/txn_anomaly_model.onnx")
|
57 |
+
outputs = session.run(None, {"input": your_input_array})
|
58 |
+
```
|
59 |
+
|
60 |
+
---
|
61 |
+
|
62 |
+
## π Fine-Tuning
|
63 |
+
|
64 |
+
To fine-tune on your own dataset:
|
65 |
+
|
66 |
+
```bash
|
67 |
+
python fine_tune_template.py --data your_dataset.csv
|
68 |
+
```
|
69 |
+
|
70 |
+
Ensure your data is preprocessed into sequences of the same input dimension (`input_dim=32` by default).
|
71 |
+
|
72 |
+
---
|
73 |
+
|
74 |
+
## π¦ Files Included
|
75 |
+
|
76 |
+
- `models/txn_anomaly_model.pt` β Pretrained PyTorch model
|
77 |
+
- `models/txn_anomaly_model.onnx` β ONNX export
|
78 |
+
- `fine_tune_template.py` β Script to fine-tune on your dataset
|
79 |
+
- `pipeline/main.py` β End-to-end pipeline
|
80 |
+
|
81 |
+
---
|
82 |
+
|
83 |
+
## π License
|
84 |
+
|
85 |
+
This model is released under the **MIT License**. You are free to use, modify, and distribute it for research or commercial purposes.
|
86 |
+
|
87 |
+
---
|
88 |
+
|
89 |
+
## π€ Author
|
90 |
+
|
91 |
+
Developed by [Your Name]
|
92 |
+
For contributions, bug reports, or questions, please use GitHub Issues.
|
models/model.py
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torch.nn as nn
|
3 |
+
class TxnAnomalyGRU(nn.Module):
|
4 |
+
def __init__(self, input_dim=32, hidden_dim=128, num_layers=2, dropout=0.3):
|
5 |
+
super().__init__()
|
6 |
+
self.gru = nn.GRU(input_dim, hidden_dim, num_layers=num_layers, bidirectional=True, batch_first=True, dropout=dropout)
|
7 |
+
self.batchnorm = nn.BatchNorm1d(hidden_dim * 2)
|
8 |
+
self.dropout = nn.Dropout(dropout)
|
9 |
+
self.fc1 = nn.Linear(hidden_dim * 2, 64)
|
10 |
+
self.relu = nn.ReLU()
|
11 |
+
self.out = nn.Linear(64, 1)
|
12 |
+
self.sigmoid = nn.Sigmoid()
|
13 |
+
def forward(self, x):
|
14 |
+
h0 = torch.zeros(self.gru.num_layers * 2, x.size(0), self.gru.hidden_size).to(x.device)
|
15 |
+
out, _ = self.gru(x, h0)
|
16 |
+
out = out[:, -1, :]
|
17 |
+
out = self.batchnorm(out)
|
18 |
+
out = self.dropout(out)
|
19 |
+
out = self.relu(self.fc1(out))
|
20 |
+
out = self.sigmoid(self.out(out))
|
21 |
+
return out
|
models/txn_anomaly_model.onnx
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
|
3 |
+
size 0
|
models/txn_anomaly_model.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
|
3 |
+
size 0
|
notebooks/demo.ipynb
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
# demo notebook
|
notebooks/demo.py
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
# Demo usage for inference or loading pretrained model
|
pipeline/main.py
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Pipeline placeholder
|
2 |
+
class TxnAnomalyPipeline:
|
3 |
+
def run(self):
|
4 |
+
print('Pipeline running')
|
5 |
+
def load_model(self):
|
6 |
+
print('Loading pretrained model')
|
7 |
+
def fine_tune_on_external(self):
|
8 |
+
print('Fine tuning on external data')
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
scikit-learn
|
3 |
+
onnx
|
4 |
+
onnxruntime
|
5 |
+
numpy
|
6 |
+
pandas
|
7 |
+
matplotlib
|
tests/test_pipeline.py
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
def test_dummy():
|
2 |
+
assert True
|
txn_anomaly_pipeline.py
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
from pipeline.main import TxnAnomalyPipeline
|
2 |
+
pipe = TxnAnomalyPipeline()
|
3 |
+
pipe.run()
|
utils/logger.py
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
import logging
|
2 |
+
logging.basicConfig(level=logging.INFO, filename='run.log', format='%(asctime)s %(levelname)s:%(message)s')
|
3 |
+
logger = logging.getLogger(__name__)
|