Views
No views yet
wakaflocka17/ensemble-majority-voting-imdb model is a majority-voting ensemble of three fine-tuned sentiment classifiers (bert-imdb-finetuned, bart-imdb-finetuned, gptneo-imdb-finetuned) on the IMDb dataset. Each model votes on the sentiment label and the ensemble returns the label with the most votes, improving overall accuracy.| Metric | Value |
|---|---|
| Accuracy | 0.93296 |
| Precision | 0.9559 |
| Recall | 0.9078 |
| F1-score | 0.9312 |
| Parameter | Values |
|---|---|
| Models in ensemble | bert_base_uncased, bart_base, gpt_neo_2_7b |
| Repo for ensemble | models/ensemble_majority_voting |
| Batch size (eval) | 64 |
!pip install --upgrade transformers huggingface_hub1from huggingface_hub import login
2login(token="hf_yourhftoken")1from transformers import AutoTokenizer, AutoModelForSequenceClassification, TextClassificationPipeline
2from collections import Counter
3
4# List of fine-tuned model repo IDs
5model_ids = [
6 "wakaflocka17/bert-imdb-finetuned",
7 "wakaflocka17/bart-imdb-finetuned",
8 "wakaflocka17/gptneo-imdb-finetuned"
9]1pipelines = []
2for repo_id in model_ids:
3 tokenizer = AutoTokenizer.from_pretrained(repo_id)
4 model = AutoModelForSequenceClassification.from_pretrained(repo_id)
5 model.config.id2label = {0: 'NEGATIVE', 1: 'POSITIVE'}
6 pipelines.append(TextClassificationPipeline(model=model, tokenizer=tokenizer, return_all_scores=False))1def ensemble_predict(text):
2 votes = []
3 # Collect each model's vote along with its name
4 for model_id, pipe in zip(model_ids, pipelines):
5 label = pipe(text)[0]['label']
6 votes.append({
7 "model": model_id, # or model_id.split("/")[-1] for just the short name
8 "label": label
9 })
10 # Determine majority label
11 majority_label = Counter([v["label"] for v in votes]).most_common(1)[0][0]
12 return {
13 "ensemble_label": majority_label,
14 "individual_votes": votes
15 }1testo = "This movie was absolutely fantastic—wonderful performances and a gripping story!"
2result = ensemble_predict(testo)
3print(result)
4# Example output:
5# {
6# 'ensemble_label': 'POSITIVE',
7# 'individual_votes': [
8# {'model': 'wakaflocka17/bert-imdb-finetuned', 'label': 'POSITIVE'},
9# {'model': 'wakaflocka17/bart-imdb-finetuned', 'label': 'NEGATIVE'},
10# {'model': 'wakaflocka17/gptneo-imdb-finetuned', 'label': 'POSITIVE'}
11# ]
12# }1@misc{Sentiment-Project,
2 author = {Francesco Congiu},
3 title = {Sentiment Analysis with Pretrained, Fine-tuned and Ensemble Transformer Models},
4 howpublished = {\url{https://github.com/wakaflocka17/DLA_LLMSANALYSIS}},
5 year = {2025}
6}All the file structure and script examples can be found at: https://github.com/wakaflocka17/DLA_LLMSANALYSIS/tree/main