Getting error (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same

shary: <pre><code class="lang-auto">class ConvBlock(nn.Module):
    def __init__(self, in_channels, out_channels, down=True, use_act=True, **kwargs):
        super().__init__()
        self.conv = nn.Sequential(
            nn.Conv2d(in_channels, out_channels, padding_mode="reflect", **kwargs)
            if down
            else nn.ConvTranspose2d(in_channels, out_channels, **kwargs),
            nn.InstanceNorm2d(out_channels),
            nn.ReLU(inplace=True) if use_act else nn.Identity()
        )
        
    def forward(self, x):
        return self.conv(x)

class ResidualBlock(nn.Module):
    def __init__(self, channels):
        super().__init__()
        self.block = nn.Sequential(
            ConvBlock(channels, channels, kernel_size=3, padding=1),
            ConvBlock(channels, channels, use_act=False, kernel_size=3, padding=1),
        )
        
    def forward(self, x):
        return x + self.block(x)

class Generator(nn.Module):
    def __init__(self, image_channels, num_features= 64, num_residuals=9):
        super().__init__()
        self.initial = nn.Sequential(
            nn.Conv2d(image_channels, num_features, kernel_size=7, stride=1, padding=3, padding_mode="reflect"),
            nn.ReLU(inplace=True)
        )
        self.down_blocks = nn.ModuleList = ([
            ConvBlock(num_features, num_features*2, kernel_size=3, stride=2, padding=1),
            ConvBlock(num_features*2, num_features*4, kernel_size=3, stride=2, padding=1),
        ])
        
        self.residual_blocks = nn.Sequential(
            *[ResidualBlock(num_features*4) for _ in range(num_residuals)]
        )
        
        self.up_blocks = nn.ModuleList = ([
            ConvBlock(num_features*4, num_features*2, down=False, kernel_size=3, padding=1, stride=2, output_padding=1),
            ConvBlock(num_features*2, num_features, down=False, kernel_size=3, padding=1, stride=2, output_padding=1),
        ])
        
        self.last = nn.Conv2d(num_features, image_channels, kernel_size=7, stride=1, padding=3, padding_mode="reflect")
        
    def forward(self, x):
        x = self.initial(x)
        
        for layer in self.down_blocks:
            x = layer(x)
            
        x = self.residual_blocks(x)
        
        for layer in self.up_blocks:
            x = layer(x)
            
        return torch.tanh(self.last(x))

img_channels = 3
img_size = 256
x = torch.randn((2, img_channels, img_size, img_size))
gen = Generator(img_channels, 9).to(DEVICE)
print(gen(x).shape)
</code></pre>
<p>On running this model I have made I get this error. I have spent hours and I am still unable to figure out where I am missing to set some Tensor or module to Cuda</p>
<hr>
<p>RuntimeError                              Traceback (most recent call last)<br>
 in <br>
3 x = torch.randn((2, img_channels, img_size, img_size))<br>
4 gen = Generator(img_channels, 9).to(DEVICE)<br>
----&gt; 5 print(gen(x).shape)</p>
<p>/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)<br>
725             result = self._slow_forward(*input, **kwargs)<br>
726         else:<br>
 → 727             result = self.forward(*input, **kwargs)<br>
728         for hook in itertools.chain(<br>
729                 _global_forward_hooks.values(),</p>
<p> in forward(self, x)<br>
23<br>
24     def forward(self, x):<br>
—&gt; 25         x = self.initial(x)<br>
26<br>
27         for layer in self.down_blocks:</p>
<p>/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)<br>
725             result = self._slow_forward(*input, **kwargs)<br>
726         else:<br>
 → 727             result = self.forward(*input, **kwargs)<br>
728         for hook in itertools.chain(<br>
729                 _global_forward_hooks.values(),</p>
<p>/opt/conda/lib/python3.7/site-packages/torch/nn/modules/container.py in forward(self, input)<br>
115     def forward(self, input):<br>
116         for module in self:<br>
 → 117             input = module(input)<br>
118         return input<br>
119</p>
<p>/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)<br>
725             result = self._slow_forward(*input, **kwargs)<br>
726         else:<br>
 → 727             result = self.forward(*input, **kwargs)<br>
728         for hook in itertools.chain(<br>
729                 _global_forward_hooks.values(),</p>
<p>/opt/conda/lib/python3.7/site-packages/torch/nn/modules/conv.py in forward(self, input)<br>
421<br>
422     def forward(self, input: Tensor) → Tensor:<br>
 → 423         return self._conv_forward(input, self.weight)<br>
424<br>
425 class Conv3d(_ConvNd):</p>
<p>/opt/conda/lib/python3.7/site-packages/torch/nn/modules/conv.py in _conv_forward(self, input, weight)<br>
416             return F.conv2d(F.pad(input, self._reversed_padding_repeated_twice, mode=self.padding_mode),<br>
417                             weight, self.bias, self.stride,<br>
 → 418                             _pair(0), self.dilation, self.groups)<br>
419         return F.conv2d(input, weight, self.bias, self.stride,<br>
420                         self.padding, self.dilation, self.groups)</p>
<p>RuntimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the same</p>

Sarmad_GTU: <p>I think you should convert the x value also to the device also</p>

shary: <p>oh sorry that was just for demonstration but the actual input is converted to device</p>

Sarmad_GTU: <p>also the img_channels, convert it to device</p>

shary: <p>The img_channels is used as a parameter for convolution layer so it has to be an integer not a Tensor</p>

Sarmad_GTU: <p>you are right<br>
put this line before this line: gen = Generator(img_channels, 9).to(DEVICE)</p>
<p>x = x.to(device)</p>

shary: <p>Nope same error. Anyways the actual input was being converted to DEVICE so that’s not the issue</p>