LSTM 'tuple' object has no attribute 'size'

username: Aymane_G-r
cooked: <p>After defining my model:<br>
‘’<br>
def forward(self,x):<br>
C1=F.relu(self.CONV1(x))<br>
M1=self.MAX1(C1)<br>
C2=F.relu(self.CONV2(M1))<br>
M2=self.MAX2(C2)<br>
C3=F.relu(self.CONV3(M2))<br>
M3=self.MAX3(C3)</p>
<pre><code>h0= Variable(torch.zeros(2,64,500))
c0=Variable(torch.zeros(2,64,500))
Pred,(hn,cn)=self.LSTM(M3,(h0,c0))''
</code></pre>
<p>With ‘’ summary(net, (1,4000)) ‘’ , I keep getting the following error:</p>
<p>’ AttributeError: ‘tuple’ object has no attribute ‘size’ ’</p>
<p>Does anyone know how to fix it ?</p>
<p>Thank you</p>

username: Abhilash_Srivastava
cooked: <p>Can you share your full code here for clarity?</p>

username: Aymane_G-r
cooked: <p>Here is my full code</p>
<p><strong>Model definition</strong></p>
<pre><code class="lang-auto">  def __init__(self):
    super(NET,self).__init__()

    #Couches de convolution - Encoder
    self.CONV1=nn.Conv1d(1,16,kernel_size=300,padding=0,stride=2)
    self.CONV2=nn.Conv1d(16,32,kernel_size=150,padding=0,stride=2)
    self.CONV3=nn.Conv1d(32,64,kernel_size=75,padding=0,stride=1)
    
    #Fonctions d'activation - Encoder 
    self.RELU=nn.ReLU()
    
    #Maxpooling - Encoder 
    self.MAX1=nn.MaxPool1d(kernel_size=2,stride=2,padding=0)
    self.MAX2=nn.MaxPool1d(kernel_size=2,stride=2,padding=0)
    self.MAX3=nn.MaxPool1d(kernel_size=2,stride=2,padding=0)

    #Bidirectionnel LSTM 
    self.LSTM=nn.LSTM(input_size=60,hidden_size=500,num_layers=2,batch_first=False) ```

**Definition of forward**
```def forward(self,x):

    C1=F.relu(self.CONV1(x))

    M1=self.MAX1(C1)

    C2=F.relu(self.CONV2(M1))

    M2=self.MAX2(C2)

    C3=F.relu(self.CONV3(M2))

    M3=self.MAX3(C3)

    h0= Variable(torch.zeros(2,64,500))

    c0= Variable(torch.zeros(2,64,500))

    Pred,out= self.LSTM(M3,(h0,c0))```

**Trying to have a summary of my model**

"from torchsummary import summary
summary(net, input_size=(1,4000))"

**The full error**
AttributeError                            Traceback (most recent call last)
&lt;ipython-input-162-69d32417aabe&gt; in &lt;module&gt;()
      1 from torchsummary import summary
----&gt; 2 summary(net, input_size=(1,4000))

5 frames
/usr/local/lib/python3.6/dist-packages/torchsummary/torchsummary.py in summary(model, input_size, batch_size, device)
     70     # make a forward pass
     71     # print(x.shape)
---&gt; 72     model(*x)
     73 
     74     # remove these hooks

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    720             result = self._slow_forward(*input, **kwargs)
    721         else:
--&gt; 722             result = self.forward(*input, **kwargs)
    723         for hook in itertools.chain(
    724                 _global_forward_hooks.values(),

&lt;ipython-input-160-c66aa62e34fe&gt; in forward(self, x)
     37     h0= Variable(torch.zeros(2,64,500))
     38     c0= Variable(torch.zeros(2,64,500))
---&gt; 39     Pred,out= self.LSTM(M3,(h0,c0))

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    724                 _global_forward_hooks.values(),
    725                 self._forward_hooks.values()):
--&gt; 726             hook_result = hook(self, input, result)
    727             if hook_result is not None:
    728                 result = hook_result

/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: ptrblck
cooked: <p>I think this is a known issue in <code>torchsummary</code>, which doesn’t seem to support RNNs as seen <a href="https://github.com/sksq96/pytorch-summary/issues/130">here</a>.<br>
Also, there is a fork in <a href="https://github.com/TylerYep/torch-summary">torch-summary</a> which has apparently fixed this issue.</p>

username: Aymane_G-r
cooked: <p>Thank you <a class="mention" href="/u/ptrblck">@ptrblck</a>, changing to torch-summary fixed the problem.</p>