# CNN, LSTM RuntimeError: For unbatched 2-D input, hx and cx should also be 2-D but got (3-D, 3-D) tensors

username: vin
cooked: <p>Hello, I have designed a network to gather the CNN + LSTM and the goal is to feed the LSTM of the cnns network (more details in the code below). So, after initialization of hidden state, then use in this line of code <code>outputs, _ = self.lstm1(features_spaces, (hidden_state1.detach(), hidden_cell_1.detach()))</code>, this error is lifted <code>RuntimeError: For unbatched 2-D input, hx and cx should also be 2-D but got (3-D, 3-D) tensors</code>.  Please , I need your help, thanks.</p>
<pre><code class="lang-auto">import torch
import torch.nn as nn
import torch.nn.functional as F
from torchsummary import summary
import math


class LSTMNet(nn.ModuleList):
    """
    """
    def __init__(self, input_dim, hidden_dim1, hidden_dim2, layers_dim1, layers_dim2, output_dim):
        super(LSTMNet, self).__init__()

        # Hidden dimensions
        self.hidden1 = hidden_dim1
        self.hidden2 = hidden_dim2
        # Number of hidden layers
        self.layers_dim1 = layers_dim1
        self.layers_dim2 = layers_dim2

        self.cnn1 = nn.Conv2d(2, 16, 3, stride=1, padding=1)

        self.cnn2 = nn.Conv2d(16, 32, 3, stride=1, padding=1)

        self.cnn3 = nn.Conv2d(32, 64, 3, stride=1, padding=1)

        self.pool = nn.MaxPool2d(2, 2)

        self.lstm1 = nn.LSTM(input_dim, hidden_dim1, layers_dim1, batch_first=True)

        self.lstm2 = nn.LSTM(hidden_dim1, hidden_dim2, layers_dim2, batch_first=True)

        self.fc1 = nn.Linear(hidden_dim2, output_dim)

        self.fc2 = nn.Linear(output_dim, output_dim)

    def forward(self, x):
        # Initialize hidden state with zeros
        hidden_state1 = torch.zeros(self.layers_dim1, x.size(0), self.hidden1).requires_grad_()
        # Initialize cell state
        hidden_cell_1 = torch.zeros(self.layers_dim1, x.size(0), self.hidden1).requires_grad_()

        hidden_state2 = torch.zeros(self.layers_dim2, x.size(0), self.hidden2).requires_grad_()

        hidden_cell_2 = torch.zeros(self.layers_dim2, x.size(0), self.hidden2).requires_grad_()

        features_spaces = self.pool(F.relu(self.cnn1(x)))
        features_spaces = self.pool(F.relu(self.cnn2(features_spaces)))
        features_spaces = self.pool(F.relu(self.cnn3(features_spaces)))
        features_spaces = torch.flatten(features_spaces, 1)

        outputs, _ = self.lstm1(features_spaces, (hidden_state1.detach(), hidden_cell_1.detach()))
        print("Features : ", outputs.shape)

        outputs, _ = self.lstm2(outputs, (hidden_state2.detach(), hidden_cell_2.detach()))

        outputs = F.relu(self.fc1(outputs[:, -1, :]))

        outputs = self.fc2(outputs)

        return outputs


if __name__ == '__main__':
    INPUT_DIM = 9216
    HIDDEN_DIM1 = 50
    HIDDEN_DIM2 = 50
    LAYERS_DIM1 = 2
    LAYERS_DIM2 = 2
    OUTPUT_DIM = 10

    if torch.cuda.is_available():
        device = True
    else:
        device = False

    model = LSTMNet(INPUT_DIM, HIDDEN_DIM1, HIDDEN_DIM2, LAYERS_DIM1, LAYERS_DIM2, OUTPUT_DIM)

    summary(model, (2, 100, 100))


</code></pre>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/2/52950f23c09845de8e3961591723296faf35990f.png" data-download-href="https://discuss.pytorch.org/uploads/default/52950f23c09845de8e3961591723296faf35990f" title="21"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/5/2/52950f23c09845de8e3961591723296faf35990f_2_690x252.png" alt="21" data-base62-sha1="bMyvDD8ikvNdcAiVJEL1nRxSHL1" width="690" height="252" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/5/2/52950f23c09845de8e3961591723296faf35990f_2_690x252.png, https://discuss.pytorch.org/uploads/default/optimized/3X/5/2/52950f23c09845de8e3961591723296faf35990f_2_1035x378.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/5/2/52950f23c09845de8e3961591723296faf35990f_2_1380x504.png 2x" data-dominant-color="2F3036"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">21</span><span class="informations">1583×579 78.9 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

username: NancyShehata
cooked: <p>Hi, I got same error as you mentioned above, and I need help to solve it, did you find solution for it?</p>