How to test saved fine tuned bert model?
Hello, I wrote following code to make a chatbot and I fine tuned some data. However, I am not sure how I should run it because my test code returned empty results. The fine tuned data consists of questions and answers. Any kind of help would be nice, thanks.import torch
from torch.utils.data import Dataset, DataLoader
import transformers
from transformers import BertTokenizer, BertForQuestionAnswering, AdamW, get_linear_schedule_with_warmup
import pandas as pd
import re

transformers.logging.set_verbosity_error()



# Sample questions and answers
df = pd.read_csv('counsel_chat.csv')

questions = df['questionText'].tolist()
answers = df['answerText'].tolist()

def clean_text(text):
    '''Clean text by removing unnecessary characters and altering the format of words.'''
    text = str(text)
    text = text.lower()
    # ... (your cleaning logic)
    text = " ".join(text.split())
    return text

# Clean the data
clean_questions = [clean_text(question) for question in questions]
clean_answers = [clean_text(answer) for answer in answers]

# Create a dataset class
class QADataset(Dataset):
    def __init__(self, questions, answers, tokenizer, max_length=128):
        self.questions = questions
        self.answers = answers
        self.tokenizer = tokenizer
        self.max_length = max_length

    def __len__(self):
        return len(self.questions)

    def __getitem__(self, idx):
        question = str(self.questions[idx])  # Ensure the question is a string
        encoding = self.tokenizer(
            question,
            self.answers[idx],
            max_length=self.max_length,
            padding="max_length",
            truncation=True,
            return_tensors="pt"
        )
        return {
            "input_ids": encoding["input_ids"].squeeze(),
            "attention_mask": encoding["attention_mask"].squeeze(),
            "start_positions": torch.tensor(0),  # Modify as needed
            "end_positions": torch.tensor(0)  # Modify as needed
        }

num_epochs = 10  # Adjust as needed

# Create dataset and dataloader using cleaned data
tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")
dataset = QADataset(clean_questions, clean_answers, tokenizer)
dataloader = DataLoader(dataset, batch_size=16, shuffle=True)

# Load pre-trained BERT model for question answering
model = BertForQuestionAnswering.from_pretrained("bert-base-uncased")

# Set up optimizer and learning rate scheduler
optimizer = AdamW(model.parameters(), lr=5e-5)
scheduler = get_linear_schedule_with_warmup(optimizer, num_warmup_steps=0, num_training_steps=len(dataloader) * num_epochs)

# Training loop
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)

for epoch in range(num_epochs):
    model.train()
    for batch in dataloader:
        # Move batch to device
        batch = {key: value.to(device) for key, value in batch.items()}

        # Forward pass
        outputs = model(**batch)
        loss = outputs.loss

        # Backward pass
        loss.backward()

        # Update parameters and scheduler
        optimizer.step()
        scheduler.step()
        optimizer.zero_grad()

# Save the fine-tuned model
tokenizer.save_pretrained("fine_tuned_model")
model.save_pretrained("fine_tuned_model")