Downloads · 30 days
0
jamal-ibrahim/distilbert-hybrid-fraud-detector-v2
distilbert-hybrid-fraud-detector-v2 is a text classification model from jamal-ibrahim. Use it when you need a label for a piece of text. The card lists the license as apache-2.0.
This repository contains a Hybrid DistilBERT model for binary classification of email messages:
Downloads · 30 days
0
Access
Public
Updated Mar 1, 2026
Repo size
266 MB
Likes
0
Public
Click a slice to open those files.
.pt266 MB · 100%
From the Hugging Face model README
This repository contains a Hybrid DistilBERT model for binary classification of email messages:
This model represents Phase 2 of a multi-stage fraud detection system. Unlike the Phase 1 text-only baseline, this architecture fuses semantic embeddings from a pre-trained transformer with engineered linguistic risk signals (urgency, financial triggers, formatting anomalies, structural indicators).
The objective of this phase is to increase model robustness and improve sensitivity (Recall) against polished phishing attacks that may bypass purely lexical checks.
Phase 1 relied solely on distilbert-base-uncased to classify text. While effective, it exhibited two primary weaknesses:
Phase 2 introduces a Hybrid Architecture (Dual-Branch Network):
This architecture validates not only what is said (semantic meaning) but how it is structured (risk profile), aligning with a defense-in-depth strategy common in cybersecurity systems.
Training was conducted on the Enron Spam Dataset to ensure direct comparability with Phase 1.
In addition to raw text, six engineered numeric features were extracted and normalized (StandardScaler):
Data split strategy remains stratified (70% train / 15% validation / 15% test).
Text Branch:
distilbert-base-uncasedDense Branch:
Fusion Layer:
Classifier:
Differential Learning Rates:
Optimizer:
Batch Size:
Loss Function:
Early Stopping:
Evaluation was performed on the held-out test set and directly compared to Phase 1.
| Metric | Phase 1 (Text-Only) | Phase 2 (Hybrid) | Change |
|---|---|---|---|
| Recall | 0.9635 | 0.9863 | +2.28% |
| Precision | 0.9769 | 0.9114 | -6.55% |
| F1 Score | 0.9701 | 0.9474 | -2.27% |
| ROC-AUC | 0.9972 | 0.9959 | -0.13% |
The Hybrid model achieved the primary objective of increasing sensitivity. It detects significantly more fraud cases (Recall ~99%), acting as a tighter safety net.
However, explicit risk features introduced structural bias, leading to more benign emails being flagged as fraud (lower Precision).
This trade-off reflects a deliberate design choice: prioritizing threat detection over convenience.
Manual inspection revealed:
Conclusion:
The Hybrid architecture trades user convenience (higher False Positives) for increased security (lower False Negatives), a common and often desirable trade-off in high-security environments.
Length Robustness:
Feature Contribution:
This model is intended for:
Deployment Note:
Unlike standard transformer models, this architecture requires preprocessing to compute the six numeric features prior to inference.
import torch
import torch.nn as nn
from transformers import AutoModel
Define Architecture
class HybridDistilBERT(nn.Module):
def __init__(self, num_labels=2, dense_feature_dim=6):
super().__init__()
self.distilbert = AutoModel.from_pretrained("distilbert-base-uncased")
self.dense_mlp = nn.Sequential(
nn.Linear(dense_feature_dim, 16),
nn.ReLU(),
nn.Dropout(0.2)
)
self.classifier = nn.Sequential(
nn.Linear(768 + 16, 64),
nn.ReLU(),
nn.Dropout(0.2),
nn.Linear(64, num_labels)
)
def forward(self, input_ids, attention_mask, dense_features):
outputs = self.distilbert(input_ids=input_ids, attention_mask=attention_mask)
cls_embedding = outputs.last_hidden_state[:, 0, :]
dense_embed = self.dense_mlp(dense_features)
combined = torch.cat((cls_embedding, dense_embed), dim=1)
return self.classifier(combined)
model = HybridDistilBERT()
model.load_state_dict(torch.load("best_hybrid_model.pt"))
model.eval()
Load Model
model = HybridDistilBERT()
model.load_state_dict(torch.load("best_hybrid_model.pt"))
model.eval()
Inference (Requires Feature Extraction)
text = "URGENT: Verify your bank account now!!"
# 1. Tokenize text
# 2. Extract six dense features
# 3. Pass both into the model
with torch.no_grad():
outputs = model(input_ids, attention_mask, dense_features)
probabilities = torch.softmax(outputs, dim=1)
Phase 3 (Next Steps):
Adversarial Training with obfuscated and paraphrased fraud examples.
Feature Ablation Studies to reduce false positives.
Precision-recall trade-off tuning through threshold optimization.
Temporal drift simulation for robustness against evolving attack patterns.
If you use this model in research, please cite:
DistilBERT Sanh et al., 2019. DistilBERT: a distilled version of BERT.
Enron Dataset Klimt & Yang, 2004. The Enron Corpus.
This version:
If you want, we can now tighten it further into a more research-paper tone or a more product