-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransferlearning_huggingface.py
More file actions
92 lines (67 loc) · 3.69 KB
/
transferlearning_huggingface.py
File metadata and controls
92 lines (67 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# -*- coding: utf-8 -*-
"""TransferLearning_HuggingFace.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/16y-hKZHY5tWW2H4LaclauQ4y7QGWDp6M
**Objective:**
Run prediction (inference) using pre-trained models and dataset from HuggingFace.
***Imported the Necessary libraries***
"""
from transformers import AutoModelForSequenceClassification, AutoTokenizer
from datasets import load_dataset
from sklearn.metrics import confusion_matrix, accuracy_score
import torch
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
"""**Loading a pre trained model amd dataset from HuggingFace**
Model name = "bert-base-uncasaed"
Dataset name = "imdb"
"""
# Load a pre-trained model and dataset
model_name = "bert-base-uncased"
model = AutoModelForSequenceClassification.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)
dataset = load_dataset("imdb")
"""**Preparing the data**"""
# Sample a small percentage of the data (e.g., 1%) for faster execution
sample_percentage = 0.01
sample_size = int(sample_percentage * len(dataset["test"]))
sample_indices = np.random.choice(len(dataset["test"]), sample_size, replace=False)
# Prepare the data
sampled_texts = [dataset["test"]["text"][i] for i in sample_indices]
sampled_labels = [dataset["test"]["label"][i] for i in sample_indices]
# Tokenize the sampled data
inputs = tokenizer(sampled_texts, padding="max_length", truncation=True, return_tensors="pt")
"""**Evaluating the model and computing the confusion matrix**"""
# Run predictions
with torch.no_grad():
outputs = model(**inputs)
# Calculate a confusion matrix
predictions = outputs.logits.argmax(dim=1)
confusion = confusion_matrix(sampled_labels, predictions)
# Print the confusion matrix
print("Confusion Matrix:")
print(confusion)
# Plot the confusion matrix
plt.figure(figsize=(8, 6))
sns.heatmap(confusion, annot=True, fmt="d", cmap="Blues")
plt.xlabel("Predicted Labels")
plt.ylabel("True Labels")
plt.title("Confusion Matrix")
plt.show()
"""The model seems to be performing well in identifying negative sentiments (True Negatives), but it struggles to correctly identify positive sentiments (False Negatives). The absence of True Positives (correctly predicted positive sentiments) in this sample suggests that there may be room for improvement in the model's ability to recognize positive sentiment in movie reviews."""
# Calculate accuracy
accuracy = accuracy_score(sampled_labels, predictions)
print("Accuracy:", accuracy)
"""0.476 percent of accuracy means that the model is correct in its predictions for a majority of the sampled movie reviews. Out of all the movie reviews in your sample, the model correctly predicted the sentiment for approximately 47% of them.
**Few examples of the predicted output**
"""
# Display a few examples of predicted outputs
for i in range(5): # Display 5 examples
print("Text:", sampled_texts[i])
print("True Label:", sampled_labels[i])
print("Predicted Label:", predictions[i])
"""**Observation:** Based on the output, it appears that the model's predictions don't align perfectly with the true labels. For example, in the first and second reviews, the true labels are 0 (negative), but the model predicted 0 as well. However, in the third and fourth reviews, the true labels are 0, but the model incorrectly predicted 0 (negative) instead of 1 (positive). In the fifth review, both the true label and predicted label are 0.
This demonstrates that the model's predictions are not always accurate and that it might struggle with correctly classifying sentiment in some movie reviews. Further fine-tuning and model selection might improve the accuracy of sentiment classification.
"""