-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassify_eval.py
More file actions
89 lines (66 loc) · 2.97 KB
/
classify_eval.py
File metadata and controls
89 lines (66 loc) · 2.97 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
from time import time
from typing import List, Tuple
import torch
from loguru import logger
from sklearn.metrics import classification_report, confusion_matrix
from tqdm.auto import tqdm
from transformers import AutoModelForSequenceClassification, AutoTokenizer
from data_loader import id2label, label2id, load_dataset
dataset = load_dataset("AutoModelForSequenceClassification")
# Load the model and tokenizer
MODEL_ID = "/teamspace/studios/this_studio/flan-t5-base-email-classification-test"
model = AutoModelForSequenceClassification.from_pretrained(MODEL_ID)
model.to("cuda") if torch.cuda.is_available() else model.to("cpu")
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
def classify(texts_to_classify: List[str]) -> List[Tuple[str, float]]:
"""Classify a list of texts using the model."""
# Tokenize all texts in the batch
start = time()
inputs = tokenizer(
texts_to_classify,
return_tensors="pt",
max_length=512,
truncation=True,
padding=True,
)
inputs = inputs.to("cuda" if torch.cuda.is_available() else "cpu")
# Get predictions
with torch.no_grad():
outputs = model(**inputs)
logger.debug(
f"Classification of {len(texts_to_classify)} examples took {time() - start} seconds"
)
# Process the outputs to get the probability distribution
logits = outputs.logits
probs = torch.nn.functional.softmax(logits, dim=-1)
# Get the top class and the corresponding probability (certainty) for each input text
confidences, predicted_classes = torch.max(probs, dim=1)
predicted_classes = (
predicted_classes.cpu().numpy()
) # Move to CPU for numpy conversion if needed
confidences = confidences.cpu().numpy() # Same here
# Map predicted class IDs to labels
predicted_labels = [id2label[class_id] for class_id in predicted_classes]
# Zip together the predicted labels and confidences and convert to a list of tuples
return list(zip(predicted_labels, confidences))
def eval():
"""Evaluate the model on the test dataset."""
predictions_list, labels_list = [], []
batch_size = 16 # Adjust batch size based GPU capacity
num_batches = len(dataset["test"]) // batch_size + (
0 if len(dataset["test"]) % batch_size == 0 else 1
)
progress_bar = tqdm(total=num_batches, desc="Evaluating")
for i in range(0, len(dataset["test"]), batch_size):
batch_texts = dataset["test"]["text"][i : i + batch_size]
batch_labels = dataset["test"]["label"][i : i + batch_size]
batch_predictions = classify(batch_texts)
predictions_list.extend(batch_predictions)
labels_list.extend([id2label[label_id] for label_id in batch_labels])
progress_bar.update(1)
progress_bar.close()
report = classification_report(labels_list, [pair[0] for pair in predictions_list])
print(report)
print(confusion_matrix(labels_list, [pair[0] for pair in predictions_list]))
if __name__ == "__main__":
eval()