Hm, I’m not sure how to interpret the error. It seem to come from a torch package? If your aim is to get your code running, I would try the following to simplify the model – makes debugging easier:


Name your class MyLSTM. It’s probably not an issue but there’s not need to risk naming problems :).


Use just one layer and unidirectional: self.lstm = nn.LSTM(500, 300,1,bidirectional=False). Otherwise lstm_out = lstm_out[:,-1,:] might yield unexpected results


Remove (comment out) self.dropout and self.act. nn.LSTM already use activation functions, an Dropout is not needed to get it working, “just” to improve the results.


Since you don’t show the code, I’m not sure if you set the pretrained word vectors. Anyway, again, they are not needed to get a working model for a start


The hidden_dim of you nn.LSTM is 300, but you define nn.Linear(200, 5). How do you get from 300 dimensions to 200 for the linear layer?


Can you change your forward() method to


def forward(self,x):
    print(x.shape)
    text_emb = self.word_embeddings(x)
    print(text_emb.shape)
    lstm_out, lstm_hidden = self.lstm(text_emb)
    lstm_out = lstm_out[:,-1,:]
    print(lstm_out.shape)
    output = self.dense(drop_out)
    return output