0

I'm developing a project by myself and I need help. I just created a dataset of questions and answers. Like this;

    Questions                                                    Answer
Where is my laptop?                                 On the work table in the living room.
Where is the bakery?                                The oven is under the counter in the kitchen.
How can ı go to Bedroom from the living room?       After exiting the living room, if we go straight to the left, the first room on our left is the bedroom.

In this way, I was able to generate 400 data. I encoded the data but I don't know how to train it. Can you help me to train this? How can I train the data after encoding it? Or do you have any idea about this dataset?

import re
import pandas as pd
from sentence_transformers import SentenceTransformer
import numpy as np
import torch
import torch.nn as nn


df = pd.read_excel('Book.xlsx')

questions = df['Questions'].tolist()
answers = df['Answer'].tolist()

lowercased_questions = []
lowercased_answers = []

for question in questions:
  lowercased_questions.append(question.lower())

for answer in answers:
  lowercased_answers.append(answer.lower())


cleaned_questions = []
cleaned_answers = []

for lowercased_questions in lowercased_questions:
  cleaned_questions.append(re.sub(r'[^\w\s]', '', lowercased_questions))

for lowercased_answers in lowercased_answers:
  cleaned_answers.append(re.sub(r'[^\w\s]', '', lowercased_answers))


model = SentenceTransformer('bert-base-nli-mean-tokens')

questions_encoded = model.encode(questions)
answers_encoded = model.encode(answers)

questions_tensor = torch.from_numpy(questions_encoded)
answers_tensor = torch.from_numpy(answers_encoded)

I tried this way but my accuracy is so low (0.012626262626262626)

I know it is about my data but I want to know if there is any other way.

# Define the model
class DNN(nn.Module):
    def __init__(self, input_size, hidden_size, output_size):
        super().__init__()
        self.fc1 = nn.Linear(input_size, hidden_size)
        self.fc2 = nn.Linear(hidden_size, hidden_size)
        self.fc3 = nn.Linear(hidden_size, output_size)
        self.relu = nn.ReLU()
        self.softmax = nn.Softmax(dim=1)  # Add a softmax layer
        
    def forward(self, x):
        x = self.fc1(x)
        x = self.relu(x)
        x = self.fc2(x)
        x = self.relu(x)
        x = self.fc3(x)
        x = self.softmax(x)  # Apply the softmax function
        return x

# Initialize the model
input_size = questions_tensor.size(-1)
output_size = answers_tensor.size(-1)
hidden_size = 32
model = DNN(input_size, hidden_size, output_size)

# Define the loss function and the optimizer
loss_fn = torch.nn.CrossEntropyLoss()  # Use the cross-entropy loss functionloss_fn = torch.nn.CrossEntropyLoss()  # Use the cross-entropy loss function
optimizer = torch.optim.Adam(model.parameters())

# Train the model
for epoch in range(500):
    # Forward pass
    logits = model(questions_tensor)
    loss = loss_fn(logits, answers_tensor)

    # Backward pass
    loss.backward()
    optimizer.step()
    optimizer.zero_grad()

model = torch.nn.Sequential(
    torch.nn.Linear(questions_tensor.size(-1), answers_tensor.size(-1)),  # change the number of input units to match the number of rows in the questions tensor
    torch.nn.ReLU(),
    torch.nn.Linear(answers_tensor.size(-1), 1)
)

loss_fn = torch.nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters())

for epoch in range(500):
    # Forward pass
    logits = model(questions_tensor)
    loss = loss_fn(logits, answers_tensor)

    # Backward pass
    loss.backward()
    optimizer.step()
    optimizer.zero_grad()


# Load the SentenceTransformer model
sentence_transformer = SentenceTransformer('bert-base-nli-mean-tokens')

# Encode the input question using the SentenceTransformer model
input_question = "I wanna go bathroom"
input_question_encoded = sentence_transformer.encode([input_question])

# Convert the NumPy array to a PyTorch tensor
input_question_tensor = torch.from_numpy(input_question_encoded)

# Make a prediction using the PyTorch model
prediction = model(input_question_tensor)

# Print the prediction
print(prediction)

# Make a prediction using the PyTorch model
prediction = model(input_question_tensor)

# Get the index of the highest prediction
prediction_index = torch.argmax(prediction).item()

# Get the corresponding answer from the list of answers
prediction_string = answers[prediction_index]

# Print the prediction
print(prediction_string)


# Convert the encoded answers into a tensor
answers_tensor = torch.from_numpy(answers_encoded)

# Initialize a counter for the number of correct predictions
correct_predictions = 0

# Iterate over the list of questions
# Iterate over the list of questions
for question, answer in zip(questions, answers):
    # Encode the question using the SentenceTransformer model
    question_encoded = sentence_transformer.encode([question])
    
    # Convert the encoded question into a tensor
    question_tensor = torch.from_numpy(question_encoded)
    
    # Make a prediction using the PyTorch model
    prediction = model(question_tensor)
    
    # Get the index of the highest prediction
    prediction_index = torch.argmax(prediction).item()
    
    # Get the corresponding answer from the list of answers
    prediction_string = answers[prediction_index]
    
    # Check if the prediction is correct
    if prediction_string == answer:
        # Increment the counter for correct predictions
        correct_predictions += 1

# Calculate the accuracy of the model
accuracy = correct_predictions / len(questions)

# Print the accuracy
print(accuracy)
starball
  • 20,030
  • 7
  • 43
  • 238

0 Answers0