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 outputs, _ = self.lstm1(features_spaces, (hidden_state1.detach(), hidden_cell_1.detach()))
, this error is lifted RuntimeError: For unbatched 2-D input, hx and cx should also be 2-D but got (3-D, 3-D) tensors
. Please , I need your help, thanks.
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))