problem with nn.ModuleDict ('method' object is not subscriptable)
I am trying to use nn.ModuleDict following this documentation page:
I have this PyTorch network:
class Net(nn.Module):
    def __init__(self, kernel_size):
        super(Net, self).__init__()
        modules = {}
        modules[&quot;layer1&quot;] = nn.Conv2d(3, 16, kernel_size=kernel_size, stride=1, padding=2)
        self.modules = nn.ModuleDict(modules)

    def forward(self, x):
        x = self.modules[&quot;layer1&quot;](x)

when I use the forward method, I get the following error:

'method' object is not subscriptable

when I change the forward method to:
  def forward(self, x):
        x = self.modules()[&quot;layer1&quot;](x)

I get the following error:

TypeError: 'generator' object is not subscriptable


The key &quot;modules&quot; is already used by nn.Module. The property is used to retrieve all modules of the model: see nn.Module.modules. You need to use another property name.
For example:
class Net(nn.Module):
    def __init__(self, kernel_size):
        super(Net, self).__init__()
        modules = {}
        modules[&quot;layer1&quot;] = nn.Conv2d(3, 16, 
            kernel_size=kernel_size, stride=1, padding=2)
        self.layers = nn.ModuleDict(modules)

    def forward(self, x):
        x = self.layers[&quot;layer1&quot;](x)
        return x

