XLM-T RoBERTa Balanced Sentiment Model for Roman Urdu
Model Overview
This is a balanced version of the sentiment analysis model specifically optimized for Roman Urdu text. Unlike the previous version, this model was trained on a perfectly balanced dataset (1200 samples per class), resulting in significantly improved neutral sentiment detection.
Predicted
Actual Neg Neu Pos
Negative 207 24 9
Neutral 20 196 24
Positive 11 20 209
Dataset Information
Total samples: 3,600 (balanced)
Training samples: 2,592 (864 per class)
Validation samples: 288 (96 per class)
Test samples: 720 (240 per class)
Class distribution: Perfectly balanced (33.3% each)
Training Details
Preprocessing Pipeline
The model uses a comprehensive Roman Urdu normalization pipeline:
Lowercasing
URL and mention removal
Roman Urdu dictionary-based normalization
Handles mixed Urdu/English/Roman Urdu scripts
Fine-tuning Configuration
Parameter
Value
Method
LoRA (Low-Rank Adaptation)
LoRA Rank (r)
12
LoRA Alpha
24
LoRA Dropout
0.1
Target Modules
query, key, value, dense
Trainable Parameters
2.58M (0.92% of total)
Learning Rate
2e-5
Batch Size
16
Epochs
8
Optimizer
AdamW
Weight Decay
0.01
Max Sequence Length
128 tokens
How to Use
Installation
pip install transformers torch
Inference Code
python
1from transformers import AutoModelForSequenceClassification, AutoTokenizer
2import torch
34# Load model and tokenizer5model_name ="Umair1710/xlm-roberta-balanced-sentiment"6model = AutoModelForSequenceClassification.from_pretrained(model_name)7tokenizer = AutoTokenizer.from_pretrained(model_name)89# Roman Urdu preprocessing10defpreprocess_text(text):11import re
12 roman_urdu_dict ={13'aj':'aaj','acha':'achha','bohat':'bahut',14'nahi':'nahin','kya':'kya','yaar':'yar',15'thora':'thoda','jeet':'jeet','gaye':'gaye'16}17 text = text.lower()18 words = text.split()19 normalized =[roman_urdu_dict.get(word, word)for word in words]20return' '.join(normalized)2122defpredict_sentiment(text):23 text = preprocess_text(text)24 inputs = tokenizer(text, return_tensors='pt', truncation=True, max_length=128)25with torch.no_grad():26 outputs = model(**inputs)27 probs = torch.nn.functional.softmax(outputs.logits, dim=-1)28 pred_class = torch.argmax(probs, dim=-1).item()29 labels ={0:'negative',1:'neutral',2:'positive'}30return labels[pred_class],float(probs[0][pred_class])3132# Example33sentiment, confidence = predict_sentiment('aj acha din tha jeet gaye')34print(f'Sentiment: {sentiment} (confidence: {confidence:.3f})')
Example Predictions
Text
Predicted
Confidence
jeet gaye badminton singles
Positive
0.962
aj normal din tha
Neutral
0.945
My alarm didn't go off
Negative
0.923
The professor gave us an assignment
Neutral
0.728
Citation
If you use this model in your research, please cite:
bibtex
1@misc{umair2024xlmtbalanced,
2 author = {Umair},
3 title = {XLM-T RoBERTa Balanced Sentiment Model for Roman Urdu},
4 year = {2024},
5 publisher = {Hugging Face},
6 url = {https://huggingface.co/Umair1710/xlm-roberta-balanced-sentiment}
7}
License
This model is released under the Apache 2.0 license.