leyjfk6
leyjfk6

Reputation: 9

How do I make a neural network class with fit and predict functions like with sklearn models, when my train and test data are different sizes?

I'm trying to make a neural network model that will answer a linear regression problem (I've already made a model using sklearn's LinearRegression and I'd like to compare the two). Ultimately I'd like to make a class with fit and predict functions, as with the models in sklearn, so that I can make a loop that will test all the models I am using in my project.

To do this I followed the code in the answer to this question: Writing a pytorch neural net class that has functions for both model fitting and prediction. With some modifications, here is what I have:

import torch
import torch.nn as nn
import torch.optim as optim

class MyNeuralNet(nn.Module):
    def __init__(self):
        super().__init__()
        self.layer1 = nn.Linear(2, 4, bias=True)
        self.layer2 = nn.Linear(4, 1, bias=True)
        self.loss = nn.MSELoss()
        self.compile_()

    def forward(self, x):
        x = self.layer1(x)
        x = self.layer2(x)
        return x.squeeze()

    def fit(self, x, y):
        x = torch.tensor(x.values, dtype=torch.float32)
        y = torch.tensor(y.values, dtype=torch.float32)
        losses = []
        for epoch in range(100):
            ## Inference
            res = self.forward(x)#self(self,x)
            loss_value = self.loss(res,y)

            ## Backpropagation
            loss_value.backward() # compute gradient
            self.opt.zero_grad()  # flush previous epoch's gradient
            self.opt.step()       # Perform iteration using gradient above

            ## Logging
            losses.append(loss_value.item())
        
    def compile_(self):
        self.opt = optim.SGD(self.parameters(), lr=0.01)
       
    def predict(self, x_test):
        self.eval()
        y_test_hat = self(x_test)
        return y_test_hat.detach().numpy()
        # self.train()

Note, you also need numpy, I just don't have it here because this code was put into a separate .py file.

Here is how I used the model, after importing my class:

model = MyNeuralNet()
X_train = # pandas dataframe with 1168 rows and 49 columns
y_train = # pandas dataframe with 1168 rows and 1 column
X_test = # pandas dataframe with 292 rows and 49 columns
model.fit(X_train, y_train)
pred = model.predict(X_test)
print(pred)

The error I got is RuntimeError: mat1 and mat2 shapes cannot be multiplied (1168x49 and 2x4) at the fit step. I understand this has to do with the parameters for the linear layers of my network. I think if I change my input size for the first linear layer to 49 and my output size for the second linear layer to 1168 then it will work for the fit step (or at least something like that, to match the sizes of the train data). However, my test data is of a different size and I'm pretty sure then the predict step won't work.

Is it possible to make a neural network class where the training and test data are of different sizes?

Upvotes: 0

Views: 65

Answers (1)

leyjfk6
leyjfk6

Reputation: 9

Yes, the input layer needs to be size 49. The output layer needs to be size 1. Then it'll work for both the train and test data.

Upvotes: -1

Related Questions