Expected object of backend CUDA but got backend CPU for argument #2 'mat2' err

username: hiepnguyen034
cooked: <p>I received this error<br>
<code>Expected object of backend CUDA but got backend CPU for argument #2 'mat2'</code><br>
while trying to run my model. Here is what I have.</p>
<pre><code class="lang-auto">torch.cuda.current_device()

device = torch.device("cuda:0" if (torch.cuda.is_available() and ngpu &gt; 0) else "cpu")

class Flatten(nn.Module):
    def forward(self, x):        
        x = x.view(-1)
        return x

class Discriminator(nn.Module):
    def __init__(self,ngpu):
        super().__init__()
        self.ngpu=ngpu
        self.main=nn.Sequential(
            nn.Conv2d(in_channels=nc, out_channels=ndf, kernel_size=(4,4), stride=2, padding=1, bias=False),
            nn.LeakyReLU(0.2, inplace=True),
            nn.BatchNorm2d(ndf),
            nn.Conv2d(ndf, ndf * 2, kernel_size=(4,4), stride=2, padding=1, bias=False),
            nn.BatchNorm2d(ndf * 2),
            nn.LeakyReLU(0.2, inplace=True),
            nn.Conv2d(ndf*2, ndf * 4, kernel_size=(4,4), stride=2, padding=1, bias=False),
            nn.BatchNorm2d(ndf * 4),
            nn.LeakyReLU(0.2, inplace=True),
            nn.Conv2d(ndf * 4, ndf * 8, 4, 2, 1, bias=False),
            nn.BatchNorm2d(ndf * 8),
            nn.LeakyReLU(0.2, inplace=True),
            nn.Conv2d(ndf * 8, 1, 4, 1, 0, bias=False),
        )
    def forward(self, inputs):
        x= self.main(inputs)
        x=Flatten()(x)
        x=nn.Linear(x.size(-1),128)(x)
        x=nn.Sigmoid()(x)
        return x

model=Discriminator(ngpu).to(device)
a=torch.randn(128,3,128,128,device=device)
model(a)
</code></pre>
<p>I was trying to play around with the model in this <a href="https://pytorch.org/tutorials/beginner/dcgan_faces_tutorial.html" rel="nofollow noopener">tutorial</a> and all hyperparameters were taken from there. I even tried adding <code>.to(device)</code> to almost every single line of the code but it did not resolve the problem.</p>
<p>Any help is much appreciated.</p>

username: ptrblck
cooked: <p>Currently you are re-creating a <code>nn.Linear</code> layer in your <code>forward</code> method in each call.<br>
This will reinitialize its parameters in each forward pass, so that this layer won’t be able to learn anything.<br>
That being said, the <code>to()</code> operator also cannot push this module to the device, so that you would have to do it manually in the <code>forward</code> method.</p>
<p>However, since I assume you would like to train this layer, just define it inside <code>__init__</code>:</p>
<pre><code class="lang-python">class Discriminator(nn.Module):
    def __init__(self,ngpu):
        super().__init__()
        self.ngpu=ngpu
        self.main=nn.Sequential(
            nn.Conv2d(in_channels=nc, out_channels=ndf, kernel_size=(4,4), stride=2, padding=1, bias=False),
            nn.LeakyReLU(0.2, inplace=True),
            nn.BatchNorm2d(ndf),
            nn.Conv2d(ndf, ndf * 2, kernel_size=(4,4), stride=2, padding=1, bias=False),
            nn.BatchNorm2d(ndf * 2),
            nn.LeakyReLU(0.2, inplace=True),
            nn.Conv2d(ndf*2, ndf * 4, kernel_size=(4,4), stride=2, padding=1, bias=False),
            nn.BatchNorm2d(ndf * 4),
            nn.LeakyReLU(0.2, inplace=True),
            nn.Conv2d(ndf * 4, ndf * 8, 4, 2, 1, bias=False),
            nn.BatchNorm2d(ndf * 8),
            nn.LeakyReLU(0.2, inplace=True),
            nn.Conv2d(ndf * 8, 1, 4, 1, 0, bias=False),
        )
        self.fc = nn.Linear(INPUT_FEATURES, 128)

    def forward(self, inputs):
        x = self.main(inputs)
        x = x.view(x.size(0), -1)
        x = self.fc(x)
        x = torch.sigmoid(x)
        return x
</code></pre>
<p>I’ve also removed the <code>Flatten</code> module, since it flattens the complete tensor to a single dimension, which is probably also not desired.</p>

username: hiepnguyen034
cooked: <p>Thank you very much for your answer. This does indeed solve my problem. Just out of curiosity, can you clarify a bit on how to push the module to cuda “manually” in the <code>forward</code> method?</p>

username: ptrblck
cooked: <aside class="quote no-group" data-username="hiepnguyen034" data-post="3" data-topic="46475">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/hiepnguyen034/48/12872_2.png" class="avatar"> hiepnguyen034:</div>
<blockquote>
<p>Just out of curiosity, can you clarify a bit on how to push the module to cuda “manually” in the <code>forward</code> method?</p>
</blockquote>
</aside>
<p>Just for the sake of completeness, as I don’t recommend doing it for the aforementioned issues:</p>
<pre data-code-wrap="python"><code class="lang-python">def forward(self, inputs):
        x = self.main(inputs)
        x = Flatten()(x)
        lin = nn.Linear(x.size(-1),128).to(device)
        x = lin(x)
        x = nn.Sigmoid()(x)
        return x
</code></pre>
<p>This will now create a new linear module in each forward pass, push the parameters to the device, and feed <code>x</code> to it. However, please don’t use this code, except you really want to create new linear layers in each forward pass.</p>