josmi9966:

The Linear layer requires a tensor that has the batch instances in the first dimension, but the LSTM returns the last hidden state as shape (num_layers*isbidirectional), batchsize, hiddensize, (where isbidirectional is 2 if bidirectional, otherwise 1) even if batch_first=True


I’m replying because I run into the same problem and I can’t really understand that one would be forced to use things like h_n.transpose(0,1),contiguous(),view(batchsize, -1) or input_to_cl = out.permute(1, 0, 2).view(-1, self.hidden_dim*len(x))` (suggested in another post) which are quite obscure to me. It also sounds like super cumbersome for a framework such as pytorch, so I did a test :
test = torch.tensor([[[1,2,3]],[[1,2,3]]]).type(torch.FloatTensor)
#tensor of size ([2,1,6]), 2 beeing batch dimension.
lstm = nn.LSTM(3,6,batch_first = True)
lstm(test)[0].size() 

outputs torch.Size([2, 1, 6]), which means that using batch_first = True in the input of the LSTM instance makes it return an output also with batch first, which solves the issue.