nn.Module's object didn't recognize parameters

username: hungsvdut2k2
cooked: <p>I have defined 4 weight matrices as you can see in the below image, but my Model which is subclass of nn.Module didn’t find out that these matrices are its parameters. Can anyone help me to resolve this problem<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/1/6/16664b8ac466d0a41a6d55ea133226891958403d.png" data-download-href="https://discuss.pytorch.org/uploads/default/16664b8ac466d0a41a6d55ea133226891958403d" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/1/6/16664b8ac466d0a41a6d55ea133226891958403d_2_690x388.png" alt="image" data-base62-sha1="3c9FzDvNkiqsN0xnv0OrLJyaScd" width="690" height="388" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/1/6/16664b8ac466d0a41a6d55ea133226891958403d_2_690x388.png, https://discuss.pytorch.org/uploads/default/optimized/3X/1/6/16664b8ac466d0a41a6d55ea133226891958403d_2_1035x582.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/1/6/16664b8ac466d0a41a6d55ea133226891958403d_2_1380x776.png 2x" data-dominant-color="282B32"><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">image</span><span class="informations">1920×1080 334 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></p>

username: ptrblck
cooked: <p>The <code>.to()</code> call is a differentiable operation and will create non-leaf tensors which won’t be registered in the parent module.<br>
Call <code>model.to(device)</code> instead or call <code>.to()</code> on the internal tensor instead of the <code>nn.Parameter</code>.</p>
<p>PS: it’s better to post code snippets by wrapping them into three backticks ``` instead of screenshots.</p>

username: hungsvdut2k2
cooked: <p>i have fixed my code using your solution but it still not works, can you give me another solution</p>

username: ptrblck
cooked: <p>Could you post a minimal and executable code snippet reproducing the issue, please?</p>

username: hungsvdut2k2
cooked: <p>This is my code for MLPMixer class:</p>
<pre><code class="lang-auto">class MlpMixer(nn.Module):
    def __init__(
        self,
        patch_size: int,
        s: int,
        c: int,
        ds: int,
        dc: int,
        num_mlp_blocks: int,
        num_classes: int,
    ):
        self.c = c
        self.s = s
        self.ds = ds
        self.dc = dc
        super().__init__()
        self.num_classes = num_classes
        super().__init__()
        self.mixer_blocks = [MixerBlock(s, c, ds, dc) for i in range(num_mlp_blocks)]
        self.num_classes = num_classes
        self.classifier = nn.Sequential(nn.Flatten(), nn.Dropout(0.2))
        self.patches_extract = Patches(patch_size)

    def forward(self, x):
        patches = self.patches_extract(x)
        for block in self.mixer_blocks:
            patches = block(patches)
        output = self.classifier(patches)
        if self.num_classes == 2:
            output = nn.Linear(self.c * self.s, 1)(output)
            output = nn.Sigmoid()(output)
        else:
            output = nn.Linear(self.c * self.s, self.num_classes)(output)
            output = nn.Softmax()(output)
        return output
</code></pre>
<p>This is my code for testing</p>
<pre><code class="lang-auto">input_tensor = torch.rand(16, 3, 224, 224)
model = MlpMixer(16, 196, 768, 2048, 512, 8, 2)
for param in model.parameters():
    print(param)
</code></pre>
<p>and the result is nothing happens</p>

username: ptrblck
cooked: <p>Your code is not executable so I cannot confirm what exactly is broken, but <code>self.mixer_blocks</code> should not be a plain Python list but an <code>nn.ModuleList</code> object to properly register the submodules.</p>

username: hungsvdut2k2
cooked: <p>it works, thank you so much</p>