Skip to main content

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

License

MIT

Release files for qriton-hopfield-anomaly 4.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for qriton-hopfield-anomaly 4.0.1
File Size Uploaded
qriton_hopfield_anomaly-4.0.1.tar.gz 11.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for qriton-hopfield-anomaly 4.0.1
File Interpreter ABI Platform
qriton_hopfield_anomaly-4.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 22.9 kB

Release files / qriton_hopfield_anomaly-4.0.1.tar.gz

Download URL qriton_hopfield_anomaly-4.0.1.tar.gz
Size 11.2 kB
Tags Source
SHA-256 checksum
How to use checksums
128cbaebe41743597b3665730db01a81a5b1c7e9ee53690f94e43f279a92b33f
BLAKE2b-256 checksum
How to use checksums
c741058f694ee02648be02ef3ebc176c9b5f48a92c0f4327d859d65ff0a94401
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.10

Release files / qriton_hopfield_anomaly-4.0.1-py3-none-any.whl

Download URL qriton_hopfield_anomaly-4.0.1-py3-none-any.whl
Size 11.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7698784d069612f41e528011ee6064527e249ca99bfd955e96ef5440b83a3c14
BLAKE2b-256 checksum
How to use checksums
8badda95fd8f77058be0c75c6edcb42b23627d04586d6445b513601552b74040
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.10

Release history Release notifications | RSS feed

This release

4.0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page