mohammad B
mohammad B

Reputation: 11

input must have 3 dimensions, got 2 Error in create LSTM Classifier

The structure of the network must be as follows:

(lstm): LSTM(1, 64, batch_first=True)

(fc1): Linear(in_features=64, out_features=32, bias=True)

(relu): ReLU()

(fc2): Linear(in_features=32, out_features=5, bias=True)

I wrote this code:

class LSTMClassifier(nn.Module):

    def __init__(self):
        super(LSTMClassifier, self).__init__() 
        self.lstm = nn.LSTM(1, 64, batch_first=True)
        self.fc1 = nn.Linear(in_features=64, out_features=32, bias=True)
        self.relu = nn.ReLU()
        self.fc2 = nn.Linear(in_features=32, out_features=5, bias=True)
         

    def forward(self, x):
       x = torch.tanh(self.lstm(x)[0])
       x = self.fc1(x)
       x = F.relu(x)
       x = self.fc2(x)

This is for test:

    (batch_data, batch_label) = next (iter (train_loader))
    model = LSTMClassifier().to(device)
    output = model (batch_data.to(device)).cpu()
    assert output.shape == (batch_size, 5)
    print ("passed")

The error is:

----> 3 output = model (batch_data.to(device)).cpu()

5 frames /usr/local/lib/python3.7/dist-packages/torch/nn/modules/rnn.py in check_input(self, input, batch_sizes) 201 raise RuntimeError( 202 'input must have {} dimensions, got {}'.format( --> 203 expected_input_dim, input.dim())) 204 if self.input_size != input.size(-1): 205 raise RuntimeError(

RuntimeError: input must have 3 dimensions, got 2

What is my problem?

Upvotes: 1

Views: 1688

Answers (1)

MKDYasserH
MKDYasserH

Reputation: 58

LSTMs support 3 dimensional input (sample, time-step, features). You need to transform your input from 2D to 3D. To do so, you can :

Use reshape function

First, you need the shape of your 2D input using batch_data.shape. Let's assume the shape of your 2D input is (15, 4). Now to reshape your input from 2D to 3D you use the reshape function np.reshape(data, new_shape)

    (batch_data, batch_label) = next (iter (train_loader))
    batch_data = np.reshape(batch_data, (15, 4, 1)) # line to add
    model = LSTMClassifier().to(device)
    output = model (batch_data.to(device)).cpu()
    assert output.shape == (batch_size, 5)
    print ("passed")

Later on, you will also need to reshape your test data from 2D to 3D.

Add RepeatVector Layer

This layer is implemented in Keras, I'm not sure if it's available in PyTorch which is your case. This layer adds an extra dimension to your data (repeats the input n times). For example you can convert a 2D input (batch size, input size) to a 3D input (batch_size, sequence_length, input size).

Upvotes: 1

Related Questions