目标尺寸与输入尺寸不同-LSTM

时间:2019-08-12 18:46:23

标签: python deep-learning pytorch lstm

我对Pytorch还是比较陌生,并且一直在训练LSTM模型。一般情况下,任何对代码的反馈都将不胜感激。

训练模型时,我会收到以下警告

  

UserWarning:使用目标大小(torch.Size([4050,1,1]))   与输入大小(torch.Size([1]))不同。这可能会导致   由于广播而导致的错误结果。请确保他们拥有   大小相同。

有人可以帮助我解决这个问题吗?

我的目标是在包含多个特征的时间序列上训练模型。请参见下面的代码:

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()

数据加载器:


'''
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

0 个答案:

没有答案