RuntimeError: input must have 2 dimensions, got 3
I am trying to run a sequence of bigrams into an LSTM. This involves:

2.Padding the sequences using pad_sequence

3.Inputting the padded sequences in the embedding layer

4.Packing the output of the embedding layer

5.Inserting the pack into the LSTM.

class LSTMClassifier(nn.Module):
    # LSTM initialization
    def __init__(self, embedding_dim=32, hidden_dim=50, vocab_size=7138, label_size=2, static_size, batch_size=32):
        super(LSTMClassifier, self).__init__()
        # Initializing batch size
        self.batch_size = batch_size
        # Setting the hidden layer dimension of the LSTM
        self.hidden_dim = hidden_dim
        # Initializing the embedding layer
        self.embeddings = nn.Embedding(vocab_size, embedding_dim-2)
        # Initializing the LSTM layer with one hidden layer 
        self.lstm = nn.LSTM(((embedding_dim*vocab_size)+static_size), hidden_dim, num_layers=1, batch_first=True)
        # Initializing linear linear that takes the hidden layer output
        self.hidden2label = nn.Linear(hidden_dim, label_size)
        # Initializing the hidden layer
        self.hidden = self.init_hidden()

    # Defining the hidding state of the LSTM
    def init_hidden(self):
        # the first is the hidden h
        # the second is the cell  c
        return (autograd.Variable(torch.zeros(1, self.batch_size, self.hidden_dim).cuda()),
                autograd.Variable(torch.zeros(1, self.batch_size, self.hidden_dim).cuda()))

    # Defining the feed forward logic of the LSTM. It contains:
    # 1. The embedding layer
    # 2. The LSTM layer with one hidden layer
    # 3. The software layer
    def forward(self, seq, freq, time, static):
        # reset the LSTM hidden state. Must be done before you run a new batch. Otherwise the LSTM will treat
        # a new batch as a continuation of a sequence
        self.hidden = self.init_hidden()

        # Get sequence lengths
        seq_lengths = torch.LongTensor(list(map(len, seq))) # length of 59

        # Pad the sequences
        seq = rnn_utils.pad_sequence(seq, batch_first = True)
        freq = rnn_utils.pad_sequence(freq, batch_first = True)
        time = rnn_utils.pad_sequence(time, batch_first = True)
        static = rnn_utils.pad_sequence(static, batch_first = True)

        seq = autograd.Variable(seq)
        freq = autograd.Variable(freq)
        time = autograd.Variable(time)
        static = autograd.Variable(static)

        # This is the pass to the embedding layer. 
        # The sequence is of dimension N and the output is N x Demb
        embeds = self.embeddings(seq)
        embeds = torch.cat((embeds,freq), dim=3)
        embeds = torch.cat((embeds,time), dim=3)
        print(embeds.size()) #torch.Size([32, 59, 7138, 32])

        x = embeds.view(self.batch_size, seq.size()[1], 1,-1)
        print(x.size()) #torch.Size([32, 59, 1, 228416])

        static = static.view(self.batch_size, -1,1,3)
        x = torch.cat([x, static], dim=3)
        print(x.size()) #torch.Size([32, 59, 1, 228419])

        # pack the padded sequence so that paddings are ignored
        x = torch.nn.utils.rnn.pack_padded_sequence(x, seq_lengths, batch_first=True)

        lstm_out, self.hidden = self.lstm(x, self.hidden)

        # unpack the packed padded sequence so that it is ready for prediction
        lstm_out = torch.nn.utils.rnn.pad_packed_sequence(lstm_out, batch_first=True)

        y = self.hidden2label(lstm_out[-1])
        log_probs = F.log_softmax(y)
        return log_probs


However, I get an error that is the following:

---&gt; 66         lstm_out, self.hidden = self.lstm(x, self.hidden)
     67 
     68         # unpack the packed padded sequence so that it is ready for prediction

~/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    487             result = self._slow_forward(*input, **kwargs)
    488         else:
--&gt; 489             result = self.forward(*input, **kwargs)
    490         for hook in self._forward_hooks.values():
    491             hook_result = hook(self, input, result)

~/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages/torch/nn/modules/rnn.py in forward(self, input, hx)
    173                 hx = (hx, hx)
    174 
--&gt; 175         self.check_forward_args(input, hx, batch_sizes)
    176         _impl = _rnn_impls[self.mode]
    177         if batch_sizes is None:

~/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages/torch/nn/modules/rnn.py in check_forward_args(self, input, hidden, batch_sizes)
    129             raise RuntimeError(
    130                 'input must have {} dimensions, got {}'.format(
--&gt; 131                     expected_input_dim, input.dim()))
    132         if self.input_size != input.size(-1):
    133             raise RuntimeError(

RuntimeError: input must have 2 dimensions, got 3
x = rnn_utils.pad_sequence(x, batch_first=True)
import torch


I thought LSTM model required 3 dimensional input? I am very confused as to why it requires 2 dimensions. How should I fix it?

Finally found solution after 5 hours of browsing...
def forward(self, input):
    input = input.unsqueeze(0).unsqueeze(0) 
    # Initializing hidden state for first input with zeros
    h0 = torch.zeros(1, input.size(0), 128)
    
    # Initializing cell state for first input with zeros
    c0 = torch.zeros(1, input.size(0), 128)
    hidden = (h0.detach(), c0.detach())
            
    out1, _ = self.lstm(input, hidden)
    out2 = F.leaky_relu(self.fc1(out1))
    qvalue = self.fc2(out2)
    return qvalue

