def forward(self, x):
x = self.relu(self.input(x))
x = self.dropout(x)
x = self.relu(self.hidden1(x))
x = self.dropout(x)
x = self.relu(self.hidden2(x))
x = self.dropout(x)
x = self.output(x)
return x
It looks like the main issue is that you’re using a regression loss function (MSELoss) for a classification task. Since your model is predicting a continuous value, comparing this directly with the ground truth class labels using yHat == y will almost always result in a false condition, leading to 0.0 accuracy.
Change the output layer to output 2 values instead of 1 (assuming binary classification):
Make sure to adjust the output layer size if you have more than two classes, and also ensure that your target labels are in the format [0, 1, 2, ..., num_classes-1] for compatibility with CrossEntropyLoss.
Based on the data you provided, it seems that you are using a regression model to predict the sale price of a house. However, you are calculating the accuracy as if it were a classification problem. This is probably the reason you are getting an accuracy of 0.0.
In regression tasks, accuracy is not the right metric to measure the performance of your model. Instead, you should use metrics such as Mean Squared Error (MSE), Root Mean Squared Error (RMSE), Mean Absolute Error (MAE), or R-squared (Coefficient of Determination).
To calculate these metrics, you can use sklearn.metrics. Try this code block
import torch
import torch.nn as nn
import numpy as np
from sklearn.metrics import mean_squared_error
import copy
class TabularModel(nn.Module):
def __init__(self):
super().__init__()
self.input = nn.Linear(13, 250)
self.hidden1 = nn.Linear(250, 250)
self.hidden2 = nn.Linear(250, 500)
self.output = nn.Linear(500, 1)
self.relu = nn.ReLU()
self.dropout = nn.Dropout(p=0.25)
def forward(self, x):
x = self.relu(self.input(x))
x = self.dropout(x)
x = self.relu(self.hidden1(x))
x = self.dropout(x)
x = self.relu(self.hidden2(x))
x = self.dropout(x)
x = self.output(x)
return x
net = TabularModel()
optimizer = torch.optim.SGD(net.parameters(), lr=0.01, momentum=0.9)
loss_func = nn.MSELoss()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
num_epochs = 10
def train_model():
best_model = {"RMSE": float("inf"), "net": None}
losses = np.zeros(num_epochs)
train_RMSEs = []
for epoch in range(num_epochs):
net.to(device)
net.train()
batch_losses = []
batch_RMSEs = []
for X, y in train_dataloaders:
X = X.to(device)
y = y.to(device)
y_hat = net(X)
loss = loss_func(y_hat, y)
optimizer.zero_grad()
loss.backward()
optimizer.step()
batch_losses.append(loss.item())
y_hat_rounded = y_hat.detach().cpu().numpy().round()
y_cpu = y.cpu().numpy()
batch_RMSE = mean_squared_error(y_cpu, y_hat_rounded, squared=False)
batch_RMSEs.append(batch_RMSE)
train_RMSEs.append(np.mean(batch_RMSEs))
losses[epoch] = np.mean(batch_losses)
if train_RMSEs[-1] < best_model["RMSE"]:
best_model["RMSE"] = train_RMSEs[-1]
best_model["net"] = copy.deepcopy(net.state_dict())
return train_RMSEs, losses, best_model