Time series prediction (many to many lstm) basic example need help!

Hello, I am new to pytorch and have some questions regarding how to create a many-to-many lstm model. I am trying to predict the next number (x_t+1) in a sequence given an input sequence of integers like
[0, 1, 2, 3, 2, 1] - data from x_t-5 to x_t.

So example [0, 1, 2, 3, 2, 1] -> lstm model -> should give [1, 2, 3, 2, 1, 0]

So when I run the model with input sequence from x_t-5 to x_t, i expect the model to train and give an output sequence from x_t-4 to x_t+1.

Can anybody give me an example or fix my code? As i couldn’t find anywhere online with a simple example for such model that i am creating.

Or tell me what is wrong with my code? or my understanding of pytorch lstm?

May I also ask if what exactly should be the hidden_size for my model?

Below is my source code which does not run.

Any help will be greatly appreciated!

import torch
from torch.autograd import Variable
import torch.nn as nn

torch.manual_seed(1)

x_train = [ [[0], [1], [2], [3], [2], [1]],
             [[1], [2], [3], [2], [1], [0]],
             [[2], [3], [2], [1], [0], [1]],
             [[3], [2], [1], [0], [1], [2]],
             [[2], [1], [0], [1], [2], [3]] ]

y_train = [[[1], [2], [3], [2], [1], [0]],
             [[2], [3], [2], [1], [0], [1]],
             [[3], [2], [1], [0], [1], [2]],
             [[2], [1], [0], [1], [2], [3]],
             [[1], [0], [1], [2], [3], [2]]]

inputs = Variable(torch.Tensor(x_train))
labels = Variable(torch.Tensor(y_train))

input_size =  1
hidden_size = 32
batch_size = 5
sequence_length = 6
num_layers = 1

class LSTM(nn.Module):
    def __init__(self, input_size, hidden_size, num_layers, sequence_length):
        super(LSTM, self).__init__()

        self.num_layers = num_layers
        self.input_size = input_size
        self.hidden_size = hidden_size
        self.sequence_length = sequence_length

        self.lstm = nn.LSTM(input_size=input_size, hidden_size=hidden_size, batch_first=True)

    def forward(self, x):
        # (num_layers * num_directions, batch, hidden_size)
        h_0 = (Variable(torch.zeros(self.num_layers, x.size(0), self.hidden_size)) for _ in range(self.hidden_size))
        # Reshape input
        x.view(x.size(0), self.sequence_length, self.input_size)

        out, _ = self.lstm(x, h_0)

        return out

lstm = LSTM(input_size, hidden_size, num_layers, sequence_length)
print (lstm)

criterion = torch.nn.MSELoss()
optimizer = torch.optim.Adam(lstm.parameters(), lr=0.1)

# Train the model
for epoch in range(100):
    loss = 0
    outputs = lstm(inputs)
    optimizer.zero_grad()
    loss = criterion(outputs, labels)
    loss.backward()
    optimizer.step()
    print ("epoch : %d, loss: %1.3f" %(epoch+1, loss.data[0]))

print ("Learning finished!")
2 Likes

I believe your x_train should look more like
x_train = ( [[[0]], [[1]], [[2]], [[3]], [[2]], [[1]]],
[[[1]], [[2]], [[3]], [[2]], [[1]], [[0]]],
[[[2]], [[3]], [[2]], [[1]], [[0]], [[1]]],
[[[3]], [[2]], [[1]], [[0]], [[1]], [[2]]],
[[[2]], [[1]], [[0]], [[1]], [[2]], [[3]]] )

@bsalanon
You will get error message:
RuntimeError: input must have 3 dimensions, got 4