mteb/tweet_sentiment_extraction
Viewer โข Updated โข 30.2k โข 4.59k โข 38
A fine-tuned GPT-2 model for 3-class sentiment classification (negative / neutral / positive), trained on the Tweet Sentiment Extraction dataset.
| Property | Details |
|---|---|
| Base Model | gpt2 (117M parameters) |
| Task | Text Classification (Sentiment Analysis) |
| Labels | 0 โ negative, 1 โ neutral, 2 โ positive |
| Dataset | mteb/tweet_sentiment_extraction |
| Training Samples | 1,000 (shuffled subset, seed=42) |
| Eval Samples | 1,000 (shuffled subset, seed=42) |
| Max Token Length | 512 |
| Checkpoint | test_trainer/checkpoint-375 |
| Parameter | Value |
|---|---|
| Epochs | 3 |
| Train Batch Size | 8 |
| Eval Batch Size | 8 |
| Gradient Accumulation Steps | 1 |
| Precision | FP16 (mixed precision) |
| Logging Steps | 10 |
| Optimizer | AdamW (default Trainer) |
transformers + Trainer APItransformers, datasets, evaluate, torch, acceleratepad_token is set to eos_token (GPT-2 has no native pad token)model.config.pad_token_id is explicitly set to avoid generation warningsDataCollatorWithPadding is used for dynamic padding during trainingpip install transformers torch
import torch
from transformers import GPT2ForSequenceClassification, GPT2Tokenizer
model_name = "ayushArtesian/gpt2-sentiment-model"
tokenizer = GPT2Tokenizer.from_pretrained(model_name)
model = GPT2ForSequenceClassification.from_pretrained(model_name)
# Required โ GPT-2 has no native pad token
tokenizer.pad_token = tokenizer.eos_token
model.config.pad_token_id = tokenizer.pad_token_id
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)
model.eval()
label_map = {0: "negative", 1: "neutral", 2: "positive"}
def predict_sentiment(text: str) -> str:
inputs = tokenizer(
text,
return_tensors="pt",
padding=True,
truncation=True,
max_length=512
)
inputs = {k: v.to(device) for k, v in inputs.items()}
with torch.no_grad():
outputs = model(**inputs)
predicted_class = torch.argmax(outputs.logits, dim=1).item()
return label_map[predicted_class]
# Example
print(predict_sentiment("I hope your day is as pleasant as you are"))
# โ positive
print(predict_sentiment("This is the worst experience I've ever had"))
# โ negative
from transformers import pipeline
classifier = pipeline(
"text-classification",
model="ayushArtesian/gpt2-sentiment-model"
)
result = classifier("I absolutely love this!")
print(result)
# โ [{'label': 'LABEL_2', 'score': 0.91}] (LABEL_2 = positive)
The model was trained on the mteb/tweet_sentiment_extraction dataset, which contains tweets labelled with three sentiment classes:
| Label | Meaning |
|---|---|
| 0 | Negative |
| 1 | Neutral |
| 2 | Positive |
Only a 1,000-sample subset of the training and test splits was used for this experiment.
If you use this model, please consider citing the original GPT-2 paper:
@article{radford2019language,
title={Language Models are Unsupervised Multitask Learners},
author={Radford, Alec and Wu, Jeff and Child, Rewon and Luan, David and Amodei, Dario and Sutskever, Ilya},
year={2019}
}
ayushArtesian โ HuggingFace Profile
Base model
openai-community/gpt2