I am trying to use pack_padded_sequence function to feed the output of the function into a LSTM layer as following:
class LSTMClassification(nn.Module): def __init__(self, input_dim, hidden_dim, target_size): super(LSTMClassification, self).__init__() self.hidden_dim = hidden_dim self.lstm = nn.LSTM(input_dim, hidden_dim, batch_first=True, num_layers=3, dropout=0.3, bidirectional=True) # The linear layer that maps from hidden state space to tag space self.fc = nn.Linear(hidden_dim*2, target_size) def forward(self, input_): input_lengths = torch.LongTensor([torch.max(input_[i, :].data.nonzero()) + 1 for i in range(input_.size())]) # Then pack the sequences packed_input = nn.utils.rnn.pack_padded_sequence(input_, input_lengths.cpu().numpy(), batch_first=True, enforce_sorted=False) lstm_out, (h, c) = self.lstm(packed_input) logits = self.fc(lstm_out[:,-1]) return logits
However, I am getting an error stating “start (384) + length (8) exceeds dimension size (384).”. I couldn’t debug what is the cause of the error. I did a small simulation code to reproduce the error as following. The error can be seen when the following code is ran.
model = LSTMClassification(76, hidden_dim=8, target_size=1) input_ = torch.randn(8, 48, 76) model(input_)
The issue seems similar to I used pack_padded_sequence() and put in lstm layer, but I got start () + length () exceeds dimension size (). error but I don’t see any clear answer on how the problem was solved in that link.