I am getting my accuracy '0.0' at each epoch. Anybody help me to resolve this. Thankyou in advance

class tabular_model(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 = tabular_model()
optimizer = torch.optim.SGD(net.parameters(), lr=0.01, momentum = 0.9)
loss_func = nn.MSELoss()

def functiontotrain():

theBestModel = {“Accuracy”:0, “net”:None}
losses = np.zeros(numepochs)
trainAcc = []
testAcc = []

for epochi in range(numepochs):

net.to(device)
net.train()

for X,y in train_dataloaders:

  X = X.to(device)
  y = y.to(device)

  batchLoss = []
  batchAcc = []

  yHat = net(X)
  loss = loss_func(yHat, y)

  optimizer.zero_grad()
  loss.backward()
  optimizer.step()

  batchLoss.append(loss.item())
  batchAcc = (100*torch.mean((yHat == y).float()).item())

trainAcc.append(np.mean(batchAcc))

losses[epochi] = np.mean(batchLoss)

if trainAcc[-1] > theBestModel['Accuracy']:
  theBestModel['net'] = trainAcc[-1].item()

  theBestModel['net'] = copy.deepcopy(net.state_dict())

return trainAcc, losses, theBestModel

Accuracy graph:
image

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.

  1. Change the output layer to output 2 values instead of 1 (assuming binary classification):
self.output = nn.Linear(500, 2)
  1. Use nn.CrossEntropyLoss() as your loss function:
loss_function = nn.CrossEntropyLoss()

This post was flagged by the community and is temporarily hidden.

Try

class tabular_model(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, 2)  # Change output to match number of classes
        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 = tabular_model()
optimizer = torch.optim.SGD(net.parameters(), lr=0.01, momentum=0.9)
loss_func = nn.CrossEntropyLoss()  # Use CrossEntropyLoss for classification

def functiontotrain():
    theBestModel = {"Accuracy": 0, "net": None}
    losses = np.zeros(numepochs)
    trainAcc = []
    testAcc = []

    for epochi in range(numepochs):
        net.to(device)
        net.train()

        for X, y in train_dataloaders:
            X = X.to(device)
            y = y.to(device)

            batchLoss = []
            batchAcc = []

            yHat = net(X)
            loss = loss_func(yHat, y)

            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

            batchLoss.append(loss.item())
            _, preds = torch.max(yHat, 1)  # Get class predictions
            batchAcc = (100 * torch.mean((preds == y).float()).item())  # Calculate accuracy based on class predictions

            trainAcc.append(np.mean(batchAcc))

        losses[epochi] = np.mean(batchLoss)

        if trainAcc[-1] > theBestModel['Accuracy']:
            theBestModel['net'] = trainAcc[-1].item()
            theBestModel['net'] = copy.deepcopy(net.state_dict())

    return trainAcc, losses, theBestModel

This post was flagged by the community and is temporarily hidden.

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.

This post was flagged by the community and is temporarily hidden.

Is this a double post from here from another account?

It seems so. @ptrblck

This post was flagged by the community and is temporarily hidden.

This post was flagged by the community and is temporarily hidden.

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

This post was flagged by the community and is temporarily hidden.

This post was flagged by the community and is temporarily hidden.

I have followed your suggestions and finally my model started to train. But i am getting hude accuracy ald losses values.

The following represents the train accuracy and losses:

@upendra @luci I would recommend using a single account and delete the other one as our system flags your posts as spam.