File size: 5,543 Bytes
d187b57 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# Toxic Comment Classification using Deep Learning
A multilingual toxic comment classification system using language-aware transformers and advanced deep learning techniques.
## ποΈ Architecture Overview
### Core Components
1. **LanguageAwareTransformer**
- Base: XLM-RoBERTa Large
- Custom language-aware attention mechanism
- Gating mechanism for feature fusion
- Language-specific dropout rates
- Support for 7 languages with English fallback
2. **ToxicDataset**
- Efficient caching system
- Language ID mapping
- Memory pinning for CUDA optimization
- Automatic handling of missing values
3. **Training System**
- Mixed precision training (BF16/FP16)
- Gradient accumulation
- Language-aware loss weighting
- Distributed training support
- Automatic threshold optimization
### Key Features
- **Language Awareness**
- Language-specific embeddings
- Dynamic dropout rates per language
- Language-aware attention mechanism
- Automatic fallback to English for unsupported languages
- **Performance Optimization**
- Gradient checkpointing
- Memory-efficient attention
- Automatic mixed precision
- Caching system for processed data
- CUDA optimization with memory pinning
- **Training Features**
- Weighted focal loss with language awareness
- Dynamic threshold optimization
- Early stopping with patience
- Gradient flow monitoring
- Comprehensive metric tracking
## π Data Processing
### Input Format
```python
{
'comment_text': str, # The text to classify
'lang': str, # Language code (en, ru, tr, es, fr, it, pt)
'toxic': int, # Binary labels for each category
'severe_toxic': int,
'obscene': int,
'threat': int,
'insult': int,
'identity_hate': int
}
```
### Language Support
- Primary: en, ru, tr, es, fr, it, pt
- Default fallback: en (English)
- Language ID mapping: {en: 0, ru: 1, tr: 2, es: 3, fr: 4, it: 5, pt: 6}
## π Model Architecture
### Base Model
- XLM-RoBERTa Large
- Hidden size: 1024
- Attention heads: 16
- Max sequence length: 128
### Custom Components
1. **Language-Aware Classifier**
```python
- Input: Hidden states [batch_size, hidden_size]
- Language embeddings: [batch_size, 64]
- Projection: hidden_size + 64 -> 512
- Output: 6 toxicity predictions
```
2. **Language-Aware Attention**
```python
- Input: Hidden states + Language embeddings
- Scaled dot product attention
- Gating mechanism for feature fusion
- Memory-efficient implementation
```
## π οΈ Training Configuration
### Hyperparameters
```python
{
"batch_size": 32,
"grad_accum_steps": 2,
"epochs": 4,
"lr": 2e-5,
"weight_decay": 0.01,
"warmup_ratio": 0.1,
"label_smoothing": 0.01,
"model_dropout": 0.1,
"freeze_layers": 2
}
```
### Optimization
- Optimizer: AdamW
- Learning rate scheduler: Cosine with warmup
- Mixed precision: BF16/FP16
- Gradient clipping: 1.0
- Gradient accumulation steps: 2
## π Metrics and Monitoring
### Training Metrics
- Loss (per language)
- AUC-ROC (macro)
- Precision, Recall, F1
- Language-specific metrics
- Gradient norms
- Memory usage
### Validation Metrics
- AUC-ROC (per class and language)
- Optimal thresholds per language
- Critical class performance (threat, identity_hate)
- Distribution shift monitoring
## π§ Usage
### Training
```bash
python model/train.py
```
### Inference
```python
from model.predict import predict_toxicity
results = predict_toxicity(
text="Your text here",
model=model,
tokenizer=tokenizer,
config=config
)
```
## π Code Structure
```
model/
βββ language_aware_transformer.py # Core model architecture
βββ train.py # Training loop and utilities
βββ predict.py # Inference utilities
βββ evaluation/
β βββ evaluate.py # Evaluation functions
β βββ threshold_optimizer.py # Dynamic threshold optimization
βββ data/
β βββ sampler.py # Custom sampling strategies
βββ training_config.py # Configuration management
```
## π€ AI/ML Specific Notes
1. **Tensor Shapes**
- Input IDs: [batch_size, seq_len]
- Attention Mask: [batch_size, seq_len]
- Language IDs: [batch_size]
- Hidden States: [batch_size, seq_len, hidden_size]
- Language Embeddings: [batch_size, embed_dim]
2. **Critical Components**
- Language ID handling in forward pass
- Attention mask shape management
- Memory-efficient attention implementation
- Gradient flow in language-aware components
3. **Performance Considerations**
- Cache management for processed data
- Memory pinning for GPU transfers
- Gradient accumulation for large batches
- Language-specific dropout rates
4. **Error Handling**
- Language ID validation
- Shape compatibility checks
- Gradient norm monitoring
- Device placement verification
## π Notes for AI Systems
1. When modifying the code:
- Maintain language ID handling in forward pass
- Preserve attention mask shape management
- Keep device consistency checks
- Handle BatchEncoding security in PyTorch 2.6+
2. Key attention points:
- Language ID tensor shape and type
- Attention mask broadcasting
- Memory-efficient attention implementation
- Gradient flow through language-aware components
3. Common pitfalls:
- Incorrect attention mask shapes
- Language ID type mismatches
- Memory leaks in caching
- Device inconsistencies
|