-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
45 lines (35 loc) · 1.4 KB
/
test.py
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
import torch
from torch.utils.data import DataLoader
from transformers import BertTokenizer, BertModel
from model import SentimentClassifier
from sklearn.metrics import classification_report
from data import YelpDataset
# Load pre-trained BERT model and tokenizer
bert_model_name = 'bert-base-uncased'
tokenizer = BertTokenizer.from_pretrained(bert_model_name)
bert_model = BertModel.from_pretrained(bert_model_name)
batch_size = 32
test_file = 'dataset/test.json'
test_dataset = YelpDataset(test_file, tokenizer)
test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=True)
# Load the best model parameters
best_model_state = torch.load('best_combined_model.pth')
# Instantiate the combined model
model = SentimentClassifier(bert_model, rnn_hidden_size=256, num_classes=5)
model.load_state_dict(best_model_state)
model.eval()
# Testing
test_predictions = []
test_labels = []
with torch.no_grad():
for batch in test_loader:
input_ids = batch['input_ids']
attention_mask = batch['attention_mask']
label = batch['label']
rnn_output = model(input_ids, attention_mask)
_, predicted = torch.max(rnn_output, 1)
test_predictions.extend(predicted.cpu().numpy())
test_labels.extend(label.cpu().numpy())
# Calculate and print classification report
print("Classification Report on Test Set:")
print(classification_report(test_labels, test_predictions))