Fine tuning gpt2 for question answering
Hello, I have a code, I do not know what else I need to configure so that I can get answers to my questions. the answers are wrong. please help me to make the model answer the questions I have trained. the end goal is to run this as a tech support service so that the trained model takes care of the easy stuff.!pip3 install transformersimport torchfrom transformers import GPT2Tokenizer, GPT2LMHeadModel, AdamWDefine the questions and answersqa_pairs = [[‘Q: How to delete the account A: IOS:  Go to settings on the app, click on Edit, Click on Logout, Delete the Account Android: Go to settings, Click on the three dots, Click on Logout, Delete the Account.’],[‘Q: How to deactivate my account A: IOS:  Go to settings on the app, click on Edit, Click on Logout, Delete the Account Android: Go to settings, Click on the three dots, Click on Logout, Delete the Account.’],[‘Q: How i can remove my nmbr A: IOS:  Go to settings on the app, click on Edit, Click on Logout, Delete the Account Android: Go to settings, Click on the three dots, Click on Logout, Delete the Account.’],[‘Q: How to delete an account A: IOS:  Go to settings on the app, click on Edit, Click on Logout, Delete the Account Android: Go to settings, Click on the three dots, Click on Logout, Delete the Account.’]]Load the pre-trained GPT-2 model and tokenizermodel = GPT2LMHeadModel.from_pretrained(‘gpt2’)tokenizer = GPT2Tokenizer.from_pretrained(‘gpt2’)text = “”for question in qa_pairs:text += f"{question} {tokenizer.eos_token}"Encode the text using the tokenizerinput_ids = tokenizer.encode(text, add_special_tokens=True, return_tensors=‘pt’)Define the optimizer and learning rate scheduleroptimizer = AdamW(model.parameters(), lr=5e-5)scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=1, gamma=0.9)Fine-tune the modeldevice = torch.device(“cuda” if torch.cuda.is_available() else “cpu”)model.to(device)model.train()for epoch in range(15):optimizer.zero_grad()loss = model(input_ids.to(device), labels=input_ids.to(device))[0]loss.backward()optimizer.step()scheduler.step()if epoch % 100 == 0:
    print(f"Epoch {epoch}, Loss {loss.item()}")Generate responses to new questionsmodel.eval()def generate_response(question):# Encode the question using the tokenizerinput_ids = tokenizer.encode(question + " <|question|>", add_special_tokens=False, return_tensors=‘pt’).to(device)# Generate the answer using the model
sample_output = model.generate(input_ids, do_sample=True, max_length=100, top_k=20, top_p=1.0)

# Decode the generated answer using the tokenizer
answer = tokenizer.decode(sample_output[0], skip_special_tokens=True)

# Split the generated answer into individual sentences
sentences = answer.split('. ')

# Look for the sentence that contains the answer to the question
for sentence in sentences:
    if question in sentence:
        return sentence

# If no sentence contains the answer, return the full generated answer
return answerExample usagequestion = “How to delete an account”response = generate_response(question)print(f"{question}\n {response}")Answer: How to delete an accountHow to delete an account <|question|>This tool allows you to:Remove an accountDelete an account by entering an invalid passwordDelete an account by entering an invalid passwordUnblock an accountUnblock an account by entering an invalid passwordUnblock an account by entering an invalid passwordUnblock an account by entering an invalid passwordUnblock an account by entering an invalid password or by entering an invalid password
2023-04-07T10:05:09Z
Hi, I don’t have an answer but I am about to tackle a similar problem. Do you have any advice, how did you make it work?
2023-09-05T07:28:34Z
My task is the same , how did you solved your problem. I have tried everything
2024-03-29T09:12:30Z
The format of gpt input should be the same during the inference and trainyou  have for training  the format   Q: question text  A: IOS: answer textfor generation it should be like  Q: question text  A: IOS: