I have the following LSTM:
self.lstm_x = nn.LSTM(27, 50, num_layers=1, batch_first=True)
with hidden state initialization:
hidden_x = (autograd.Variable(torch.randn(1, 1, 50)), autograd.Variable(torch.randn((1, 1, 50))))
Execution of the statement: feat_x, _ = self.lstm_x(X, self.hidden_x)
gives the following error:
RuntimeError: Expected hidden size (1, 401, 50), got (1, 1, 50)
.
Here, X.size
= torch.Size([401, 20, 27])
and self.hidden_x[0].size()
= torch.Size([1, 1, 50])
. 401
is the number of samples in the dataset and batch_size = 2046
.
If I execute the same code without Cuda, everything runs fine. To run with Cuda I did the following changes:
-
net
->net.cuda()
-
nn.CrossEntropyLoss()
->nn.CrossEntropyLoss().cuda()
- For each tensor,
x
->x.cuda()
-
hidden_x = (autograd.Variable(torch.randn(1, 1, 50)), autograd.Variable(torch.randn((1, 1, 50))))
->hidden_x = (autograd.Variable(torch.randn(1, 1, 50).cuda()), autograd.Variable(torch.randn(1, 1, 50).cuda()))
Thanks for any help.