Text Classification
Transformers
PyTorch
English
wrag2
weight-retrieval
domain-adaptation
medical
legal
code
Instructions to use Gyeti123/wrag2-text-classifier with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Gyeti123/wrag2-text-classifier with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-classification", model="Gyeti123/wrag2-text-classifier")# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("Gyeti123/wrag2-text-classifier", device_map="auto") - Notebooks
- Google Colab
- Kaggle
| language: en | |
| license: apache-2.0 | |
| tags: | |
| - text-classification | |
| - weight-retrieval | |
| - domain-adaptation | |
| - medical | |
| - legal | |
| - code | |
| datasets: | |
| - qiaojin/PubMedQA | |
| - lex_glue | |
| - code_search_net | |
| metrics: | |
| - accuracy | |
| library_name: transformers | |
| # WRAG 2.0: Weight-Retrieval Augmented Generation | |
| **WRAG 2.0** is a novel neural architecture that uses dynamic weight retrieval during the forward pass to achieve domain specialization without retraining the base model. | |
| ## π― Performance | |
| Trained on 3 domains with only 500 samples per domain (5 epochs): | |
| | Domain | Accuracy | | |
| |--------|----------| | |
| | Medical (PubMedQA) | **70%** | | |
| | Legal (LexGLUE) | **84%** | | |
| | Code (CodeSearchNet) | **95%** | | |
| | **Average** | **83%** | | |
| ## π Quick Start | |
| ```python | |
| from modeling_wrag2 import WRAG2TextModel | |
| import torch | |
| # Load model | |
| model = WRAG2TextModel( | |
| base_model_name="TinyLlama/TinyLlama-1.1B-Chat-v1.0", | |
| num_shards=10, | |
| k=3, | |
| num_wr_layers=3 | |
| ) | |
| # Load trained weights | |
| state_dict = torch.load("pytorch_model.bin") | |
| model.load_state_dict(state_dict, strict=False) | |
| # Move to device | |
| device = "cuda" if torch.cuda.is_available() else "cpu" | |
| model = model.to(device) | |
| model.eval() | |
| # Inference | |
| text = ["Question: What is the treatment for diabetes?"] | |
| inputs = model.tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=256).to(device) | |
| with torch.no_grad(): | |
| logits, shard_scores = model(**inputs) | |
| prediction = logits.argmax(dim=-1) | |
| print(f"Prediction: {prediction.item()}") # 0 or 1 | |
| ``` | |
| ## π‘ Key Features | |
| - β **Domain Specialization**: Different weight shards activate for different domains | |
| - β **Efficient**: Only 264M trainable params (20% of total model) | |
| - β **Fast Training**: 5 epochs, ~30 minutes on L4 GPU | |
| - β **Memory Efficient**: Fits in 24GB GPU | |
| - β **Lightweight Download**: Only 264MB (base model downloaded separately) | |
| ## ποΈ Architecture | |
| ``` | |
| Input Text | |
| β | |
| Frozen TinyLlama-1.1B (feature extraction) | |
| β | |
| Mean Pooling | |
| β | |
| Weight Retrieval Layer 1 (10 shards, k=3) | |
| β ReLU | |
| Weight Retrieval Layer 2 (10 shards, k=3) | |
| β ReLU | |
| Weight Retrieval Layer 3 (10 shards, k=3) | |
| β ReLU | |
| Classification Head (2 classes) | |
| β | |
| Output | |
| ``` | |
| ## π Model Details | |
| - **Base Model**: TinyLlama/TinyLlama-1.1B-Chat-v1.0 (frozen) | |
| - **Trainable Parameters**: 264M (20.2% of total) | |
| - **Total Parameters**: 1.3B | |
| - **Weight Shards**: 10 per layer | |
| - **Top-k Selection**: 3 shards per forward pass | |
| - **Number of WR Layers**: 3 | |
| ## π§ Training Details | |
| - **Optimizer**: AdamW | |
| - **Learning Rate**: 1e-4 | |
| - **Batch Size**: 4 | |
| - **Epochs**: 5 | |
| - **Max Sequence Length**: 256 | |
| - **Training Data**: 500 samples per domain (medical, legal, code) | |
| - **Training Time**: ~30 minutes on NVIDIA L4 GPU | |
| ## π¦ Installation | |
| ```bash | |
| pip install torch transformers huggingface_hub | |
| ``` | |
| ## π How It Works | |
| WRAG 2.0 uses **dynamic weight retrieval**: | |
| 1. Input text is encoded by frozen TinyLlama | |
| 2. Each weight retrieval layer: | |
| - Computes similarity between input and 10 weight shards | |
| - Selects top-3 most relevant shards | |
| - Composes dynamic weight matrix via weighted combination | |
| - Applies dynamic weights to input | |
| 3. Classification head produces final prediction | |
| This allows the model to specialize for different domains without retraining the base model! | |
| ## β οΈ Limitations | |
| - Binary classification only (yes/no, 0/1) | |
| - Trained on limited data (500 samples per domain) | |
| - Requires TinyLlama base model (~4.4GB) to be downloaded separately | |
| - Best for domain-specific tasks similar to training domains | |
| ## π Citation | |
| ```bibtex | |
| @misc{wrag2-2025, | |
| title={WRAG 2.0: Weight-Retrieval Augmented Generation}, | |
| year={2025}, | |
| url={https://huggingface.co/YOUR_USERNAME/wrag2-text-classifier} | |
| } | |
| ``` | |
| ## π License | |
| Apache 2.0 | |
| ## π€ Contributing | |
| Issues and pull requests welcome! | |