Can some one give me a working example of nn.ModuleDict?

The code from documentation will give back an error if you add

model = MyModule()

class MyModule(nn.Module):
    def __init__(self):
        super(MyModule, self).__init__()
        self.choices = nn.ModuleDict({
                'conv': nn.Conv2d(10, 10, 3),
                'pool': nn.MaxPool2d(3)
        self.activations = nn.ModuleDict([
                ['lrelu', nn.LeakyReLU()],
                ['prelu', nn.PReLU()]

    def forward(self, x, choice, act):
        x = self.choices[choice](x)
        x = self.activations[act](x)
        return x


AttributeError                            Traceback (most recent call last)
<ipython-input-4-a70ce091060c> in <module>()
----> 1 model = MyModule()

<ipython-input-3-20f44d4e0458> in __init__(self)
      2     def __init__(self):
      3         super(MyModule, self).__init__()
----> 4         self.choices = nn.ModuleDict({
      5                 'conv': nn.Conv2d(10, 10, 3),
      6                 'pool': nn.MaxPool2d(3)

AttributeError: module 'torch.nn' has no attribute 'ModuleDict'

Which PyTorch version are you using?
If < 0.4.1, could you try to update and run it again?

I am using 0.4.0 I will update and report back.

Updating to 0.4.1 has solved the problem. Thank you.

This may be a late reply, but would help some using moduledict.
I have implemented the AlexNet

class AlexNet(BaseModel):
    def __init__(self, num_classes=1000):
        super(AlexNet, self).__init__()
        self.features =  nn.ModuleDict({
            "Conv2d_1":nn.Conv2d(3, 64, kernel_size=11, stride=4, padding=2),
            "Max_1":nn.MaxPool2d(kernel_size=3, stride=2),
            "Conv2d_2":nn.Conv2d(64, 192, kernel_size=5, padding=2),
            "Max_2":nn.MaxPool2d(kernel_size=3, stride=2),
            "Conv2d_3":nn.Conv2d(192, 384, kernel_size=3, padding=1),
            "Conv2d_4":nn.Conv2d(384, 256, kernel_size=3, padding=1),
            "Conv2d_5":nn.Conv2d(256, 256, kernel_size=3, padding=2),
            "Max_5":nn.MaxPool2d(kernel_size=3, stride=2)

        self.avgpool = nn.AdaptiveAvgPool2d((6, 6))

        self.fullyconnected = nn.ModuleDict({
            "Pool":nn.AdaptiveAvgPool2d((6, 6)),
            "Linear_6":nn.Linear(256 * 6 * 6, 4096),
            "Linear_7":nn.Linear(4096, 4096),
            "Linear_8":nn.Linear(4096, num_classes),

    def forward(self, x):
        x = self.features['Conv2d_1'](x)
        x = self.features['Act_1'](x)
        x = self.features['Max_1'](x)
        x = self.features['Conv2d_2'](x)
        x = self.features['Act_2'](x)
        x = self.features['Max_2'](x)
        x = self.features['Conv2d_3'](x)
        x = self.features['Act_3'](x)
        x = self.features['Conv2d_4'](x)
        x = self.features['Act_4'](x)
        x = self.features['Conv2d_5'](x)
        x = self.features['Act_5'](x)
        x = self.features['Max_5'](x)
        x = self.avgpool(x)
        x = x.view(-1, 256 * 6 * 6)
        x = self.fullyconnected['Linear_6'](x)
        x = self.fullyconnected['Act_6'](x)
        x = self.fullyconnected['Linear_7'](x)
        x = self.fullyconnected['Act_7'](x)
        x = self.fullyconnected['Linear_8'](x)
        return x

Happy Learning !!

1 Like

how can i update to 0.4.1? please

You shouldn’t “update” to 0.4.1 anymore, as this version is quite old by now and you would most likely downgrade PyTorch now.
I would recommend to use the latest stable release (1.8.0) or the nightly binaries instead.

I had this problem, although i installed torch and didn’t get error when import it.

You would have to use the uppercase M in nn.Module.