LSTM 'tuple' object has no attribute 'size'

After defining my model:
‘’
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,(hn,cn)=self.LSTM(M3,(h0,c0))''

With ‘’ summary(net, (1,4000)) ‘’ , I keep getting the following error:

’ AttributeError: ‘tuple’ object has no attribute ‘size’ ’

Does anyone know how to fix it ?

Thank you

Can you share your full code here for clarity?

Here is my full code

Model definition

  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)
<ipython-input-162-69d32417aabe> in <module>()
      1 from torchsummary import summary
----> 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)
---> 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:
--> 722             result = self.forward(*input, **kwargs)
    723         for hook in itertools.chain(
    724                 _global_forward_hooks.values(),

<ipython-input-160-c66aa62e34fe> in forward(self, x)
     37     h0= Variable(torch.zeros(2,64,500))
     38     c0= Variable(torch.zeros(2,64,500))
---> 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()):
--> 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"] = [
---> 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 think this is a known issue in torchsummary, which doesn’t seem to support RNNs as seen here.
Also, there is a fork in torch-summary which has apparently fixed this issue.

2 Likes

Thank you @ptrblck, changing to torch-summary fixed the problem.