Issue with appending to torch.nn.ModuleList

4158ndfkvBHJ1: <p>I have a very confusing issue, I wrote the following code :</p>
<pre><code class="lang-auto">import torch 

class encoder(torch.nn.Module):

    def __init__(self, in_channels, **kwargs) -&gt; None:

        super(encoder, self).__init__()

        self.modules = torch.nn.ModuleList([])
        hidden_dims = [32, 64, 128, 256, 512]

        for h_dim in hidden_dims:
            self.modules.append(torch.nn.Conv2d(in_channels, out_channels=h_dim, kernel_size= 3, stride= 2, padding  = 1))
            self.modules.append(torch.nn.BatchNorm2d(h_dim))
            self.modules.append(torch.nn.LeakyReLU)
            in_channels = h_dim

    def forward(self, x):
        for module in self.modules:
            x = module(x)
        x = torch.flatten(x)
        return x

test = encoder(1)
</code></pre>
<p>And I get the following error :</p>
<pre><code class="lang-auto">self.modules.append(torch.nn.Conv2d(in_channels, out_channels=h_dim, kernel_size= 3, stride= 2, padding  = 1))
AttributeError: 'function' object has no attribute 'append'
</code></pre>
<p>I’ve been going over it for the past hour, but I can’t find why it doesn’t work. I’ve found lots of examples that show that I can append a torch.nn.Module to a torch.nn.ModuleList (<a href="https://github.com/akanimax/BMSG-GAN/blob/master/sourcecode/MSG_GAN/GAN.py" class="inline-onebox" rel="noopener nofollow ugc">BMSG-GAN/GAN.py at master · akanimax/BMSG-GAN · GitHub</a> see line 49), but it fails in this case, and I can’t figure out why.<br>
It’s probably something realy dumb but I can’t figure it out. I would be grateful for any help <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"> !</p>

InnovArul: <p><code>.modules()</code> is a function defined under class <code>nn.Module</code>.</p><aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/pytorch/pytorch/blob/75ee5756715e7161314ce037474843b68f69fc04/torch/nn/modules/module.py#L1367-L1392" target="_blank" rel="noopener">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/pytorch/pytorch/blob/75ee5756715e7161314ce037474843b68f69fc04/torch/nn/modules/module.py#L1367-L1392" target="_blank" rel="noopener">pytorch/pytorch/blob/75ee5756715e7161314ce037474843b68f69fc04/torch/nn/modules/module.py#L1367-L1392</a></h4>
<pre class="onebox"><code class="lang-py"><ol class="start lines" start="1367" style="counter-reset: li-counter 1366 ;">
<li>def modules(self) -&gt; Iterator['Module']:</li><li>    r"""Returns an iterator over all modules in the network.</li><li></li><li>    Yields:</li><li>        Module: a module in the network</li><li></li><li>    Note:</li><li>        Duplicate modules are returned only once. In the following</li><li>        example, ``l`` will be returned only once.</li><li></li><li>    Example::</li><li></li><li>        &gt;&gt;&gt; l = nn.Linear(2, 2)</li><li>        &gt;&gt;&gt; net = nn.Sequential(l, l)</li><li>        &gt;&gt;&gt; for idx, m in enumerate(net.modules()):</li><li>                print(idx, '-&gt;', m)</li><li></li><li>        0 -&gt; Sequential(</li><li>          (0): Linear(in_features=2, out_features=2, bias=True)</li><li>          (1): Linear(in_features=2, out_features=2, bias=True)</li>
</ol></code></pre>

  This file has been truncated. <a href="https://github.com/pytorch/pytorch/blob/75ee5756715e7161314ce037474843b68f69fc04/torch/nn/modules/module.py#L1367-L1392" target="_blank" rel="noopener">show original</a>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>

<p>You need to rename the variable <code>self.modules</code> as it is conflicting with this function.</p>

4158ndfkvBHJ1: <p>Thanks a lot ! that makes complete sense, I should be more careful about that</p>