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

username: Ayano_Kondo
cooked: <p>In my case,</p>
<pre><code class="lang-auto">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))
</code></pre>
<p>Above code has no problem. But below case within the class sent AttributeError: ‘tuple’ object has no attribute ‘size’.</p>
<pre><code class="lang-auto"># 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))
</code></pre>
<p>I can’t find out why this error is occured.<br>
Please help me.</p>

username: Prerna_Dhareshwar
cooked: <aside class="quote no-group quote-modified" data-username="Ayano_Kondo" data-post="1" data-topic="49542">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ayano_kondo/48/13707_2.png" class="avatar"> Ayano_Kondo:</div>
<blockquote>
<p>h0 = torch.randn(self.num_layers<em>2, int(x.size(0)), self.hidden_size).to(device) # 2 for bidirection<br>
c0 = torch.randn(self.num_layers</em>2, int(x.size(0)), self.hidden_size).to(device)</p>
</blockquote>
</aside>
<p>In this line you try to find the size of x, but x is a tuple I’m guessing.</p>

username: Ayano_Kondo
cooked: <pre><code class="lang-auto">
&lt;ipython-input-61-78910da2943e&gt; 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
---&gt; 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():
--&gt; 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"] = [
---&gt; 23                     [-1] + list(o.size())[1:] for o in output
     24                 ]
     25             else:

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

AttributeError: 'tuple' object has no attribute 'size'```</code></pre>

username: RachelShalom
cooked: <p>I had the same problem, eventually I solve it by using SummaryX<br>
pip install torchsummaryX<br>
from torchsummaryX import summary</p>