Production-ready Binary and Continuous (Modern) Hopfield Neural Networks for anomaly detection and cross-domain discovery
Project description
Qriton Hopfield Anomaly Detection
Production-ready Binary and Continuous (Modern) Hopfield Neural Networks for real-time anomaly detection in Python. A pure-Python port of the @qriton/hopfield-anomaly npm package (v4.0.1), providing adaptive thresholds, gradient-based attribution, and cross-domain pattern discovery with zero external dependencies.
Installation
pip install qriton-hopfield-anomaly
Quick Start
Binary Hopfield (Hard Anomaly Detection)
from hopfield_anomaly import HopfieldAnomalyDetector
detector = HopfieldAnomalyDetector(
feature_count=3,
snapshot_length=5,
anomaly_threshold=0.3,
learning_rule="storkey",
)
detector.set_thresholds({
"temperature": {"mode": "range", "min": 60, "max": 80},
"pressure": {"mode": "range", "min": 95, "max": 105},
"vibration": {"mode": "below", "value": 50},
})
detector.train_with_defaults()
for _ in range(5):
detector.add_data_point({"temperature": 70, "pressure": 100, "vibration": 20})
result = detector.detect()
print(result.is_anomaly) # False
print(result.anomaly_score) # 0.245
print(result.feature_impact[0]) # {"name": "temperature", "energy_delta": -0.123}
Continuous Hopfield (Soft Anomaly Detection)
from hopfield_anomaly import ContinuousHopfieldAnomalyDetector
detector = ContinuousHopfieldAnomalyDetector(
feature_count=3,
snapshot_length=5,
energy_type="logsumexp",
beta=1.0,
)
detector.set_features(
["temperature", "pressure", "vibration"],
{
"temperature": {"min": 0, "max": 100},
"pressure": {"min": 0, "max": 200},
"vibration": {"min": 0, "max": 50},
},
)
detector.train_with_defaults()
for _ in range(5):
detector.add_data_point({"temperature": 70.5, "pressure": 105.2, "vibration": 12.3})
result = detector.detect()
print(result.is_anomaly) # False
print(result.anomaly_score) # 0.182
print(result.composition[0]) # {"pattern_index": 0, "similarity": 0.95, "percentage": 72}
print(result.feature_gradients[0]) # {"name": "pressure", "gradient_norm": 0.234}
High-Level Monitor APIs
AnomalyMonitor (Binary)
from hopfield_anomaly import AnomalyMonitor
monitor = AnomalyMonitor(feature_count=3)
monitor.set_thresholds({
"temp": {"mode": "range", "min": 60, "max": 80},
"pressure": {"mode": "range", "min": 95, "max": 105},
"vibration": {"mode": "below", "value": 50},
})
monitor.train_with_defaults()
result = monitor.process({"temp": 70, "pressure": 100, "vibration": 20})
if result and result.is_anomaly:
print(f"Anomaly detected: {result.anomaly_score}")
ContinuousAnomalyMonitor (Cross-Domain Discovery)
from hopfield_anomaly import ContinuousAnomalyMonitor
monitor = ContinuousAnomalyMonitor(feature_count=3)
monitor.set_features(
["temp", "pressure", "vibration"],
{
"temp": {"min": 0, "max": 100},
"pressure": {"min": 0, "max": 200},
"vibration": {"min": 0, "max": 50},
},
)
monitor.train_with_defaults()
result = monitor.process({"temp": 70.5, "pressure": 105.2, "vibration": 12.3})
if result and result.is_anomaly:
print(f"Anomaly score: {result.anomaly_score}")
for comp in result.composition:
print(f" Pattern {comp['pattern_index']}: {comp['percentage']}% match")
API Reference
| Class | Description |
|---|---|
HopfieldNetwork |
Binary Hopfield network with Hebbian and Storkey learning rules, asynchronous recall, and energy computation. |
ContinuousHopfieldNetwork |
Modern Hopfield network with continuous states, LogSumExp/quadratic energy, gradient descent dynamics, and attention weights. |
AdaptiveThreshold |
Auto-tuning anomaly threshold using 95th-percentile tracking (unsupervised) or labeled feedback (supervised). |
HopfieldAnomalyDetector |
Binary anomaly detector with threshold-based binarization, Z-score scoring, and gradient-based feature attribution. |
ContinuousHopfieldAnomalyDetector |
Continuous anomaly detector with feature normalization, pattern composition analysis, and gradient-based discovery. |
AnomalyMonitor |
High-level wrapper around HopfieldAnomalyDetector with a single process() call for streaming data. |
ContinuousAnomalyMonitor |
High-level wrapper around ContinuousHopfieldAnomalyDetector with cross-domain event detection. |
Requirements
- Python >= 3.10
- No external dependencies
Links
- npm package: @qriton/hopfield-anomaly
- GitHub: github.com/qriton/hopfield-anomaly
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file qriton_hopfield_anomaly-4.0.1.tar.gz.
File metadata
- Download URL: qriton_hopfield_anomaly-4.0.1.tar.gz
- Upload date:
- Size: 11.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
128cbaebe41743597b3665730db01a81a5b1c7e9ee53690f94e43f279a92b33f
|
|
| MD5 |
d26c662ad6e5615e4e98eded757e6519
|
|
| BLAKE2b-256 |
c741058f694ee02648be02ef3ebc176c9b5f48a92c0f4327d859d65ff0a94401
|
File details
Details for the file qriton_hopfield_anomaly-4.0.1-py3-none-any.whl.
File metadata
- Download URL: qriton_hopfield_anomaly-4.0.1-py3-none-any.whl
- Upload date:
- Size: 11.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7698784d069612f41e528011ee6064527e249ca99bfd955e96ef5440b83a3c14
|
|
| MD5 |
f183477fa72dc641e924d6d7302d052d
|
|
| BLAKE2b-256 |
8badda95fd8f77058be0c75c6edcb42b23627d04586d6445b513601552b74040
|