I am relatively new to Pytorch and have been training an LSTM model. Any feedback on code in general would be appreciated.
When I train the model I receive the following warning
UserWarning: Using a target size (torch.Size([4050, 1, 1])) that is different to the input size (torch.Size([1])). This will likely lead to incorrect results due to broadcasting. Please ensure they have the same size.
Can anyone help me with this problem?
My goal is to train a model on a time series containing multiple features. Please see the code below:
LSTM:
import torch
import torch.nn as nn
import pandas as pd
import numpy as np
from Tools.data_loader import CreateDataset

class LSTMModel(nn.Module):
    def __init__(self, input_dim, hidden_dim, num_layers, output_dim):
        super(LSTMModel, self).__init__()
        self.input_dim = input_dim
        self.hidden_dim = hidden_dim
        self.output_dim = output_dim
        self.num_layers = num_layers
        self.lstm = nn.LSTM(input_dim, hidden_dim, num_layers)
        self.forecast = nn.Linear(hidden_dim, output_dim)

    def forward(self, x):
       
        batch_size = 1
        h0 = torch.zeros(self.num_layers, batch_size, self.hidden_dim).requires_grad_()
        c0 = torch.zeros(self.num_layers, batch_size, self.hidden_dim).requires_grad_()

        out, (hn,cn) = self.lstm(x, (h0.detach(), c0.detach()))

        y_pred = self.forecast(out[-1].view(batch_size,-1))
        return y_pred.view(-1)

dataset = CreateDataset('Data/Bloomberg_PV_weather.csv', 'Datetime', 0.8, 'Power') #PLACEHOLDER
input_dim = dataset.num_features
hidden_dim = 50
num_layers = 1
output_dim = 1
num_epochs = 50

X_train = dataset.X_train
y_train = dataset.y_train
X_test = dataset.X_test
y_test = dataset.y_test

model = LSTMModel(input_dim, hidden_dim, num_layers, output_dim)
criterion = nn.MSELoss()
optimizer=torch.optim.Adam(model.parameters())

hist = np.zeros(num_epochs)


for epoch in range(num_epochs):
    optimizer.zero_grad()
    output=model(X_train)
    loss = criterion(output,y_train)
    if epoch % 100 == 0:
        print('Epoch ', epoch, 'Loss: ',loss.item())
    hist[epoch] = loss.item()
    loss.backward()
    optimizer.step()

Data Loader:
and here is my CreateDataset class:

'''
Loads data and creates train and test sets
'''
import torch
import math
import pandas as pd


class CreateDataset():
    def __init__(self, file, datetime_col, train_proportion, target):
        self.file = file
        self.datetime_col = datetime_col
        self.train_proportion = train_proportion
        self.target = target
        self.data = self.load_data()
        self.train_set, self.test_set = self.split_data()
        self.X_train, self.y_train,self.X_test, self.y_test, self.num_features = self.reshape_data()

    def load_data(self):
        '''
        Reads in data
        '''
        data = pd.read_csv(self.file, header=0)
        data.drop(columns=self.datetime_col, inplace=True)
        return data
    
    def split_data(self):
        '''
        Creates test and train sets
        '''
        train_length = math.ceil(len(self.data)*self.train_proportion)
        train_set = self.data[0:train_length]
        test_set = self.data[train_length:]
        
        return train_set, test_set

    def reshape_data(self):
        '''
        Splits datasets into X and y sets and reshapes into 3D tensor
        '''
        num_features = (len(self.test_set.columns)-1)
        y_train = torch.tensor(self.train_set[self.target].values).float()
        X_train = torch.tensor(self.train_set.drop(columns=self.target).values).float()
        y_test = torch.tensor(self.test_set[self.target].values).float()
        X_test = torch.tensor(self.test_set.drop(columns=self.target).values).float()
    
        X_train = X_train.view(-1,1,num_features)
        y_train = y_train.view(-1,1,1)
        X_test = X_test.view(-1,1,num_features)
        y_test = y_test.view(-1,1,1)

        return X_train, y_train, X_test, y_test, num_features