-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluate_bert.py
More file actions
114 lines (95 loc) · 2.96 KB
/
evaluate_bert.py
File metadata and controls
114 lines (95 loc) · 2.96 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import torch
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from transformers import BertTokenizerFast, BertForSequenceClassification
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report, confusion_matrix
from torch.utils.data import Dataset, DataLoader
# =========================
# LOAD DATA
# =========================
labels, texts = [], []
with open("data/spam.csv", encoding="latin-1") as f:
for line in f:
line = line.strip()
if not line:
continue
parts = line.split(";", 1)
if len(parts) == 2:
label, text = parts
if label in ["ham", "spam"]:
labels.append(0 if label == "ham" else 1)
texts.append(text)
df = pd.DataFrame({"text": texts, "label": labels})
X_train, X_test, y_train, y_test = train_test_split(
df["text"],
df["label"],
test_size=0.2,
random_state=42,
stratify=df["label"]
)
# =========================
# LOAD MODEL
# =========================
tokenizer = BertTokenizerFast.from_pretrained("model_bert")
model = BertForSequenceClassification.from_pretrained("model_bert")
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)
model.eval()
class SpamDataset(Dataset):
def __init__(self, texts, labels):
self.encodings = tokenizer(
list(texts),
truncation=True,
padding=True,
max_length=128
)
self.labels = labels.values
def __len__(self):
return len(self.labels)
def __getitem__(self, idx):
item = {k: torch.tensor(v[idx]) for k, v in self.encodings.items()}
item["labels"] = torch.tensor(self.labels[idx])
return item
test_dataset = SpamDataset(X_test, y_test)
test_loader = DataLoader(test_dataset, batch_size=8)
# =========================
# PREDICT
# =========================
y_true, y_pred = [], []
with torch.no_grad():
for batch in test_loader:
labels = batch.pop("labels")
batch = {k: v.to(device) for k, v in batch.items()}
outputs = model(**batch)
preds = torch.argmax(outputs.logits, dim=1)
y_true.extend(labels.numpy())
y_pred.extend(preds.cpu().numpy())
# =========================
# CLASSIFICATION REPORT
# =========================
print("\n=== CLASSIFICATION REPORT (BERT) ===")
print(classification_report(
y_true,
y_pred,
target_names=["Ham", "Spam"]
))
# =========================
# CONFUSION MATRIX
# =========================
cm = confusion_matrix(y_true, y_pred)
plt.figure(figsize=(5,4))
sns.heatmap(
cm,
annot=True,
fmt="d",
cmap="Blues",
xticklabels=["Ham", "Spam"],
yticklabels=["Ham", "Spam"]
)
plt.xlabel("Predicted")
plt.ylabel("Actual")
plt.title("Confusion Matrix - BERT")
plt.tight_layout()
plt.show()