gautamnancy commited on
Commit
e5340b1
Β·
verified Β·
1 Parent(s): 53c4a01

Upload READ_ME.md

Browse files
Files changed (1) hide show
  1. READ_ME.md +125 -0
READ_ME.md ADDED
@@ -0,0 +1,125 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # DistilBERT Quantized Model for Sentiment Analysis on Yelp Polarity Dataset
2
+
3
+ This repository hosts a quantized version of the DistilBERT model, fine-tuned for sentiment analysis tasks on the Yelp Polarity dataset. The model has been optimized using post-training quantization to make it suitable for resource-constrained environments while maintaining high accuracy.
4
+
5
+ ## Model Details
6
+
7
+ - **Model Architecture:** DistilBERT Base Uncased
8
+ - **Task:** Sentiment Analysis
9
+ - **Dataset:** Yelp Polarity
10
+ - **Quantization:** Dynamic Quantization (INT8 on Linear layers)
11
+ - **Fine-tuning Framework:** Hugging Face Transformers
12
+
13
+ ---
14
+
15
+ ### Installation
16
+
17
+ ```sh
18
+ pip install transformers datasets evaluate scikit-learn torch
19
+
20
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
21
+ import torch
22
+
23
+ # Load trained model and tokenizer
24
+ model = AutoModelForSequenceClassification.from_pretrained("./results")
25
+ tokenizer = AutoTokenizer.from_pretrained("./results")
26
+
27
+ # Set model to eval mode
28
+ model.eval()
29
+
30
+ # 10 Sample review texts
31
+ sample_texts = [
32
+ "The food was absolutely wonderful!",
33
+ "Terrible experience. I will never come back.",
34
+ "Average service, but the food was decent.",
35
+ "I loved the ambiance and the staff was super friendly!",
36
+ "Worst food I've had in a long time.",
37
+ "Highly recommend this place for a date night.",
38
+ "The waiter was rude and the food was cold.",
39
+ "Amazing pizza, will order again!",
40
+ "They took too long to serve and it was overpriced.",
41
+ "Best customer service and delicious desserts!"
42
+ ]
43
+
44
+ # Predict and print results
45
+ for text in sample_texts:
46
+ inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
47
+ with torch.no_grad():
48
+ outputs = model(**inputs)
49
+ prediction = torch.argmax(outputs.logits, dim=-1).item()
50
+ sentiment = "Positive" if prediction == 1 else "Negative"
51
+ print(f"Text: {text}\\nPredicted Sentiment: {sentiment}\\n")
52
+
53
+ Quantization
54
+
55
+ import os
56
+ import torch
57
+ from transformers import AutoModelForSequenceClassification, AutoTokenizer
58
+
59
+ # Load the fine-tuned model
60
+ model = AutoModelForSequenceClassification.from_pretrained("./results")
61
+
62
+ # Apply dynamic quantization
63
+ quantized_model = torch.quantization.quantize_dynamic(
64
+ model,
65
+ {torch.nn.Linear},
66
+ dtype=torch.qint8
67
+ )
68
+
69
+ # Define path
70
+ quantized_model_path = "./results/quantized_model"
71
+
72
+ # Create directory if it doesn't exist
73
+ os.makedirs(quantized_model_path, exist_ok=True)
74
+
75
+ # Save the quantized model weights
76
+ torch.save(quantized_model.state_dict(), f"{quantized_model_path}/pytorch_model.bin")
77
+
78
+ # Save config and tokenizer
79
+ model.config.save_pretrained(quantized_model_path)
80
+ tokenizer = AutoTokenizer.from_pretrained("./results")
81
+ tokenizer.save_pretrained(quantized_model_path)
82
+
83
+ print("βœ… Quantized model saved at:", quantized_model_path)
84
+
85
+ Performance Metrics
86
+ Accuracy: Approx. 95% on Yelp Polarity Test Subset
87
+
88
+ Precision, Recall, F1-score: Computed during evaluation using scikit-learn
89
+
90
+ Fine-Tuning Details
91
+ Dataset
92
+ Source: Yelp Polarity (via Hugging Face Datasets)
93
+
94
+ Train samples used: 50,000
95
+
96
+ Test samples used: 10,000
97
+
98
+ Training
99
+ Number of epochs: 3
100
+
101
+ Batch size: 16
102
+
103
+ Evaluation strategy: Per epoch
104
+
105
+ Learning rate: 2e-5
106
+
107
+ Weight decay: 0.01
108
+
109
+ Repository-Structure
110
+
111
+ .
112
+ β”œβ”€β”€ results/ # Contains fine-tuned and quantized model files
113
+ β”‚ β”œβ”€β”€ pytorch_model.bin # Quantized model weights
114
+ β”‚ β”œβ”€β”€ config.json # Model config
115
+ β”‚ └── tokenizer/ # Tokenizer files
116
+ β”œβ”€β”€ logs/ # Training logs
117
+ β”œβ”€β”€ README.md # Model documentation
118
+
119
+
120
+ Limitations
121
+
122
+ The model is trained only on Yelp reviews and may not generalize to other domains.
123
+
124
+ Post-training quantization may cause minor accuracy degradation compared to full-precision models.
125
+