Unable to print Keras lke model summary in Pytorch

username: hs99
cooked: <pre><code class="lang-auto">class RNN(nn.Module):
    """RNN module(cell type lstm or gru)"""
    def __init__(
        self,
        input_size,
        hid_size,
        num_rnn_layers=1,
        dropout_p = 0.2,
        bidirectional = False,
        rnn_type = 'lstm',
    ):
        super().__init__()
        
        if rnn_type == 'lstm':
            self.rnn_layer = nn.LSTM(
                input_size=input_size,
                hidden_size=hid_size,
                num_layers=num_rnn_layers,
                dropout=dropout_p if num_rnn_layers&gt;1 else 0,
                bidirectional=bidirectional,
                batch_first=True,
            )
            
        else:
            self.rnn_layer = nn.GRU(
                input_size=input_size,
                hidden_size=hid_size,
                num_layers=num_rnn_layers,
                dropout=dropout_p if num_rnn_layers&gt;1 else 0,
                bidirectional=bidirectional,
                batch_first=True,
            )
    def forward(self, input):
        outputs, hidden_states = self.rnn_layer(input)
        return outputs, hidden_states

class RNNModel(nn.Module):
    def __init__(
        self,
        input_size,
        hid_size,
        rnn_type,
        bidirectional,
        n_classes=5,
        kernel_size=5,
    ):
        super().__init__()
            
        self.rnn_layer = RNN(
            input_size=46,#hid_size * 2 if bidirectional else hid_size,
            hid_size=hid_size,
            rnn_type=rnn_type,
            bidirectional=bidirectional
        )
        self.conv1 = ConvNormPool(
            input_size=input_size,
            hidden_size=hid_size,
            kernel_size=kernel_size,
        )
        self.conv2 = ConvNormPool(
            input_size=hid_size,
            hidden_size=hid_size,
            kernel_size=kernel_size,
        )
        self.avgpool = nn.AdaptiveAvgPool1d((1))
        self.fc = nn.Linear(in_features=hid_size, out_features=n_classes)

    def forward(self, input):
        #print("shape")
        #print(input.shape)
        x = self.conv1(input)
        x = self.conv2(x)
        x, _ = self.rnn_layer(x)
        x = self.avgpool(x)
        x = x.view(-1, x.size(1) * x.size(2))
        x = F.softmax(self.fc(x), dim=1)#.squeeze(1)
        return x
</code></pre>
<p>This is the RNN model…<br>
<strong>QUESTION 1</strong></p>
<pre><code class="lang-auto">m1=RNNModel(1, 64, 'lstm', True).to(device)

from torchsummary import summary
summary(m1, input_size=(1,187))

</code></pre>
<p><span class="hashtag">#batch</span> size is 32,<br>
On printing the summary, i get the following error <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>
<pre><code class="lang-auto">---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
&lt;ipython-input-93-6514f07d6d0b&gt; in &lt;module&gt;
      1 from torchsummary import summary
----&gt; 2 summary(m1, input_size=(1,187))

/opt/conda/lib/python3.7/site-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

/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    725             result = self._slow_forward(*input, **kwargs)
    726         else:
--&gt; 727             result = self.forward(*input, **kwargs)
    728         for hook in itertools.chain(
    729                 _global_forward_hooks.values(),

&lt;ipython-input-49-26860265b2db&gt; in forward(self, input)
     35         x = self.conv1(input)
     36         x = self.conv2(x)
---&gt; 37         x, _ = self.rnn_layer(x)
     38         x = self.avgpool(x)
     39         x = x.view(-1, x.size(1) * x.size(2))

/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    725             result = self._slow_forward(*input, **kwargs)
    726         else:
--&gt; 727             result = self.forward(*input, **kwargs)
    728         for hook in itertools.chain(
    729                 _global_forward_hooks.values(),

&lt;ipython-input-48-c383aa5a9c6e&gt; in forward(self, input)
     32             )
     33     def forward(self, input):
---&gt; 34         outputs, hidden_states = self.rnn_layer(input)
     35         return outputs, hidden_states

/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    729                 _global_forward_hooks.values(),
    730                 self._forward_hooks.values()):
--&gt; 731             hook_result = hook(self, input, result)
    732             if hook_result is not None:
    733                 result = hook_result

/opt/conda/lib/python3.7/site-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:

/opt/conda/lib/python3.7/site-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>PLEASE HELP!! it is very needful <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>
<p><strong>QUESTION 2</strong></p>
<p>Also i need to plot the model <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>
<pre><code class="lang-auto">batch = next(iter(train_loader_1))

from torchviz import make_dot

make_dot(m1(batch[0]), params=dict(list(m1.named_parameters()))).render("rnn1_torchviz")
</code></pre>
<p>is not working</p>
<pre><code class="lang-auto">---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
&lt;ipython-input-96-5d5771c12914&gt; in &lt;module&gt;
      1 from torchviz import make_dot
----&gt; 2 make_dot(m1(batch[0]), params=dict(list(m1.named_parameters()))).render("rnn1_torchviz")

/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    725             result = self._slow_forward(*input, **kwargs)
    726         else:
--&gt; 727             result = self.forward(*input, **kwargs)
    728         for hook in itertools.chain(
    729                 _global_forward_hooks.values(),

&lt;ipython-input-49-26860265b2db&gt; in forward(self, input)
     35         x = self.conv1(input)
     36         x = self.conv2(x)
---&gt; 37         x, _ = self.rnn_layer(x)
     38         x = self.avgpool(x)
     39         x = x.view(-1, x.size(1) * x.size(2))

/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    725             result = self._slow_forward(*input, **kwargs)
    726         else:
--&gt; 727             result = self.forward(*input, **kwargs)
    728         for hook in itertools.chain(
    729                 _global_forward_hooks.values(),

&lt;ipython-input-48-c383aa5a9c6e&gt; in forward(self, input)
     32             )
     33     def forward(self, input):
---&gt; 34         outputs, hidden_states = self.rnn_layer(input)
     35         return outputs, hidden_states

/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    729                 _global_forward_hooks.values(),
    730                 self._forward_hooks.values()):
--&gt; 731             hook_result = hook(self, input, result)
    732             if hook_result is not None:
    733                 result = hook_result

/opt/conda/lib/python3.7/site-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:

/opt/conda/lib/python3.7/site-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>This is the same error in both.Pls help!!</p>

username: Dwight_Foster
cooked: <p>In your rnn class in the forward function try not returning the hidden state. Then in your rnn model forward function delete the _. The problem might be that the two outputs are messing it up. If that does not work can you send your ConvNormPool code so I can try and recreate your error.</p>