Need help with a simple FFN model

Hello,

I am new to pytorch. I have a FFN network below which takes in a dataset with 12 variables and predicts a binary value (logistic regression). Somehow they accuracy is always coming out to be same. I think the problem is somwhere in the predict function down south. Any help is appreciated :wink:

import pandas as pd
import numpy as np
 
#getting the data
raw_data = pd.read_csv("data.csv")
raw_data.head()
 
#Removing the unnecessary columns
data = raw_data.drop(["RowNumber", "CustomerId", "Surname"], axis= 1)
data.head()
 
#Changing the geography and gender columns to numbers
data = pd.get_dummies(data=data, columns=["Gender", "Geography"], drop_first=True)
data.head()
 
#Creating the X and Y values
data_x = data.loc[:, data.columns != "Exited"]
data_y = data.loc[:, data.columns == "Exited"]
 
#making sure everything is ok
print("The number of rows in X are {}".format(len(data_x)))
print("The number of rows in Y are {}".format(len(data_y)))
 
#Create the numpy arrays
data_id_x = data_x.iloc[:,:].values
data_id_y = data_y.iloc[:,:].values
print("The number of rows in X are {}".format(len(data_x)))
print("The number of rows in Y are {}".format(len(data_y)))
 
#print 1st 5 rows
print(data_id_x[0:5])
print(data_id_y[0:5])
 
#Normalize the values or feature scaling
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
data_id_x = scaler.fit_transform(data_id_x)
 
#Print the 1st 5 rows
print(data_id_x[:5])
 
#divide the dataset into test and train set
from sklearn.model_selection import train_test_split
X_train, X_test, Y_train, Y_test = train_test_split(data_id_x, data_id_y, test_size = 0.15)
 
'''check the type of your tensor
if its float64 type it would be converted to doubletensor which cannot
be used by the pytorch nn class. So we will convert array from
float64 to float32. This will make the new tensor floattensor :) '''
print("X_Train", X_train.dtype, len(X_train))
print("Y_Train", Y_train.dtype, len(Y_train))
print("X_Test", X_test.dtype, len(X_test))
print("Y_Test", Y_test.dtype, len(Y_test))
 
#Try to convert numpy to torch Tensors
import torch
import torch.utils.data as utils
X_train_T = torch.from_numpy(X_train.astype("float32", casting="same_kind"))
X_test_T = torch.from_numpy(X_test.astype("float32", casting="same_kind"))
Y_train_T = torch.from_numpy(Y_train.astype("float32"))
Y_test_T = torch.from_numpy(Y_test.astype("float32"))
#now lets create the training tensor dataset
X_Dataset = utils.TensorDataset(X_train_T, Y_train_T)
Y_Dataset = utils.TensorDataset(X_test_T, Y_test_T)
 
#now we need to make it into a training and test dataloader
#setup a batch_size
batch_size = 1
train_loader = utils.DataLoader(dataset=X_Dataset, batch_size = batch_size, shuffle = True)
test_loader = utils.DataLoader(dataset=Y_Dataset, batch_size=batch_size, shuffle=True)
 
#Check if the test and the train loaders are iterable
import collections
print(isinstance(train_loader, collections.Iterable))
print(isinstance(test_loader, collections.Iterable))
 
# ## Data is prepared. Now we will work on our pytorch stuff
 
import torch.nn as nn
from torch.autograd import Variable

#Define our ANN class
class FFN(nn.Module):
    
    def __init__(self, input_size, hidden_size, output_size):
        super(FFN, self).__init__()
        self.FC1 = nn.Linear(in_features=input_size, out_features=hidden_size)
        self.relu1 = nn.ReLU()
        self.FC2 = nn.Linear(in_features=hidden_size, out_features=hidden_size)
        self.relu2 = nn.ReLU()
        self.FC3 = nn.Linear(in_features=hidden_size, out_features=output_size)
    
    def forward(self, x):
        
        out = self.relu1(self.FC1(x))
        out = self.relu2(self.FC2(out))
        return self.FC3(out)
        
 
#setup the model, loss and optimizer
model = FFN(input_size=11, hidden_size=16, output_size=1)
critereon = nn.BCEWithLogitsLoss()
optimizer = torch.optim.SGD(params=model.parameters(), lr = 0.001)
 
check = 0 
for e in range(10):
    
    for x, y in train_loader:
        
        X = Variable(x)
        Y = Variable(y)
        
        optimizer.zero_grad()
        
        results = model(X)
        loss = critereon(results, Y)
        
        loss.backward()
        optimizer.step()
        
        check += 1
        
        if check%2000 == 0:
            total = 0.0
            correct = 0.0
                        
            for x, y in test_loader:
            
                output = model(Variable(x))
                
                _, predict = torch.max(output.data, -1)
                
                total += len(y)
                correct += (y == predict.float()).sum()
            print("iter: {}, Loss : {}, Accuracy: {}".format(check, loss.data[0], correct/total) )

RRR: iter: 2000, Loss : 0.011456181295216084, Accuracy: 0.8073333333333333
RRR: iter: 4000, Loss : 0.03539063036441803, Accuracy: 0.8073333333333333

Thanks
Sid