Upload folder using huggingface_hub
Browse files- .gitattributes +1 -0
- .ipynb_checkpoints/Transformers-checkpoint.ipynb +0 -0
- README.md +299 -0
- README_HF.md +299 -0
- Transformers.ipynb +0 -0
- best_transformer_model.pth +3 -0
- m4_transformer_results.png +3 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
m4_transformer_results.png filter=lfs diff=lfs merge=lfs -text
|
.ipynb_checkpoints/Transformers-checkpoint.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
README.md
ADDED
|
@@ -0,0 +1,299 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Transformers from Scratch - Complete Implementation
|
| 3 |
+
emoji: ๐ฎ
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: green
|
| 6 |
+
sdk: pytorch
|
| 7 |
+
app_file: Transformers.ipynb
|
| 8 |
+
pinned: false
|
| 9 |
+
license: mit
|
| 10 |
+
tags:
|
| 11 |
+
- deep-learning
|
| 12 |
+
- transformers
|
| 13 |
+
- attention
|
| 14 |
+
- pytorch
|
| 15 |
+
- nlp
|
| 16 |
+
- text-classification
|
| 17 |
+
- sentiment-analysis
|
| 18 |
+
- educational
|
| 19 |
+
- from-scratch
|
| 20 |
+
datasets:
|
| 21 |
+
- synthetic-movie-reviews
|
| 22 |
+
---
|
| 23 |
+
|
| 24 |
+
# Transformers from Scratch: Complete Implementation
|
| 25 |
+
|
| 26 |
+
A comprehensive PyTorch implementation of the Transformer architecture from "Attention Is All You Need", featuring detailed mathematical foundations, educational content, and practical text classification applications.
|
| 27 |
+
|
| 28 |
+
## Model Description
|
| 29 |
+
|
| 30 |
+
This repository contains a complete, from-scratch implementation of the Transformer architecture. The model demonstrates the core concepts behind modern NLP systems like BERT, GPT, and ChatGPT through a practical sentiment analysis task. This implementation serves as both a working model and an educational resource for understanding the revolutionary attention mechanism.
|
| 31 |
+
|
| 32 |
+
### Architecture Details
|
| 33 |
+
|
| 34 |
+
- **Model Type**: Transformer Encoder for Text Classification
|
| 35 |
+
- **Framework**: PyTorch
|
| 36 |
+
- **Task**: Binary sentiment classification (positive/negative movie reviews)
|
| 37 |
+
- **Model Dimension**: 128
|
| 38 |
+
- **Attention Heads**: 8
|
| 39 |
+
- **Layers**: 4 Transformer blocks
|
| 40 |
+
- **Feed-Forward Dimension**: 256
|
| 41 |
+
- **Total Parameters**: ~200K
|
| 42 |
+
- **Vocabulary Size**: Dynamic (built from training data)
|
| 43 |
+
|
| 44 |
+
### Key Components
|
| 45 |
+
|
| 46 |
+
1. **Multi-Head Attention**: Core mechanism allowing parallel processing of sequences
|
| 47 |
+
2. **Positional Encoding**: Sine/cosine embeddings to inject position information
|
| 48 |
+
3. **Transformer Blocks**: Attention + feed-forward with residual connections
|
| 49 |
+
4. **Layer Normalization**: Stabilizes training and improves convergence
|
| 50 |
+
5. **Classification Head**: Global average pooling + linear layer for predictions
|
| 51 |
+
|
| 52 |
+
## Mathematical Foundation
|
| 53 |
+
|
| 54 |
+
### Scaled Dot-Product Attention
|
| 55 |
+
```
|
| 56 |
+
Attention(Q, K, V) = softmax(QK^T / โd_k)V
|
| 57 |
+
```
|
| 58 |
+
|
| 59 |
+
### Multi-Head Attention
|
| 60 |
+
```
|
| 61 |
+
MultiHead(Q, K, V) = Concat(head_1, ..., head_h)W^O
|
| 62 |
+
head_i = Attention(QW_i^Q, KW_i^K, VW_i^V)
|
| 63 |
+
```
|
| 64 |
+
|
| 65 |
+
### Positional Encoding
|
| 66 |
+
```
|
| 67 |
+
PE(pos, 2i) = sin(pos/10000^(2i/d_model))
|
| 68 |
+
PE(pos, 2i+1) = cos(pos/10000^(2i/d_model))
|
| 69 |
+
```
|
| 70 |
+
|
| 71 |
+
## Training Details
|
| 72 |
+
|
| 73 |
+
- **Dataset**: Synthetic movie reviews (positive/negative sentiment)
|
| 74 |
+
- **Optimizer**: AdamW with weight decay (0.01)
|
| 75 |
+
- **Learning Rate**: 0.0001 with cosine annealing
|
| 76 |
+
- **Batch Size**: 16
|
| 77 |
+
- **Max Sequence Length**: 24 tokens
|
| 78 |
+
- **Training Epochs**: 30
|
| 79 |
+
- **Hardware**: Optimized for Apple M4 and CUDA GPUs
|
| 80 |
+
|
| 81 |
+
## Model Performance
|
| 82 |
+
|
| 83 |
+
### Metrics
|
| 84 |
+
- **Test Accuracy**: 85%+
|
| 85 |
+
- **Training Time**: ~10 minutes on Apple M4
|
| 86 |
+
- **Model Size**: 200K parameters
|
| 87 |
+
- **Convergence**: Stable training without overfitting
|
| 88 |
+
|
| 89 |
+
### Capabilities
|
| 90 |
+
- โ
Binary sentiment classification
|
| 91 |
+
- โ
Attention weight visualization
|
| 92 |
+
- โ
Fast inference on modern hardware
|
| 93 |
+
- โ
Educational transparency
|
| 94 |
+
- โ
Easily extensible architecture
|
| 95 |
+
|
| 96 |
+
## Usage
|
| 97 |
+
|
| 98 |
+
### Quick Start
|
| 99 |
+
|
| 100 |
+
```python
|
| 101 |
+
import torch
|
| 102 |
+
import torch.nn as nn
|
| 103 |
+
import math
|
| 104 |
+
|
| 105 |
+
# Load the complete implementation (from notebook)
|
| 106 |
+
class TransformerClassifier(nn.Module):
|
| 107 |
+
def __init__(self, vocab_size, d_model, num_heads, num_layers, d_ff, max_len, num_classes):
|
| 108 |
+
super().__init__()
|
| 109 |
+
self.d_model = d_model
|
| 110 |
+
self.embedding = nn.Embedding(vocab_size, d_model)
|
| 111 |
+
self.pos_encoding = PositionalEncoding(d_model, max_len)
|
| 112 |
+
|
| 113 |
+
self.transformer_blocks = nn.ModuleList([
|
| 114 |
+
TransformerBlock(d_model, num_heads, d_ff)
|
| 115 |
+
for _ in range(num_layers)
|
| 116 |
+
])
|
| 117 |
+
|
| 118 |
+
self.norm = nn.LayerNorm(d_model)
|
| 119 |
+
self.classifier = nn.Linear(d_model, num_classes)
|
| 120 |
+
|
| 121 |
+
def forward(self, x):
|
| 122 |
+
# Embedding + positional encoding
|
| 123 |
+
x = self.embedding(x) * math.sqrt(self.d_model)
|
| 124 |
+
x = self.pos_encoding(x)
|
| 125 |
+
|
| 126 |
+
# Transformer blocks
|
| 127 |
+
for transformer in self.transformer_blocks:
|
| 128 |
+
x = transformer(x)
|
| 129 |
+
|
| 130 |
+
# Classification
|
| 131 |
+
x = self.norm(x)
|
| 132 |
+
x = x.mean(dim=1) # Global average pooling
|
| 133 |
+
return self.classifier(x)
|
| 134 |
+
|
| 135 |
+
# Load trained model
|
| 136 |
+
model = TransformerClassifier(
|
| 137 |
+
vocab_size=vocab_size,
|
| 138 |
+
d_model=128,
|
| 139 |
+
num_heads=8,
|
| 140 |
+
num_layers=4,
|
| 141 |
+
d_ff=256,
|
| 142 |
+
max_len=24,
|
| 143 |
+
num_classes=2
|
| 144 |
+
)
|
| 145 |
+
model.load_state_dict(torch.load('best_transformer_model.pth'))
|
| 146 |
+
model.eval()
|
| 147 |
+
|
| 148 |
+
# Example inference
|
| 149 |
+
def predict_sentiment(text, model, vocab_to_idx, max_length=24):
|
| 150 |
+
tokens = tokenize_text(text, vocab_to_idx, max_length)
|
| 151 |
+
with torch.no_grad():
|
| 152 |
+
output = model(tokens.unsqueeze(0))
|
| 153 |
+
prediction = torch.softmax(output, dim=1)
|
| 154 |
+
return "Positive" if prediction[0][1] > 0.5 else "Negative"
|
| 155 |
+
|
| 156 |
+
# Test the model
|
| 157 |
+
result = predict_sentiment("This movie was absolutely fantastic!", model, vocab_to_idx)
|
| 158 |
+
print(f"Sentiment: {result}")
|
| 159 |
+
```
|
| 160 |
+
|
| 161 |
+
### Advanced Usage
|
| 162 |
+
|
| 163 |
+
```python
|
| 164 |
+
# Visualize attention weights
|
| 165 |
+
def visualize_attention(model, text, vocab_to_idx):
|
| 166 |
+
# Extract attention weights from each layer
|
| 167 |
+
# Create heatmaps showing what the model focuses on
|
| 168 |
+
pass
|
| 169 |
+
|
| 170 |
+
# Fine-tune on new data
|
| 171 |
+
def fine_tune_model(model, new_data_loader, epochs=5):
|
| 172 |
+
optimizer = torch.optim.AdamW(model.parameters(), lr=1e-5)
|
| 173 |
+
# Continue training on domain-specific data
|
| 174 |
+
pass
|
| 175 |
+
```
|
| 176 |
+
|
| 177 |
+
## Visualizations and Analysis
|
| 178 |
+
|
| 179 |
+
1. **Training Curves**: Loss and accuracy evolution over epochs
|
| 180 |
+
2. **Attention Heatmaps**: Visualize what the model pays attention to
|
| 181 |
+
3. **Performance Metrics**: Precision, recall, F1-score breakdowns
|
| 182 |
+
4. **Architecture Diagrams**: Component-wise model visualization
|
| 183 |
+
5. **Error Analysis**: Common failure cases and model limitations
|
| 184 |
+
|
| 185 |
+
## Files and Outputs
|
| 186 |
+
|
| 187 |
+
- `Transformers.ipynb`: Complete implementation with educational content
|
| 188 |
+
- `best_transformer_model.pth`: Trained model weights
|
| 189 |
+
- `m4_transformer_results.png`: Training curves and performance metrics
|
| 190 |
+
- Architecture visualization and attention weight examples
|
| 191 |
+
|
| 192 |
+
## Educational Value
|
| 193 |
+
|
| 194 |
+
This implementation is designed as a comprehensive learning resource featuring:
|
| 195 |
+
|
| 196 |
+
### Mathematical Understanding
|
| 197 |
+
- **Complete Derivations**: From attention theory to implementation
|
| 198 |
+
- **Step-by-Step Breakdown**: Each component explained individually
|
| 199 |
+
- **Visual Mathematics**: Attention visualizations and formula explanations
|
| 200 |
+
- **Practical Examples**: Concrete numerical calculations
|
| 201 |
+
|
| 202 |
+
### Implementation Insights
|
| 203 |
+
- **Clean Code Architecture**: Modular, readable, and well-documented
|
| 204 |
+
- **Best Practices**: Modern PyTorch patterns and techniques
|
| 205 |
+
- **Performance Optimization**: Efficient training and inference
|
| 206 |
+
- **Debugging Techniques**: How to monitor and improve training
|
| 207 |
+
|
| 208 |
+
### Real-World Applications
|
| 209 |
+
- **End-to-End Pipeline**: From raw text to predictions
|
| 210 |
+
- **Production Considerations**: Model deployment and optimization
|
| 211 |
+
- **Extension Examples**: How to adapt for different tasks
|
| 212 |
+
- **Transfer Learning**: Building on pre-trained representations
|
| 213 |
+
|
| 214 |
+
## Applications
|
| 215 |
+
|
| 216 |
+
This Transformer implementation can be adapted for:
|
| 217 |
+
|
| 218 |
+
### Text Classification Tasks
|
| 219 |
+
- **Sentiment Analysis**: Movie reviews, product feedback, social media
|
| 220 |
+
- **Topic Classification**: News categorization, document organization
|
| 221 |
+
- **Spam Detection**: Email filtering, content moderation
|
| 222 |
+
- **Intent Recognition**: Chatbot understanding, voice assistants
|
| 223 |
+
|
| 224 |
+
### Sequence Processing
|
| 225 |
+
- **Named Entity Recognition**: Extract people, places, organizations
|
| 226 |
+
- **Part-of-Speech Tagging**: Grammatical analysis
|
| 227 |
+
- **Text Similarity**: Document matching, plagiarism detection
|
| 228 |
+
- **Feature Extraction**: Dense representations for downstream tasks
|
| 229 |
+
|
| 230 |
+
### Research and Development
|
| 231 |
+
- **Architecture Experiments**: Test new attention mechanisms
|
| 232 |
+
- **Ablation Studies**: Understand component contributions
|
| 233 |
+
- **Scaling Experiments**: Larger models and datasets
|
| 234 |
+
- **Novel Applications**: Domain-specific adaptations
|
| 235 |
+
|
| 236 |
+
## Comparison with Other Architectures
|
| 237 |
+
|
| 238 |
+
### Advantages over RNNs
|
| 239 |
+
- โ
**Parallel Processing**: Much faster training and inference
|
| 240 |
+
- โ
**Long-Range Dependencies**: Better handling of distant relationships
|
| 241 |
+
- โ
**Scalability**: Efficient on modern hardware
|
| 242 |
+
- โ
**Interpretability**: Attention weights provide insights
|
| 243 |
+
|
| 244 |
+
### Advantages over CNNs
|
| 245 |
+
- โ
**Sequence Modeling**: Natural fit for text and time series
|
| 246 |
+
- โ
**Variable Length**: Handle sequences of any length
|
| 247 |
+
- โ
**Global Context**: Attend to entire sequence simultaneously
|
| 248 |
+
- โ
**Position Awareness**: Explicit positional information
|
| 249 |
+
|
| 250 |
+
### Educational Benefits
|
| 251 |
+
- ๐ **Foundation Understanding**: Core concepts behind modern NLP
|
| 252 |
+
- ๐ **Mathematical Clarity**: Clean mathematical formulations
|
| 253 |
+
- ๐ **Implementation Practice**: Hands-on coding experience
|
| 254 |
+
- ๐ **Research Preparation**: Basis for advanced architectures
|
| 255 |
+
|
| 256 |
+
## Citation
|
| 257 |
+
|
| 258 |
+
If you use this implementation in your research or projects, please cite:
|
| 259 |
+
|
| 260 |
+
```bibtex
|
| 261 |
+
@misc{transformers_from_scratch_2024,
|
| 262 |
+
title={Transformers from Scratch: Complete Implementation},
|
| 263 |
+
author={Gruhesh Kurra},
|
| 264 |
+
year={2024},
|
| 265 |
+
url={https://huggingface.co/karthik-2905/TransformersFromScratch}
|
| 266 |
+
}
|
| 267 |
+
```
|
| 268 |
+
|
| 269 |
+
## Future Extensions
|
| 270 |
+
|
| 271 |
+
Planned improvements and research directions:
|
| 272 |
+
|
| 273 |
+
- ๐ **Encoder-Decoder Architecture**: Full sequence-to-sequence implementation
|
| 274 |
+
- ๐จ **Pre-training Pipeline**: Large-scale language model training
|
| 275 |
+
- ๐ **Alternative Attention**: Sparse, local, and linear attention variants
|
| 276 |
+
- ๐ผ๏ธ **Vision Transformers**: Adapt architecture for image tasks
|
| 277 |
+
- ๐ต **Multimodal Transformers**: Text, image, and audio processing
|
| 278 |
+
- ๐งฌ **Scientific Applications**: Protein sequences, molecular modeling
|
| 279 |
+
|
| 280 |
+
## License
|
| 281 |
+
|
| 282 |
+
This project is licensed under the MIT License - see the LICENSE file for details.
|
| 283 |
+
|
| 284 |
+
## Additional Resources
|
| 285 |
+
|
| 286 |
+
- **GitHub Repository**: [TransformersFromScratch](https://github.com/GruheshKurra/TransformersFromScratch)
|
| 287 |
+
- **Original Paper**: "Attention Is All You Need" by Vaswani et al.
|
| 288 |
+
- **Educational Content**: Complete mathematical derivations and examples
|
| 289 |
+
- **Performance Benchmarks**: Detailed analysis and comparisons
|
| 290 |
+
|
| 291 |
+
## Model Card Authors
|
| 292 |
+
|
| 293 |
+
**Gruhesh Kurra** - Implementation, documentation, and educational content
|
| 294 |
+
|
| 295 |
+
---
|
| 296 |
+
|
| 297 |
+
**Tags**: transformers, attention, pytorch, nlp, text-classification, educational
|
| 298 |
+
|
| 299 |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+
- deep-learning
|
| 12 |
+
- transformers
|
| 13 |
+
- attention
|
| 14 |
+
- pytorch
|
| 15 |
+
- nlp
|
| 16 |
+
- text-classification
|
| 17 |
+
- sentiment-analysis
|
| 18 |
+
- educational
|
| 19 |
+
- from-scratch
|
| 20 |
+
datasets:
|
| 21 |
+
- synthetic-movie-reviews
|
| 22 |
+
---
|
| 23 |
+
|
| 24 |
+
# Transformers from Scratch: Complete Implementation
|
| 25 |
+
|
| 26 |
+
A comprehensive PyTorch implementation of the Transformer architecture from "Attention Is All You Need", featuring detailed mathematical foundations, educational content, and practical text classification applications.
|
| 27 |
+
|
| 28 |
+
## Model Description
|
| 29 |
+
|
| 30 |
+
This repository contains a complete, from-scratch implementation of the Transformer architecture. The model demonstrates the core concepts behind modern NLP systems like BERT, GPT, and ChatGPT through a practical sentiment analysis task. This implementation serves as both a working model and an educational resource for understanding the revolutionary attention mechanism.
|
| 31 |
+
|
| 32 |
+
### Architecture Details
|
| 33 |
+
|
| 34 |
+
- **Model Type**: Transformer Encoder for Text Classification
|
| 35 |
+
- **Framework**: PyTorch
|
| 36 |
+
- **Task**: Binary sentiment classification (positive/negative movie reviews)
|
| 37 |
+
- **Model Dimension**: 128
|
| 38 |
+
- **Attention Heads**: 8
|
| 39 |
+
- **Layers**: 4 Transformer blocks
|
| 40 |
+
- **Feed-Forward Dimension**: 256
|
| 41 |
+
- **Total Parameters**: ~200K
|
| 42 |
+
- **Vocabulary Size**: Dynamic (built from training data)
|
| 43 |
+
|
| 44 |
+
### Key Components
|
| 45 |
+
|
| 46 |
+
1. **Multi-Head Attention**: Core mechanism allowing parallel processing of sequences
|
| 47 |
+
2. **Positional Encoding**: Sine/cosine embeddings to inject position information
|
| 48 |
+
3. **Transformer Blocks**: Attention + feed-forward with residual connections
|
| 49 |
+
4. **Layer Normalization**: Stabilizes training and improves convergence
|
| 50 |
+
5. **Classification Head**: Global average pooling + linear layer for predictions
|
| 51 |
+
|
| 52 |
+
## Mathematical Foundation
|
| 53 |
+
|
| 54 |
+
### Scaled Dot-Product Attention
|
| 55 |
+
```
|
| 56 |
+
Attention(Q, K, V) = softmax(QK^T / โd_k)V
|
| 57 |
+
```
|
| 58 |
+
|
| 59 |
+
### Multi-Head Attention
|
| 60 |
+
```
|
| 61 |
+
MultiHead(Q, K, V) = Concat(head_1, ..., head_h)W^O
|
| 62 |
+
head_i = Attention(QW_i^Q, KW_i^K, VW_i^V)
|
| 63 |
+
```
|
| 64 |
+
|
| 65 |
+
### Positional Encoding
|
| 66 |
+
```
|
| 67 |
+
PE(pos, 2i) = sin(pos/10000^(2i/d_model))
|
| 68 |
+
PE(pos, 2i+1) = cos(pos/10000^(2i/d_model))
|
| 69 |
+
```
|
| 70 |
+
|
| 71 |
+
## Training Details
|
| 72 |
+
|
| 73 |
+
- **Dataset**: Synthetic movie reviews (positive/negative sentiment)
|
| 74 |
+
- **Optimizer**: AdamW with weight decay (0.01)
|
| 75 |
+
- **Learning Rate**: 0.0001 with cosine annealing
|
| 76 |
+
- **Batch Size**: 16
|
| 77 |
+
- **Max Sequence Length**: 24 tokens
|
| 78 |
+
- **Training Epochs**: 30
|
| 79 |
+
- **Hardware**: Optimized for Apple M4 and CUDA GPUs
|
| 80 |
+
|
| 81 |
+
## Model Performance
|
| 82 |
+
|
| 83 |
+
### Metrics
|
| 84 |
+
- **Test Accuracy**: 85%+
|
| 85 |
+
- **Training Time**: ~10 minutes on Apple M4
|
| 86 |
+
- **Model Size**: 200K parameters
|
| 87 |
+
- **Convergence**: Stable training without overfitting
|
| 88 |
+
|
| 89 |
+
### Capabilities
|
| 90 |
+
- โ
Binary sentiment classification
|
| 91 |
+
- โ
Attention weight visualization
|
| 92 |
+
- โ
Fast inference on modern hardware
|
| 93 |
+
- โ
Educational transparency
|
| 94 |
+
- โ
Easily extensible architecture
|
| 95 |
+
|
| 96 |
+
## Usage
|
| 97 |
+
|
| 98 |
+
### Quick Start
|
| 99 |
+
|
| 100 |
+
```python
|
| 101 |
+
import torch
|
| 102 |
+
import torch.nn as nn
|
| 103 |
+
import math
|
| 104 |
+
|
| 105 |
+
# Load the complete implementation (from notebook)
|
| 106 |
+
class TransformerClassifier(nn.Module):
|
| 107 |
+
def __init__(self, vocab_size, d_model, num_heads, num_layers, d_ff, max_len, num_classes):
|
| 108 |
+
super().__init__()
|
| 109 |
+
self.d_model = d_model
|
| 110 |
+
self.embedding = nn.Embedding(vocab_size, d_model)
|
| 111 |
+
self.pos_encoding = PositionalEncoding(d_model, max_len)
|
| 112 |
+
|
| 113 |
+
self.transformer_blocks = nn.ModuleList([
|
| 114 |
+
TransformerBlock(d_model, num_heads, d_ff)
|
| 115 |
+
for _ in range(num_layers)
|
| 116 |
+
])
|
| 117 |
+
|
| 118 |
+
self.norm = nn.LayerNorm(d_model)
|
| 119 |
+
self.classifier = nn.Linear(d_model, num_classes)
|
| 120 |
+
|
| 121 |
+
def forward(self, x):
|
| 122 |
+
# Embedding + positional encoding
|
| 123 |
+
x = self.embedding(x) * math.sqrt(self.d_model)
|
| 124 |
+
x = self.pos_encoding(x)
|
| 125 |
+
|
| 126 |
+
# Transformer blocks
|
| 127 |
+
for transformer in self.transformer_blocks:
|
| 128 |
+
x = transformer(x)
|
| 129 |
+
|
| 130 |
+
# Classification
|
| 131 |
+
x = self.norm(x)
|
| 132 |
+
x = x.mean(dim=1) # Global average pooling
|
| 133 |
+
return self.classifier(x)
|
| 134 |
+
|
| 135 |
+
# Load trained model
|
| 136 |
+
model = TransformerClassifier(
|
| 137 |
+
vocab_size=vocab_size,
|
| 138 |
+
d_model=128,
|
| 139 |
+
num_heads=8,
|
| 140 |
+
num_layers=4,
|
| 141 |
+
d_ff=256,
|
| 142 |
+
max_len=24,
|
| 143 |
+
num_classes=2
|
| 144 |
+
)
|
| 145 |
+
model.load_state_dict(torch.load('best_transformer_model.pth'))
|
| 146 |
+
model.eval()
|
| 147 |
+
|
| 148 |
+
# Example inference
|
| 149 |
+
def predict_sentiment(text, model, vocab_to_idx, max_length=24):
|
| 150 |
+
tokens = tokenize_text(text, vocab_to_idx, max_length)
|
| 151 |
+
with torch.no_grad():
|
| 152 |
+
output = model(tokens.unsqueeze(0))
|
| 153 |
+
prediction = torch.softmax(output, dim=1)
|
| 154 |
+
return "Positive" if prediction[0][1] > 0.5 else "Negative"
|
| 155 |
+
|
| 156 |
+
# Test the model
|
| 157 |
+
result = predict_sentiment("This movie was absolutely fantastic!", model, vocab_to_idx)
|
| 158 |
+
print(f"Sentiment: {result}")
|
| 159 |
+
```
|
| 160 |
+
|
| 161 |
+
### Advanced Usage
|
| 162 |
+
|
| 163 |
+
```python
|
| 164 |
+
# Visualize attention weights
|
| 165 |
+
def visualize_attention(model, text, vocab_to_idx):
|
| 166 |
+
# Extract attention weights from each layer
|
| 167 |
+
# Create heatmaps showing what the model focuses on
|
| 168 |
+
pass
|
| 169 |
+
|
| 170 |
+
# Fine-tune on new data
|
| 171 |
+
def fine_tune_model(model, new_data_loader, epochs=5):
|
| 172 |
+
optimizer = torch.optim.AdamW(model.parameters(), lr=1e-5)
|
| 173 |
+
# Continue training on domain-specific data
|
| 174 |
+
pass
|
| 175 |
+
```
|
| 176 |
+
|
| 177 |
+
## Visualizations and Analysis
|
| 178 |
+
|
| 179 |
+
1. **Training Curves**: Loss and accuracy evolution over epochs
|
| 180 |
+
2. **Attention Heatmaps**: Visualize what the model pays attention to
|
| 181 |
+
3. **Performance Metrics**: Precision, recall, F1-score breakdowns
|
| 182 |
+
4. **Architecture Diagrams**: Component-wise model visualization
|
| 183 |
+
5. **Error Analysis**: Common failure cases and model limitations
|
| 184 |
+
|
| 185 |
+
## Files and Outputs
|
| 186 |
+
|
| 187 |
+
- `Transformers.ipynb`: Complete implementation with educational content
|
| 188 |
+
- `best_transformer_model.pth`: Trained model weights
|
| 189 |
+
- `m4_transformer_results.png`: Training curves and performance metrics
|
| 190 |
+
- Architecture visualization and attention weight examples
|
| 191 |
+
|
| 192 |
+
## Educational Value
|
| 193 |
+
|
| 194 |
+
This implementation is designed as a comprehensive learning resource featuring:
|
| 195 |
+
|
| 196 |
+
### Mathematical Understanding
|
| 197 |
+
- **Complete Derivations**: From attention theory to implementation
|
| 198 |
+
- **Step-by-Step Breakdown**: Each component explained individually
|
| 199 |
+
- **Visual Mathematics**: Attention visualizations and formula explanations
|
| 200 |
+
- **Practical Examples**: Concrete numerical calculations
|
| 201 |
+
|
| 202 |
+
### Implementation Insights
|
| 203 |
+
- **Clean Code Architecture**: Modular, readable, and well-documented
|
| 204 |
+
- **Best Practices**: Modern PyTorch patterns and techniques
|
| 205 |
+
- **Performance Optimization**: Efficient training and inference
|
| 206 |
+
- **Debugging Techniques**: How to monitor and improve training
|
| 207 |
+
|
| 208 |
+
### Real-World Applications
|
| 209 |
+
- **End-to-End Pipeline**: From raw text to predictions
|
| 210 |
+
- **Production Considerations**: Model deployment and optimization
|
| 211 |
+
- **Extension Examples**: How to adapt for different tasks
|
| 212 |
+
- **Transfer Learning**: Building on pre-trained representations
|
| 213 |
+
|
| 214 |
+
## Applications
|
| 215 |
+
|
| 216 |
+
This Transformer implementation can be adapted for:
|
| 217 |
+
|
| 218 |
+
### Text Classification Tasks
|
| 219 |
+
- **Sentiment Analysis**: Movie reviews, product feedback, social media
|
| 220 |
+
- **Topic Classification**: News categorization, document organization
|
| 221 |
+
- **Spam Detection**: Email filtering, content moderation
|
| 222 |
+
- **Intent Recognition**: Chatbot understanding, voice assistants
|
| 223 |
+
|
| 224 |
+
### Sequence Processing
|
| 225 |
+
- **Named Entity Recognition**: Extract people, places, organizations
|
| 226 |
+
- **Part-of-Speech Tagging**: Grammatical analysis
|
| 227 |
+
- **Text Similarity**: Document matching, plagiarism detection
|
| 228 |
+
- **Feature Extraction**: Dense representations for downstream tasks
|
| 229 |
+
|
| 230 |
+
### Research and Development
|
| 231 |
+
- **Architecture Experiments**: Test new attention mechanisms
|
| 232 |
+
- **Ablation Studies**: Understand component contributions
|
| 233 |
+
- **Scaling Experiments**: Larger models and datasets
|
| 234 |
+
- **Novel Applications**: Domain-specific adaptations
|
| 235 |
+
|
| 236 |
+
## Comparison with Other Architectures
|
| 237 |
+
|
| 238 |
+
### Advantages over RNNs
|
| 239 |
+
- โ
**Parallel Processing**: Much faster training and inference
|
| 240 |
+
- โ
**Long-Range Dependencies**: Better handling of distant relationships
|
| 241 |
+
- โ
**Scalability**: Efficient on modern hardware
|
| 242 |
+
- โ
**Interpretability**: Attention weights provide insights
|
| 243 |
+
|
| 244 |
+
### Advantages over CNNs
|
| 245 |
+
- โ
**Sequence Modeling**: Natural fit for text and time series
|
| 246 |
+
- โ
**Variable Length**: Handle sequences of any length
|
| 247 |
+
- โ
**Global Context**: Attend to entire sequence simultaneously
|
| 248 |
+
- โ
**Position Awareness**: Explicit positional information
|
| 249 |
+
|
| 250 |
+
### Educational Benefits
|
| 251 |
+
- ๐ **Foundation Understanding**: Core concepts behind modern NLP
|
| 252 |
+
- ๐ **Mathematical Clarity**: Clean mathematical formulations
|
| 253 |
+
- ๐ **Implementation Practice**: Hands-on coding experience
|
| 254 |
+
- ๐ **Research Preparation**: Basis for advanced architectures
|
| 255 |
+
|
| 256 |
+
## Citation
|
| 257 |
+
|
| 258 |
+
If you use this implementation in your research or projects, please cite:
|
| 259 |
+
|
| 260 |
+
```bibtex
|
| 261 |
+
@misc{transformers_from_scratch_2024,
|
| 262 |
+
title={Transformers from Scratch: Complete Implementation},
|
| 263 |
+
author={Gruhesh Kurra},
|
| 264 |
+
year={2024},
|
| 265 |
+
url={https://huggingface.co/karthik-2905/TransformersFromScratch}
|
| 266 |
+
}
|
| 267 |
+
```
|
| 268 |
+
|
| 269 |
+
## Future Extensions
|
| 270 |
+
|
| 271 |
+
Planned improvements and research directions:
|
| 272 |
+
|
| 273 |
+
- ๐ **Encoder-Decoder Architecture**: Full sequence-to-sequence implementation
|
| 274 |
+
- ๐จ **Pre-training Pipeline**: Large-scale language model training
|
| 275 |
+
- ๐ **Alternative Attention**: Sparse, local, and linear attention variants
|
| 276 |
+
- ๐ผ๏ธ **Vision Transformers**: Adapt architecture for image tasks
|
| 277 |
+
- ๐ต **Multimodal Transformers**: Text, image, and audio processing
|
| 278 |
+
- ๐งฌ **Scientific Applications**: Protein sequences, molecular modeling
|
| 279 |
+
|
| 280 |
+
## License
|
| 281 |
+
|
| 282 |
+
This project is licensed under the MIT License - see the LICENSE file for details.
|
| 283 |
+
|
| 284 |
+
## Additional Resources
|
| 285 |
+
|
| 286 |
+
- **GitHub Repository**: [TransformersFromScratch](https://github.com/GruheshKurra/TransformersFromScratch)
|
| 287 |
+
- **Original Paper**: "Attention Is All You Need" by Vaswani et al.
|
| 288 |
+
- **Educational Content**: Complete mathematical derivations and examples
|
| 289 |
+
- **Performance Benchmarks**: Detailed analysis and comparisons
|
| 290 |
+
|
| 291 |
+
## Model Card Authors
|
| 292 |
+
|
| 293 |
+
**Gruhesh Kurra** - Implementation, documentation, and educational content
|
| 294 |
+
|
| 295 |
+
---
|
| 296 |
+
|
| 297 |
+
**Tags**: transformers, attention, pytorch, nlp, text-classification, educational
|
| 298 |
+
|
| 299 |
+
**Model Card Last Updated**: December 2024
|
Transformers.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
best_transformer_model.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5d2c6241ca2e72ed6e0587c6875485bb522b2b55d4fc6272c8686f139a379f20
|
| 3 |
+
size 2215301
|
m4_transformer_results.png
ADDED
|
Git LFS Details
|