Create README.md
Browse files
README.md
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
language:
|
| 3 |
+
- en
|
| 4 |
+
metrics:
|
| 5 |
+
- accuracy
|
| 6 |
+
---
|
| 7 |
+
# Custom Transformer for Amazon Sentiment Analysis
|
| 8 |
+
|
| 9 |
+
This repository contains a **custom-built Transformer Encoder** model for binary sentiment classification, trained on the **Amazon Polarity** dataset.
|
| 10 |
+
|
| 11 |
+
## 🚀 Model Overview
|
| 12 |
+
Unlike standard pre-trained models (like BERT), this architecture was built from scratch to demonstrate the implementation of **Self-Attention** and **Positional Encodings** in PyTorch.
|
| 13 |
+
|
| 14 |
+
* **Architecture**: 4-Layer Transformer Encoder
|
| 15 |
+
* **Task**: Binary Sentiment Analysis (Positive/Negative)
|
| 16 |
+
* **Accuracy**: 89.67% on Test Set
|
| 17 |
+
* **Parameters**: Optimized for efficient inference on edge devices
|
| 18 |
+
|
| 19 |
+
## 🛠️ Technical Specifications
|
| 20 |
+
* **Embedding Dimension**: 128
|
| 21 |
+
* **Attention Heads**: 8
|
| 22 |
+
* **Feed-Forward Dimension**: 512
|
| 23 |
+
* **Sequence Length**: 300 tokens
|
| 24 |
+
* **Optimizer**: AdamW with Linear Learning Rate Warmup
|
| 25 |
+
|
| 26 |
+
## 💻 Training Environment
|
| 27 |
+
This model was trained locally on an **Apple Mac mini M4** with **24GB of Unified Memory**.
|
| 28 |
+
* **Accelerator**: Metal Performance Shaders (MPS)
|
| 29 |
+
* **Dataset**: Subset of 500,000 samples from Amazon Polarity
|
| 30 |
+
|
| 31 |
+
## 📈 Performance & Insights
|
| 32 |
+
During development, the model was benchmarked against a Bidirectional LSTM. The Transformer architecture achieved a **~5% improvement in accuracy**, demonstrating its superior ability to capture long-range dependencies in product reviews.
|
| 33 |
+
|
| 34 |
+
## 📖 How to Use
|
| 35 |
+
To use this model, ensure you have `torch` and `transformers` installed.
|
| 36 |
+
|
| 37 |
+
```python
|
| 38 |
+
from transformers import DistilBertTokenizer
|
| 39 |
+
import torch
|
| 40 |
+
|
| 41 |
+
# 1. Initialize Tokenizer
|
| 42 |
+
tokenizer = DistilBertTokenizer.from_pretrained('Nefflymicn/amazon-sentiment-transformer')
|
| 43 |
+
|
| 44 |
+
# 2. Load Model (Architecture must match)
|
| 45 |
+
model = TransformerSentimentModel(
|
| 46 |
+
vocab_size=tokenizer.vocab_size,
|
| 47 |
+
embed_dim=128,
|
| 48 |
+
num_heads=8,
|
| 49 |
+
ff_dim=512,
|
| 50 |
+
num_layers=4,
|
| 51 |
+
output_dim=2
|
| 52 |
+
)
|
| 53 |
+
model.load_state_dict(torch.load("pytorch_model.bin", map_location='cpu'))
|
| 54 |
+
model.eval()
|