I am new in coding pyton/pytorch… and I have a hard time in using pytorch lstm layers for predicting a high dimensional sequence. Let’s say I have a matrix of size 5* 10 of integer numbers as my data. This means sequence length is 10 and each point of my sequence has 5 features.
My goal is to build a network to predict this sequence. Let say I am going to feed two pints of this sequence (25) at each time to my network and network return me same size (25) just one step ahead in the sequence. Therefore data is something like
X=torch.LongTensor(5,10).random_(0, 10)) # 5 number of features and 10 size of sequence
My main problem is how to feed this data to my model? I find following code somewhere and change it a little bit. I read many posts about this issue but still confuse how to feed my data to this model
class LSTM(nn.Module):
def __init__(self, input_dim, hidden_dim, batch_size, output_dim=1,num_layers=2):
super(LSTM, self).__init__()
self.input_dim = input_dim
self.hidden_dim = hidden_dim
self.batch_size = batch_size
self.num_layers = num_layers
# LSTM layer
self.lstm = nn.LSTM(self.input_dim, self.hidden_dim, self.num_layers)
# output layer
self.linear = nn.Linear(self.hidden_dim, output_dim)
def init_hidden(self):
return (torch.zeros(self.num_layers, self.batch_size, self.hidden_dim),
torch.zeros(self.num_layers, self.batch_size, self.hidden_dim))
def forward(self, input):
lstm_out, self.hidden = self.lstm(input.view(len(input), self.batch_size, -1))
y_pred = self.linear(lstm_out[-1].view(self.batch_size, -1))
return y_pred.view(-1)
model = LSTM(lstm_input_size, h1, batch_size=num_train, output_dim=output_dim, num_layers=num_layers)
loss_fn = torch.nn.MSELoss(size_average=False)
optimiser = torch.optim.Adam(model.parameters(), lr=learning_rate)
# Training
for t in range(num_epochs):
model.zero_grad()
model.hidden = model.init_hidden()
# Forward pass
y_pred = model(x_train)
loss = loss_fn(x_train, x_train)
optimiser.zero_grad()
loss.backward()
optimiser.step()
Any help appreciated. Also if have any other suggestion to predict such a sequence please let me know. thank you