Hi PyTorch users,
I’m still quite new to pytorch, but I’ve spent on this problem sometime already.
So I’ve got this demo model of LSTM which works on batches.
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
self.lstm = nn.LSTM(self.input_dim, self.hidden_dim, self.num_layers)
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(-1, self.batch_size, self.input_dim))
y_pred = self.linear(lstm_out[-1].view(b_size, -1))
return y_pred.view(-1)
I tried it on some sine signal and looks like it learns okay.
But I give as input tensor batches of batch_size length. Now I was wondering how to achieve similar to keras method model.predict(X_test) that I can feed to the LSTM model only single example. Any simple solution?