RuntimeError: expected scalar type Long but found Float (conv1d))

class text_CNN(nn.Module):
  def __init__(self):
    super(text_CNN, self).__init__()
    self.conv1 = nn.Conv1d(in_channels=1, out_channels=10, kernel_size=2)

  def forward(self, x):
    print(x.type())
    x = F.max_pool1d(F.relu(self.conv1(x)), 2)
    return x

model = text_CNN()
x = torch.randint(2, (16, 1, 22))
model(x)

Output:

torch.LongTensor
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-27-f0bfac495e81> in <module>()
     11 model = text_CNN()
     12 x = torch.randint(2, (16, 1, 22))
---> 13 model(x)

3 frames
/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    725             result = self._slow_forward(*input, **kwargs)
    726         else:
--> 727             result = self.forward(*input, **kwargs)
    728         for hook in itertools.chain(
    729                 _global_forward_hooks.values(),

<ipython-input-27-f0bfac495e81> in forward(self, x)
      6   def forward(self, x):
      7     print(x.type())
----> 8     x = F.max_pool1d(F.relu(self.conv1(x)), 2)
      9     return x
     10 

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    725             result = self._slow_forward(*input, **kwargs)
    726         else:
--> 727             result = self.forward(*input, **kwargs)
    728         for hook in itertools.chain(
    729                 _global_forward_hooks.values(),

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/conv.py in forward(self, input)
    257                             _single(0), self.dilation, self.groups)
    258         return F.conv1d(input, self.weight, self.bias, self.stride,
--> 259                         self.padding, self.dilation, self.groups)
    260 
    261 

RuntimeError: expected scalar type Long but found Float

However, when I change the type to a float:

x = torch.randint(2, (16, 1, 22), dtype=torch.float)

It works! Why is this?

PyTorch expects the input tensor and model parameters to have the same dtype and thus raises the error. torch.randint returns a LongTensor, while the model parameters are initialized as FloatTensors, which is why you need to change the input to torch.float.

1 Like

I see, but why is the error message stating that it is expecting scalar type long, while it is actually expecting a type float?

The error message might be confusing as the data type mismatch points to one of the used tensors. Based on the error it seems that the first tensor (input in this case) is used as the expected type.

1 Like