RuntimeError while using Cuda

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.

In your cpu code, hidden_x seems having batch size 401. But that is not the case in your gpu code.

Yes, I realize that I have initialized hidden_x incorrectly, thanks for pointing that.
However, the GPU and CPU codes are almost the same, so why does the code run in the CPU mode and not the GPU mode?