Continuing the discussion from LSTM 'tuple' object has no attribute 'size':
Hey thanks for posting this solution I also tried with the torchsummary
from torchinfo
. But still continued getting the error regarding the AttributeError: 'tuple' object has no attribute 'size'
. Complete code of the model is here.
import torch.nn as nn
class LSTMModel(nn.Module):
def __init__(self, input_size, hidden_size, num_layers, output_size):
super(LSTMModel, self).__init__()
self.hidden_size = hidden_size
self.num_layers = num_layers
self.lstm1 = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True, dropout=0.2)
self.bn1 = nn.BatchNorm1d(hidden_size)
# self.lstm2 = nn.LSTM(hidden_size, 32, batch_first=True, dropout=0.2)
self.fc = nn.Linear(hidden_size, output_size)
self.softmax = nn.Softmax(dim=1)
def forward(self, x):
# Initialize hidden state with zeros
h0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size).to(device=x.device)
# Initialize cell state with zeros
c0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size).to(device=x.device)
#Xaviers initialization
torch.nn.init.xavier_normal_(h0)
torch.nn.init.xavier_normal_(c0)
# Forward propagate LSTM1
out, (h_final, c_final) = self.lstm1(x, (h0, c0))
# out = self.bn1(out)
# Forward propagate LSTM2
# out, _ = self.lstm2(out)
# Decode the hidden state of the last time step
out = self.fc(out[:, -1, :])
out = self.softmax(out)
return out