nn.LSTM in my class sent AttributeError: 'tuple' object has no attribute 'size'

In my case,

x = torch.randn(10, 6, input_size).to(device)
lstm = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True, bidirectional=True).to(device)
h0 = torch.randn(num_layers*2, int(x.size(0)), hidden_size).to(device) # 2 for bidirection 
c0 = torch.randn(num_layers*2, int(x.size(0)), hidden_size).to(device)
out, (hn, cn)  = lstm(x, (h0, c0))

Above code has no problem. But below case within the class sent AttributeError: ‘tuple’ object has no attribute ‘size’.

# Hyper Parameters 
from torchsummary import summary
import torch.optim as optim
num_classes = len(dataid)
input_size = num_classes+3
hidden_size = 256
num_layers = 2
# Model
class ScorePredRNN(nn.Module):
    def __init__(self, input_size, hidden_size, num_layers, num_classes):
        super(ScorePredRNN, self).__init__()
        self.hidden_size = hidden_size
        self.num_layers = num_layers
        self.lstm = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True, bidirectional=True).to(device)
        self.fc = nn.Linear(hidden_size, num_classes)  # 2 for bidirection
   
    def forward(self, x):
        h0 = torch.randn(self.num_layers*2, int(x.size(0)), self.hidden_size).to(device) # 2 for bidirection 
        c0 = torch.randn(self.num_layers*2, int(x.size(0)), self.hidden_size).to(device)     
        # Forward propagate LSTM
        out, (hn, cn)  = self.lstm(x, (h0, c0))#(h0, c0))  # out: tensor of shape (batch_size, seq_length, hidden_size*2)
        # Decode the hidden state of the last time step
        out = self.fc(out[:, -1, :])
        return out
model = ScorePredRNN(input_size, hidden_size, num_layers,  num_classes)#.to(device) 
optimizer = optim.Adam(model.parameters())
criterion = nn.CrossEntropyLoss()
summary(model,(6, input_size))

I can’t find out why this error is occured.
Please help me.

1 Like

In this line you try to find the size of x, but x is a tuple I’m guessing.


<ipython-input-61-78910da2943e> in forward(self, x)
     18         c0 = torch.randn(self.num_layers*2, int(x.size(0)), self.hidden_size).to(device)
     19         # Forward propagate LSTM
---> 20         out, (hn, cn)  = self.lstm(x, (h0, c0))#(h0, c0))  # out: tensor of shape (batch_size, seq_length, hidden_size*2)
     21         # Decode the hidden state of the last time step
     22         out = self.fc(out[:, -1, :])

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    493             result = self.forward(*input, **kwargs)
    494         for hook in self._forward_hooks.values():
--> 495             hook_result = hook(self, input, result)
    496             if hook_result is not None:
    497                 raise RuntimeError(

/usr/local/lib/python3.6/dist-packages/torchsummary/torchsummary.py in hook(module, input, output)
     21             if isinstance(output, (list, tuple)):
     22                 summary[m_key]["output_shape"] = [
---> 23                     [-1] + list(o.size())[1:] for o in output
     24                 ]
     25             else:

/usr/local/lib/python3.6/dist-packages/torchsummary/torchsummary.py in <listcomp>(.0)
     21             if isinstance(output, (list, tuple)):
     22                 summary[m_key]["output_shape"] = [
---> 23                     [-1] + list(o.size())[1:] for o in output
     24                 ]
     25             else:

AttributeError: 'tuple' object has no attribute 'size'```

I had the same problem, eventually I solve it by using SummaryX
pip install torchsummaryX
from torchsummaryX import summary