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

inkplay: <p>The code from documentation will give back an error if you add</p>
<p><code>model = MyModule()</code></p>
<pre><code class="lang-auto">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
</code></pre>
<p>Error:</p>
<pre><code class="lang-auto">AttributeError                            Traceback (most recent call last)
&lt;ipython-input-4-a70ce091060c&gt; in &lt;module&gt;()
----&gt; 1 model = MyModule()

&lt;ipython-input-3-20f44d4e0458&gt; in __init__(self)
      2     def __init__(self):
      3         super(MyModule, self).__init__()
----&gt; 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'
</code></pre>

ptrblck: <p>Which PyTorch version are you using?<br>
If <code>&lt; 0.4.1</code>, could you try to update and run it again?</p>

inkplay: <p>I am using 0.4.0 I will update and report back.</p>
<p>Updating to 0.4.1 has solved the problem. Thank you.</p>

Janashree_Ananthan: <p>This may be a  late reply, but would help some using moduledict.<br>
I have implemented the AlexNet</p>
<pre><code class="lang-auto">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),
            #"BN_1":nn.BatchNorm2d(64),
            "Act_1":nn.ReLU(inplace=True),
            "Max_1":nn.MaxPool2d(kernel_size=3, stride=2),
            "Conv2d_2":nn.Conv2d(64, 192, kernel_size=5, padding=2),
            #"BN_2":nn.BatchNorm2d(192),
            "Act_2":nn.ReLU(inplace=True),
            "Max_2":nn.MaxPool2d(kernel_size=3, stride=2),
            "Conv2d_3":nn.Conv2d(192, 384, kernel_size=3, padding=1),
            #"BN_3":nn.BatchNorm2d(384),
            "Act_3":nn.ReLU(inplace=True),
            "Conv2d_4":nn.Conv2d(384, 256, kernel_size=3, padding=1),
            #"BN_4":nn.BatchNorm2d(64),
            "Act_4":nn.ReLU(inplace=True),
            "Conv2d_5":nn.Conv2d(256, 256, kernel_size=3, padding=2),
            #"BN_5":nn.BatchNorm2d(64),
            "Act_5":nn.ReLU(inplace=True),
            "Max_5":nn.MaxPool2d(kernel_size=3, stride=2)
        })

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

        self.fullyconnected = nn.ModuleDict({
            "Pool":nn.AdaptiveAvgPool2d((6, 6)),
            "drop_6":nn.Dropout(),
            "Linear_6":nn.Linear(256 * 6 * 6, 4096),
            #"BN_6":nn.BatchNorm1d(4096),
            "Act_6":nn.ReLU(inplace=True),
            "drop_7":nn.Dropout(),
            "Linear_7":nn.Linear(4096, 4096),
            #"BN_7":nn.BatchNorm1d(4096),
            "Act_7":nn.ReLU(inplace=True),
            "Linear_8":nn.Linear(4096, num_classes),
            #"BN_8":nn.BatchNorm1d(num_classes),
            #"Softmax":nn.LogSoftmax()
        })

    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
</code></pre>
<p>Happy Learning !!</p>

wessam-essam-max: <p>how can i update to 0.4.1? please</p>

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

wessam-essam-max: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/0/b/0bbac6962659126eaef57b7b6b2f77b361d690b7.png" data-download-href="https://discuss.pytorch.org/uploads/default/0bbac6962659126eaef57b7b6b2f77b361d690b7" title="Capture"><img src="https://discuss.pytorch.org/uploads/default/original/3X/0/b/0bbac6962659126eaef57b7b6b2f77b361d690b7.png" alt="Capture" data-base62-sha1="1FLpScVK9uLrFZqOE0ZavY1q4tx" width="690" height="337" data-dominant-color="F6F6F6"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Capture</span><span class="informations">829×405 15.6 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
I had this problem, although i installed torch and didn’t get error when import it.</p>

ptrblck: <p>You would have to use the uppercase <code>M</code> in <code>nn.Module</code>.</p>