'tuple' object has no attribute 'size' in lstm but not in rnn

I have a two models for encoder-decorder architecture

class Encoder(nn.Module):

    def __init__(self, vector_size):

        """Load the pretrained ResNet-152 and replace top fc layer."""

        super(Encoder, self).__init__()

        resnet = models.resnet18(pretrained=True)

        modules = list(resnet.children())[:-1]      # delete the last fc layer.

        self.resnet = nn.Sequential(*modules)

        self.linear = nn.Linear(resnet.fc.in_features, vector_size)

        # self.resnet = nn.Sequential(resnet, nn.Linear(1000, vector_size),

        #               nn.ReLU())

        self.bn = nn.BatchNorm1d(vector_size, momentum=0.01)


    def forward(self, images):

        """Extract feature vectors from input images."""

        with torch.no_grad():

            features = self.resnet(images)

        features = features.reshape(features.size(0), -1)

        features = self.bn(self.linear(features))


        return features

And another model

class Decoder(nn.Module):

    def __init__(self, input_dim, hidden_dim, layer_no, output_dim):



        self.hidden_dim = hidden_dim

        self.layer_no = layer_no


        self.lstm = nn.LSTM(input_dim,hidden_dim,layer_no, batch_first=True)

        self.linear = nn.Linear(hidden_dim, output_dim)


    def forward(self,x):

        if torch.cuda.is_available():

            h0 = Variable(torch.zeros(self.layer_no, x.size(0), self.hidden_dim).cuda())

            c0 = Variable(torch.zeros(self.layer_no, x.size(0), self.hidden_dim).cuda())


            h0 = Variable(torch.zeros(self.layer_no, x.size(0), self.hidden_dim))

            c0 = Variable(torch.zeros(self.layer_no, x.size(0), self.hidden_dim))

        out, (hn,cn) = self.lstm(x,(h0,c0))

        out = self.linear(out[:, -1, :])

        # out = self.linear(out[0])

        return out

But during training time I get this error

AttributeError                            Traceback (most recent call last)
<ipython-input-79-a8aac8e36479> in <module>()
----> 1 train_model(n_epoch,data_load)
      2 # iter = 0
      3 # n = []
      4 # for epoch in range(n_epoch):
      5 #     for i, (images, labels) in enumerate(data_load['train']):

5 frames
/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'

The above code works when I use it with a RNN instead of a LSTM. What am I doing wrong here?

1 Like

Hi, I encountered a similar problem yesterday while working with LSTM, what I did was I replaced

out, (hn,cn) = self.lstm(x,(h0,c0))


out, state = self.lstm(x, state)

where the state is the same tuple containing h_0, c_0, this solved the error.

hello, Do you mean something like this?

state = (h0,c0)
out, state = self.lstm(x, state)

you may want to try torchsummaryX , which can interpret RNNs corrent

1 Like

great. it worked for me. I had the same error when running model summary on lstm