Model Details
Model Description
This is the model card of a RoBerta model that has been pushed on the Hub. This model card has been automatically generated.
- Developed by: Group 20
- Model type: Transformer model based on BERT
- Language(s) (NLP): Python
Model Sources
Note: Change the testing csv file to your own file and path! Run the evaluation code in GPU!
Evaluation Pipeline:
from transformers import RobertaTokenizer, RobertaForSequenceClassification from sklearn.metrics import accuracy_score, f1_score, classification_report import torch from datasets import load_dataset import pandas as pd
Load the model and tokenizer from the Hugging Face Hub
model_repo = "Group-20-cis-5190/news" # Replace with your model name model = RobertaForSequenceClassification.from_pretrained(model_repo) tokenizer = RobertaTokenizer.from_pretrained(model_repo)
Load the test dataset
df = pd.read_csv("/content/drive/MyDrive/test_data_random_subset.csv") # Adjust file path test_texts = df["title"].tolist() # Use "title" for the text test_labels = df["labels"].tolist() # Use "labels" for the target labels
Tokenize test data
tokenized_test = tokenizer(test_texts, padding=True, truncation=True, max_length=128, return_tensors="pt")
Create PyTorch DataLoader for evaluation
class TestDataset(torch.utils.data.Dataset): def init(self, encodings, labels): self.encodings = encodings self.labels = labels
def __len__(self):
return len(self.labels)
def __getitem__(self, idx):
item = {key: val[idx] for key, val in self.encodings.items()}
item["labels"] = self.labels[idx]
return {k: torch.tensor(v) for k, v in item.items()}
Prepare data for DataLoader
test_dataset = TestDataset(tokenized_test, test_labels) test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=32)
Define the evaluation function
def evaluate(model, dataloader, device): model.to(device) model.eval()
all_predictions = []
all_labels = []
with torch.no_grad():
for batch in dataloader:
inputs = {key: batch[key].to(device) for key in ["input_ids", "attention_mask"]}
labels = batch["labels"].to(device)
outputs = model(**inputs)
logits = outputs.logits
predictions = torch.argmax(logits, dim=-1)
all_predictions.extend(predictions.cpu().numpy())
all_labels.extend(labels.cpu().numpy())
return all_predictions, all_labels
Run evaluation
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") predictions, true_labels = evaluate(model, test_loader, device)
Compute metrics
accuracy = accuracy_score(true_labels, predictions) f1 = f1_score(true_labels, predictions, average="weighted") report = classification_report( true_labels, predictions, target_names=["NBC", "FoxNews"], # Adjust class names as needed digits=4 )
Print metrics
print("Evaluation Results") print(f"Accuracy: {accuracy:.4f}") print(f"F1 Score (Weighted): {f1:.4f}") print("\nClassification Report:") print(report)
- Downloads last month
- 15