If the input_dim=2 in LSTM

I am new to Pytorch, Kindly help to understand error.

class LSTMClassifier(nn.Module):

def __init__(self, input_dim, seq_size, hidden_dim, label_size, batch_size, bidirectional, num_layers):

   



    super(LSTMClassifier, self).__init__()

    self.input_dim = input_dim

    self.seq_size = seq_size

    self.hidden_dim = hidden_dim

    self.batch_size = batch_size

    self.num_layers = num_layers

    self.bidirectional = bidirectional

    self.lstm = nn.LSTM(input_dim, hidden_dim, num_layers=num_layers, bidirectional=bidirectional, batch_first=True)

    self.hidden = self.init_hidden()

    self.fc = nn.Linear(hidden_dim*seq_size, 128)

    if bidirectional:

        self.fc = nn.Linear(2*hidden_dim*seq_size, 128)  

                  

    self.hidden2label = nn.Linear(128, label_size)



def init_hidden(self):

    first_size = 1

    if self.bidirectional:

        first_size = 2

    h0 = Variable(torch.zeros(first_size*self.num_layers, self.batch_size, self.hidden_dim)).float()

    c0 = Variable(torch.zeros(first_size*self.num_layers, self.batch_size, self.hidden_dim)).float()

    return (h0, c0) 

def forward(self, x):

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

    lstm_out = torch.squeeze(lstm_out) #original

    y = self.fc(lstm_out)

    y = F.relu(y)

    y = self.hidden2label(y)

    return y

…
/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py in linear(input, weight, bias)
1674 ret = torch.addmm(bias, input, weight.t())
1675 else:
-> 1676 output = input.matmul(weight.t())
1677 if bias is not None:
1678 output += bias

RuntimeError: size mismatch, m1: [320 x 2], m2: [20 x 128] at /pytorch/aten/src/TH/generic/THTensorMath.cpp:41

The linear layer is raising this error, as the number of input features from the activation doesn’t match the in_features from the layer.
Check the shape of lstm_out and make sure it’s [batch_size, *, in_features].

PS: I would also be careful with using torch.squeeze without specifying the dim argument, as it could also remove the batch dimension, if you are working with a single sample, which is usually not desired.

1 Like

I changed the line from
#self.fc = nn.Linear(hidden_dim*seq_size, 128) #original

to
self.fc = nn.Linear(self.hidden_dim, 128, bias=True)

error resolved… but result is something unexpected…