Akshatsingh07/Log-Anomaly-Transformer
GitHub: Akshatsingh07/Log-Anomaly-Transformer
这是一个基于Transformer的深度学习管道,用于在结构化系统日志中实时检测灾难性系统故障。
Stars: 0 | Forks: 0
# 日志异常Transformer 🧠⚙️
一个自定义的深度学习流程,利用Transformer自编码器在高度不平衡(97%正常)的结构化事件日志中检测灾难性系统故障。
本模型不依赖简单的关键词匹配或正则表达式,而是学习系统健康执行的深层上下文语法。当出现不可预测的事件时,模型的交叉熵损失会急剧上升,从而触发自动异常警报。
## 🚀 关键特性
* **自定义Transformer架构:** 使用TensorFlow/Keras从头构建,包含多头注意力机制和层归一化(71,000个参数)。
* **解决“稀释问题”:** 采用激进的 **最大词元策略** 替代标准的序列平均损失,确保在50个词元的序列中,单个灾难性词汇能触发警报,而不会被正常词汇平均掉。
* **动态阈值:** 在保留的正常序列上采用第95百分位数数学校准,而非静态、硬编码的触发阈值。
* **生产环境就绪的推理:** 分离的 `train.py` 和 `inference.py` 脚本支持即时、实时的日志评分,无需重新训练。
## 📊 稀释问题与评估策略
系统日志通常包含大量重复、正常的运行数据。如果一个序列包含49个正常词元和1个致命词元,取交叉熵损失的 `平均值` 会稀释错误,导致崩溃事件逃过检测器。
为了最大化 **召回率** 和 **F1分数**,本流程提取实际序列的三维概率矩阵,并隔离单个最高的非预期词元错误(`最大` 策略)。
### 性能结果
* **精确率:** 1.00(异常类别零误报)
* **阈值校准:** 正态分布的第95百分位数
* **评估指标:** ROC-AUC、F1分数、精确率、召回率
## 🛠️ 技术栈
* **框架:** TensorFlow 2.x, Keras
* **数据处理:** NumPy, Pandas
* **评估:** Scikit-Learn
* **环境:** Python 3.12
## 📂 项目结构
* `data_pipeline.py` - 对原始CSV事件轨迹进行分词、向量化和批处理。
* `transformer_model.py` - 定义自定义的多头注意力架构。
* `train.py` - 主训练循环,保存最佳的 `.keras` 权重。
* `inference.py` - 生产环境脚本,加载模型,应用最大词元策略,计算百分位数阈值,并生成最终分类报告。
## ⚙️ 设置与安装
**1. 克隆仓库:**
```
git clone https://github.com/Akshatsingh07/Log-Anomaly-Transformer
cd Log-Anomaly-Transformer
**2. Create a virtual environment:**
```Bash
python -m venv venv
source venv/bin/activate # On Windows use `venv\Scripts\activate`
**3. Install dependencies:**
```Bash
pip install tensorflow numpy pandas scikit-learn
(Note: The datasets Event_traces.csv and anomaly_label.csv are required to run this project locally but are excluded from this repository due to GitHub file size limits. Ensure you have the datasets in the root directory before running).
💻 Usage
Training the Model:
To train the autoencoder on healthy logs and save the weights/vocabulary:
```Bash
python train.py
Running Inference:
To evaluate the dataset using the dynamic thresholding engine:
```Bash
python inference.py
This will output the final configuration to anomaly_threshold.txt and anomaly_strategy.txt for use in a live production environment.
```
标签:Apex, F1分数, Keras, Max-Token策略, NumPy, Scikit-Learn, TensorFlow, Transformer模型, 交叉熵损失, 动态阈值, 层归一化, 异常检测, 异常警报, 时间序列分析, 机器学习, 注意力机制, 深度学习, 生产就绪, 结构化日志, 自编码器, 逆向工具, 高召回率