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

username: Sayeed_Shaiban
cooked: <p>I have a two models for encoder-decorder architecture</p>
<pre><code class="lang-auto">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))

        print(features.shape)

        return features
</code></pre>
<p>And another model</p>
<pre><code class="lang-auto">class Decoder(nn.Module):

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

        super(Decoder,self).__init__()

        

        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())

        else:

            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
</code></pre>
<p>But during training time I get this error</p>
<pre><code class="lang-auto">---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
&lt;ipython-input-79-a8aac8e36479&gt; in &lt;module&gt;()
----&gt; 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 &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>
<p>The above code works when I use it with a RNN instead of a LSTM. What am I doing wrong here?</p>

username: aanurraj
cooked: <p>Hi, I encountered a similar problem yesterday while working with LSTM, what I did was I replaced</p>
<blockquote>
<p>out, (hn,cn) = self.lstm(x,(h0,c0))</p>
</blockquote>
<p>with</p>
<blockquote>
<p>out, state = self.lstm(x, state)</p>
</blockquote>
<p>where the state is the same tuple containing h_0, c_0, this solved the error.</p>

username: Sayeed_Shaiban
cooked: <p>hello, Do you mean something like this?</p>
<blockquote>
<p>state = (h0,c0)<br>
out, state = self.lstm(x, state)</p>
</blockquote>

username: niata
cooked: <p>you may want to try <a href="https://github.com/nmhkahn/torchsummaryX" rel="nofollow noopener">torchsummaryX</a> , which can interpret RNNs corrent</p>

username: RachelShalom
cooked: <p>great. it worked for me. I had the same error when running model summary on lstm</p>