RuntimeError: expected scalar type Int but found Float

hjyxxxxxl: <p>I tried to run the following code:</p>
<pre><code class="lang-auto">class ResBlock(nn.Module):
    def __init__(self, in_channel, out_channel):
        super(ResBlock, self).__init__()

        self.bn = nn.BatchNorm2d(in_channel)
        self.relu = nn.ReLU(inplace=True)
        self.conv = nn.Conv2d(in_channels=in_channel, out_channels=out_channel, kernel_size=3, stride=1)

    def forward(self, x):
        identity = x
        out = self.bn(x)
        out = self.relu(out)
        out = self.relu(self.conv(out))
        out += identity
        return out


class Net(nn.Module):
    def __init__(self, h, w):
        super().__init__()
        self.h = h
        self.w = w

        self.conv1 = nn.Conv2d(in_channels=1, out_channels=32, kernel_size=2, stride=1)
        self.res1 = ResBlock(in_channel=16, out_channel=32)
        self.res2 = ResBlock(in_channel=32, out_channel=64)
        self.fc1 = nn.Linear(64*w*h, 4*w*h-2*w-2*h)
        self.fc2 = nn.Linear(4*w*h-2*w-2*h, 2*w*h-w-h)

    def forward(self, x):
        x = x.unsqueeze(0)
        x = x.unsqueeze(0)
        out = F.relu(self.conv1(x))
        out = self.res1(out)
        out = self.res2(out)
        out = out.view(-1, 64*self.w*self.h)

        return out


net = Net(3, 3)
a = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
out = net(a)
</code></pre>
<p>but got the following error:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/a/1/a1cb6cbcff53b21f69ceb1e04f64d41167211466.png" data-download-href="https://discuss.pytorch.org/uploads/default/a1cb6cbcff53b21f69ceb1e04f64d41167211466" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/a/1/a1cb6cbcff53b21f69ceb1e04f64d41167211466_2_690x219.png" alt="image" data-base62-sha1="n5iHUCvA5R5PYLpj1FDTrtrE1ym" width="690" height="219" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/a/1/a1cb6cbcff53b21f69ceb1e04f64d41167211466_2_690x219.png, https://discuss.pytorch.org/uploads/default/optimized/3X/a/1/a1cb6cbcff53b21f69ceb1e04f64d41167211466_2_1035x328.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/a/1/a1cb6cbcff53b21f69ceb1e04f64d41167211466_2_1380x438.png 2x" data-dominant-color="313034"><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">1386×441 71.8 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>
<p>If I change the last line to:</p>
<pre><code class="lang-auto">out = net(a.to(torch.int32))
</code></pre>
<p>I will get the following error:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/d/9/d9bd1871c17194bb7d279896828a8a3112d6622d.png" data-download-href="https://discuss.pytorch.org/uploads/default/d9bd1871c17194bb7d279896828a8a3112d6622d" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/d/9/d9bd1871c17194bb7d279896828a8a3112d6622d_2_690x221.png" alt="image" data-base62-sha1="v4cKgLwVns2Z8gOjHJUkwf9hasl" width="690" height="221" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/d/9/d9bd1871c17194bb7d279896828a8a3112d6622d_2_690x221.png, https://discuss.pytorch.org/uploads/default/optimized/3X/d/9/d9bd1871c17194bb7d279896828a8a3112d6622d_2_1035x331.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/d/9/d9bd1871c17194bb7d279896828a8a3112d6622d_2_1380x442.png 2x" data-dominant-color="313034"><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">1389×445 73.1 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>
<p>Any help will be appreciated!</p>

ptrblck: <p>PyTorch parameters are created as <code>float32</code> tensors by default and expect the input to have the same type.<br>
Use <code>net(a.float())</code> to create a <code>FloatTensor</code> and it should work.</p>

hjyxxxxxl: <p>Thank you so much!. It solves the problem.</p>

hitbuyi: <p>I also encounter such problem, it works for me.</p>